Skip to content

Commit 2888ff6

Browse files
committed
hooks experiments
1 parent c6b12b2 commit 2888ff6

31 files changed

+3504
-3466
lines changed

package-lock.json

+1,304-3,150
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,24 @@
3434
"tslib": "^2.4.1",
3535
"typescript": "^4.9.3",
3636
"vite": "^4.0.0",
37-
"zenstack": "1.0.0-alpha.101"
37+
"zenstack": "^1.0.0-alpha.116"
3838
},
3939
"type": "module",
4040
"dependencies": {
4141
"@prisma/client": "^4.8.1",
4242
"@steeze-ui/heroicons": "^2.2.2",
4343
"@steeze-ui/svelte-icon": "^1.3.2",
44-
"@zenstackhq/runtime": "1.0.0-alpha.101",
44+
"@tanstack/svelte-query": "^4.29.7",
45+
"@zenstackhq/runtime": "^1.0.0-alpha.116",
46+
"@zenstackhq/server": "^1.0.0-alpha.116",
47+
"@zenstackhq/tanstack-query": "^1.0.0-alpha.116",
4548
"bcryptjs": "^2.4.3",
4649
"daisyui": "^2.51.5",
4750
"jsonwebtoken": "^9.0.0",
4851
"moment": "^2.29.4",
4952
"nanoid": "^4.0.2",
53+
"superjson": "^1.12.3",
5054
"zod": "^3.21.4",
5155
"zod-validation-error": "^1.3.0"
52-
},
53-
"prisma": {
54-
"seed": "ts-node prisma/seed.ts"
5556
}
5657
}

prisma/schema.prisma

+10-14
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ model Space {
3131
slug String @unique()
3232
members SpaceUser[]
3333
lists List[]
34+
3435
zenstack_guard Boolean @default(true)
3536
zenstack_transaction String?
37+
3638
@@index([zenstack_transaction])
3739
}
3840

@@ -48,8 +50,10 @@ model SpaceUser {
4850
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
4951
userId String
5052
role SpaceUserRole
53+
5154
zenstack_guard Boolean @default(true)
5255
zenstack_transaction String?
56+
5357
@@index([zenstack_transaction])
5458
@@unique([userId, spaceId])
5559
}
@@ -65,12 +69,13 @@ model User {
6569
/// @length(6, 32)
6670
password String
6771
name String?
68-
posts Post[]
6972
spaces SpaceUser[]
7073
todos Todo[]
7174
lists List[]
75+
7276
zenstack_guard Boolean @default(true)
7377
zenstack_transaction String?
78+
7479
@@index([zenstack_transaction])
7580
}
7681

@@ -91,8 +96,10 @@ model List {
9196
title String
9297
private Boolean @default(false)
9398
todos Todo[]
99+
94100
zenstack_guard Boolean @default(true)
95101
zenstack_transaction String?
102+
96103
@@index([zenstack_transaction])
97104
}
98105

@@ -111,20 +118,9 @@ model Todo {
111118
/// @length(1, 100)
112119
title String
113120
completedAt DateTime?
121+
114122
zenstack_guard Boolean @default(true)
115123
zenstack_transaction String?
116-
@@index([zenstack_transaction])
117-
}
118124
119-
model Post {
120-
id String @id() @default(cuid())
121-
createdAt DateTime @default(now())
122-
updatedAt DateTime @updatedAt()
123-
title String
124-
content String?
125-
published Boolean @default(false)
126-
viewCount Int @default(0)
127-
author User? @relation(fields: [authorId], references: [id])
128-
authorId String?
129-
zenstack_guard Boolean @default(true)
125+
@@index([zenstack_transaction])
130126
}

prisma/seed.ts

-67
This file was deleted.

schema.zmodel

+6-13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ enum SpaceUserRole {
1515
ADMIN
1616
}
1717

