Skip to content

Commit 6675bdc

Browse files
authored
Merge pull request #16 from gitpod-io/mikenikles/use-prisma-orm-15
Use Prisma ORM
2 parents 31c2ea2 + 8342d2d commit 6675bdc

File tree

9 files changed

+156
-18
lines changed

9 files changed

+156
-18
lines changed

.gitpod.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---
12
# For more details, please see https://www.gitpod.io/docs/references/gitpod-yml
23
image: gitpod/workspace-postgres
34

@@ -6,19 +7,27 @@ tasks:
67
command: |
78
export HMR_HOST=`gp url 3000`
89
npm run dev
10+
env:
11+
DATABASE_URL: postgres://gitpod@localhost/todos
12+
- command: npx prisma studio
13+
env:
14+
DATABASE_URL: postgres://gitpod@localhost/todos
915

1016
ports:
1117
- port: 3000
1218
onOpen: open-browser
1319
- port: 5432
1420
onOpen: ignore
21+
- port: 5555
22+
onOpen: open-browser
1523

1624
vscode:
1725
extensions:
1826
- svelte.svelte-vscode
27+
- prisma.prisma
1928

2029
github:
2130
prebuilds:
2231
master: true
2332
branches: true
24-
pullRequests: true
33+
pullRequests: true

package-lock.json

Lines changed: 81 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "full-stack-web-development",
33
"version": "0.0.1",
44
"scripts": {
5+
"predev": "prisma migrate dev",
56
"dev": "svelte-kit dev",
67
"build": "svelte-kit build",
78
"package": "svelte-kit package",
@@ -12,11 +13,15 @@
1213
"devDependencies": {
1314
"@sveltejs/adapter-auto": "next",
1415
"@sveltejs/kit": "next",
16+
"prisma": "^3.5.0",
1517
"svelte": "^3.44.0",
1618
"svelte-check": "^2.2.6",
1719
"svelte-preprocess": "^4.9.4",
1820
"tslib": "^2.3.1",
1921
"typescript": "^4.4.3"
2022
},
21-
"type": "module"
22-
}
23+
"type": "module",
24+
"dependencies": {
25+
"@prisma/client": "^3.5.0"
26+
}
27+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- CreateTable
2+
CREATE TABLE "Todo" (
3+
"uid" TEXT NOT NULL,
4+
"created_at" TIMESTAMP(3) NOT NULL,
5+
"text" TEXT NOT NULL,
6+
"done" BOOLEAN NOT NULL,
7+
8+
CONSTRAINT "Todo_pkey" PRIMARY KEY ("uid")
9+
);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Please do not edit this file manually
2+
# It should be added in your version-control system (i.e. Git)
3+
provider = "postgresql"

prisma/schema.prisma

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Learn more about this file in the docs: https://pris.ly/d/prisma-schema
2+
datasource db {
3+
provider = "postgresql"
4+
url = env("DATABASE_URL")
5+
}
6+
7+
generator client {
8+
provider = "prisma-client-js"
9+
binaryTargets = ["native"]
10+
}
11+
12+
model Todo {
13+
uid String @id @default(cuid())
14+
created_at DateTime
15+
text String
16+
done Boolean
17+
}

src/lib/prisma.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import Prisma, * as PrismaAll from "@prisma/client";
2+
3+
const PrismaClient = Prisma?.PrismaClient || PrismaAll?.PrismaClient;
4+
export default PrismaClient;

src/routes/todos/_api.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,47 @@
11
import type { Request } from "@sveltejs/kit";
2+
import PrismaClient from "$lib/prisma";
23

3-
// TODO: Persist in database
4-
let todos: Todo[] = [];
4+
const prisma = new PrismaClient();
55

6-
export const api = (request: Request, data?: Record<string, unknown>) => {
6+
export const api = async (request: Request, data?: Record<string, unknown>) => {
77
let body = {};
88
let status = 500;
99

1010
switch (request.method.toUpperCase()) {
1111
case "GET":
12-
body = todos;
12+
body = await prisma.todo.findMany();
1313
status = 200;
1414
break;
1515
case "POST":
16-
todos.push(data as Todo);
17-
body = data;
16+
body = await prisma.todo.create({
17+
data: {
18+
created_at: data.created_at as Date,
19+
done: data.done as boolean,
20+
text: data.text as string
21+
}
22+
})
1823
status = 201;
1924
break;
2025
case "DELETE":
21-
todos = todos.filter(todo => todo.uid !== request.params.uid)
26+
body = await prisma.todo.delete({
27+
where: {
28+
uid: request.params.uid
29+
}
30+
})
31+
// todos = todos.filter(todo => todo.uid !== request.params.uid)
2232
status = 200;
2333
break;
2434
case "PATCH":
25-
todos = todos.map(todo => {
26-
if (todo.uid === request.params.uid) {
27-
if (data.text) todo.text = data.text as string;
28-
else todo.done = data.done as boolean;
35+
body = await prisma.todo.update({
36+
where: {
37+
uid: request.params.uid
38+
},
39+
data: {
40+
done: data.done,
41+
text: data.text
2942
}
30-
return todo;
31-
});
43+
})
3244
status = 200;
33-
body = todos.find(todo => todo.uid === request.params.uid);
3445
break;
3546

3647
default:

src/routes/todos/index.json.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export const get: RequestHandler = (request) => {
77

88
export const post: RequestHandler<{}, FormData> = (request) => {
99
return api(request, {
10-
uid: `${Date.now()}`, // TODO: Replace with the UID from the datbase
1110
created_at: new Date(),
1211
text: request.body.get("text"),
1312
done: false

0 commit comments

Comments
 (0)