This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/pages/users/index.astro | |
--- | |
import { getAllUsers } from "../../lib/users"; | |
import Layout from "../layouts/Layout.astro"; | |
const users = await getAllUsers(); | |
if (!users) { | |
return new Response(null, { | |
status: 404, | |
statusText: "Not found", | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/components/CreateUserForm.svelte | |
<script> | |
const onSubmit = async (e) => { | |
const formData = new FormData(e.target); | |
const data = {}; | |
for (let field of formData) { | |
const [key, value] = field; | |
data[key] = value; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/pages/api/users.js | |
import { createUser, getAllUsers } from "../../lib/users"; | |
export const get = async () => { | |
const users = await getAllUsers(); | |
if (!users) { | |
return new Response(null, { | |
status: 404, | |
statusText: "Not found", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// /src/lib/users.js | |
import { Users } from "./mongodb"; | |
export const getAllUsers = async () => { | |
const users = await (await Users()).find({}).toArray(); | |
return users; | |
}; | |
export const createUser = async (newUser) => { | |
const user = await (await Users()).insert(newUser); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/lib/mongodb.js | |
import { MongoClient } from "mongodb"; | |
if (!import.meta.env.MONGODB_URI) { | |
throw new Error('Invalid environment variable: "MONGODB_URI"'); | |
} | |
const uri = import.meta.env.MONGODB_URI; | |
const options = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// astro.config.mjs | |
import { defineConfig } from "astro/config"; | |
import node from "@astrojs/node"; | |
import svelte from "@astrojs/svelte"; | |
export default defineConfig({ | |
output: "server", | |
adapter: node(), | |
integrations: [svelte()] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import { getAllUsers } from '../prisma/user' | |
const Homepage = ({ users }) => { | |
return ( | |
<div> | |
{users.map(user => ( | |
<div key={user.id}>{user.name}</div> | |
))} | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl - request GET \ | |
- url http://localhost:3000/api/users | |
curl - request POST \ | |
- url http://localhost:3000/api/users \ | |
- header 'Content-Type: application/json' \ | |
- data '{ | |
"name": "Bill Gates", | |
"email": "[email protected]", | |
"birthYear": 1955 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pages/api/user | |
import { | |
createUser, | |
deleteUser, | |
getAllUsers, | |
getUser, | |
updateUser | |
} from '../../prisma/user' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// /prisma/user.js | |
import prisma from './prisma' | |
// READ | |
export const getAllUsers = async () => { | |
const users = await prisma.user.findMany({}) | |
return users | |
} | |
export const getUser = async id => { |
NewerOlder