18+
plugin tanstack {
19+
provider = '@zenstackhq/tanstack-query'
20+
output = 'src/lib/hooks'
21+
framework = 'svelte'
22+
}
23+
1824
/*
1925
* Model for a space in which users can collaborate on Lists and Todos
2026
*/
@@ -72,7 +78,6 @@ model User {
7278
email String @unique @email
7379
password String @password @omit @length(6, 32)
7480
name String?
75-
posts Post[]
7681
spaces SpaceUser[]
7782
todos Todo[]
7883
lists List[]
@@ -136,15 +141,3 @@ model Todo {
136141
// update cannot change owner
137142
@@deny('update', future().owner != owner)
138143
}
139-
140-
model Post {
141-
id String @id @default(cuid())
142-
createdAt DateTime @default(now())
143-
updatedAt DateTime @updatedAt
144-
title String
145-
content String?
146-
published Boolean @default(false)
147-
viewCount Int @default(0)
148-
author User? @relation(fields: [authorId], references: [id])
149-
authorId String?
150-
}

src/hooks.server.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { env } from '$env/dynamic/private';
22
import { JWT_TOKEN_COOKIE_NAME } from '$lib/constant';
33
import prisma, { getEnhancedPrisma } from '$lib/prisma';
44
import type { Handle } from '@sveltejs/kit';
5+
import { sequence } from '@sveltejs/kit/hooks';
6+
import zenstack from '@zenstackhq/server/sveltekit';
57
import jwt from 'jsonwebtoken';
68

7-
export const handle = (async ({ event, resolve }) => {
9+
const auth = (async ({ event, resolve }) => {
810
const token = event.cookies.get(JWT_TOKEN_COOKIE_NAME);
911
if (token) {
1012
try {
@@ -29,3 +31,10 @@ export const handle = (async ({ event, resolve }) => {
2931

3032
return resolve(event);
3133
}) satisfies Handle;
34+
35+
const crud = zenstack.ZenStackSvelteKitHandler({
36+
prefix: '/api/model',
37+
getPrisma: (event) => event.locals.db,
38+
});
39+
40+
export const handle = sequence(auth, crud);

src/lib/components/Avatar.svelte

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script lang="ts">
22
import type { User } from '@prisma/client';
3-
export let user: User;
3+
import { getContext } from 'svelte';
4+
const user = getContext<User>('currentUser');
45
export let size: number = 32;
56
</script>
67

src/lib/components/CreateListDialog.svelte

+22-16
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,43 @@
11
<script lang="ts">
22
import { invalidateAll } from '$app/navigation';
3+
import { useCreateList } from '$lib/hooks';
34
import type { Space } from '@prisma/client';
5+
import { getContext } from 'svelte';
46
57
let modalOpen = false;
68
let titleRef: HTMLElement;
79
let title = '';
810
let _private = false;
911
12+
const user = getContext<{ id: string }>('currentUser');
13+
const createList = useCreateList();
14+
1015
export let space: Space;
1116
1217
$: if (modalOpen) {
1318
setTimeout(() => titleRef.focus(), 100);
1419
}
1520
21+
$: if ($createList.isSuccess) {
22+
invalidateAll();
23+
modalOpen = false;
24+
title = '';
25+
_private = false;
26+
}
27+
28+
$: if ($createList.error) {
29+
alert(($createList.error as any).message);
30+
}
31+
1632
async function onCreate() {
17-
const r = await fetch('/api/list', {
18-
method: 'POST',
19-
headers: {
20-
'Content-Type': 'application/json',
21-
},
22-
body: JSON.stringify({
33+
$createList.mutate({
34+
data: {
2335
spaceId: space.id,
2436
title,
2537
private: _private,
26-
}),
38+
ownerId: user.id,
39+
},
2740
});
28-
if (r.status !== 200) {
29-
const { message } = await r.json();
30-
alert(message);
31-
} else {
32-
await invalidateAll();
33-
modalOpen = false;
34-
title = '';
35-
_private = false;
36-
}
3741
}
3842
</script>
3943

@@ -78,6 +82,8 @@
7882
<div class="modal-action">
7983
<input
8084
class="btn btn-primary"
85+
class:loading={$createList.isLoading}
86+
class:btn-disabled={$createList.isLoading}
8187
type="submit"
8288
value="Create"
8389
/>

0 commit comments

Comments
 (0)