Skip to content

Commit c9bb046

Browse files
committed
update to latest zenstack
1 parent 2888ff6 commit c9bb046

File tree

14 files changed

+438
-431
lines changed

14 files changed

+438
-431
lines changed

package-lock.json

+280-221
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+12-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
"build": "vite build",
99
"preview": "vite preview",
1010
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
11-
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch"
11+
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
12+
"package-clean": "npm rm zenstack @zenstackhq/runtime @zenstackhq/server @zenstackhq/tanstack-query",
13+
"up": "npm install -D --save-exact zenstack@latest @zenstackhq/tanstack-query@latest && npm install --save-exact @zenstackhq/runtime@latest @zenstackhq/server@latest",
14+
"up-preview": "npm install --registry https://packagecloud.io/ymc9/zenstack-preview/npm -D --save-exact zenstack@latest @zenstackhq/tanstack-query@latest && npm install --registry https://packagecloud.io/ymc9/zenstack-preview/npm --save-exact @zenstackhq/runtime@latest @zenstackhq/server@latest"
1215
},
1316
"devDependencies": {
1417
"@sveltejs/adapter-auto": "^2.0.0",
@@ -19,32 +22,32 @@
1922
"@types/jsonwebtoken": "^9.0.1",
2023
"@typescript-eslint/eslint-plugin": "^5.45.0",
2124
"@typescript-eslint/parser": "^5.45.0",
25+
"@zenstackhq/tanstack-query": "1.0.0-alpha.119",
2226
"autoprefixer": "^10.4.14",
2327
"eslint": "^8.28.0",
2428
"eslint-config-prettier": "^8.5.0",
2529
"eslint-plugin-svelte3": "^4.0.0",
26-
"postcss": "^8.4.21",
30+
"postcss": "^8.4.23",
2731
"prettier": "^2.8.0",
2832
"prettier-plugin-svelte": "^2.8.1",
29-
"prisma": "^4.8.1",
33+
"prisma": "^4.14.1",
3034
"svelte": "^3.54.0",
3135
"svelte-check": "^2.9.2",
32-
"tailwindcss": "^3.3.1",
36+
"tailwindcss": "^3.3.2",
3337
"ts-node": "10.9.1",
3438
"tslib": "^2.4.1",
3539
"typescript": "^4.9.3",
3640
"vite": "^4.0.0",
37-
"zenstack": "^1.0.0-alpha.116"
41+
"zenstack": "1.0.0-alpha.119"
3842
},
3943
"type": "module",
4044
"dependencies": {
41-
"@prisma/client": "^4.8.1",
45+
"@prisma/client": "^4.14.1",
4246
"@steeze-ui/heroicons": "^2.2.2",
4347
"@steeze-ui/svelte-icon": "^1.3.2",
4448
"@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",
49+
"@zenstackhq/runtime": "1.0.0-alpha.119",
50+
"@zenstackhq/server": "1.0.0-alpha.119",
4851
"bcryptjs": "^2.4.3",
4952
"daisyui": "^2.51.5",
5053
"jsonwebtoken": "^9.0.0",

schema.zmodel

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ enum SpaceUserRole {
1818
plugin tanstack {
1919
provider = '@zenstackhq/tanstack-query'
2020
output = 'src/lib/hooks'
21-
framework = 'svelte'
21+
target = 'svelte'
2222
}
2323

2424
/*

src/app.d.ts

-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@ import type { PrismaClient, User } from '@prisma/client';
55

66
declare global {
77
namespace App {
8-
// interface Error {}
98
interface Locals {
109
user?: User;
1110
db: PrismaClient;
1211
}
13-
// interface PageData {}
14-
// interface Platform {}
1512
}
1613
}
1714

src/hooks.server.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const auth = (async ({ event, resolve }) => {
3232
return resolve(event);
3333
}) satisfies Handle;
3434

35-
const crud = zenstack.ZenStackSvelteKitHandler({
35+
const crud = zenstack.SvelteKitHandler({
3636
prefix: '/api/model',
3737
getPrisma: (event) => event.locals.db,
3838
});

src/lib/hooks/_helper.ts

+46-64
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,21 @@
1-
/* eslint-disable */
1+
/**
2+
* The default query endpoint.
3+
*/
4+
export const DEFAULT_QUERY_ENDPOINT = '/api/model';
25

3-
import {
4-
createMutation,
5-
createQuery,
6-
useQueryClient,
7-
type MutateFunction,
8-
type MutationOptions,
9-
type QueryClient,
10-
type QueryOptions,
11-
} from '@tanstack/svelte-query';
12-
import Decimal from 'decimal.js';
13-
import superjson from 'superjson';
6+
/**
7+
* Prefix for react-query keys.
8+
*/
9+
export const QUERY_KEY_PREFIX = 'zenstack:';
1410

1511
/**
16-
* Context type for configuring react hooks.
12+
* Context type for configuring the hooks.
1713
*/
1814
export type RequestHandlerContext = {
1915
endpoint: string;
2016
};
2117

22-
export const SvelteQueryContextKey = 'zenstack-svelte-query-context';
23-
24-
export const QUERY_KEY_PREFIX = 'zenstack:';
25-
26-
export function makeUrl(url: string, args: unknown) {
27-
return args ? url + `?q=${encodeURIComponent(marshal(args))}` : url;
28-
}
29-
30-
export async function fetcher<R>(url: string, options?: RequestInit) {
18+
async function fetcher<R>(url: string, options?: RequestInit) {
3119
const res = await fetch(url, options);
3220
if (!res.ok) {
3321
const error: Error & { info?: unknown; status?: number } = new Error(
@@ -47,49 +35,30 @@ export async function fetcher<R>(url: string, options?: RequestInit) {
4735
}
4836
}
4937

50-
export function registerSerializers() {
51-
if (typeof Buffer !== 'undefined') {
52-
superjson.registerCustom<Buffer, string>(
53-
{
54-
isApplicable: (v): v is Buffer => Buffer.isBuffer(v),
55-
serialize: (v) => JSON.stringify(v.toJSON().data),
56-
deserialize: (v) => Buffer.from(JSON.parse(v)),
57-
},
58-
'Buffer',
59-
);
60-
}
61-
62-
if (typeof Decimal !== 'undefined') {
63-
superjson.registerCustom<Decimal, string>(
64-
{
65-
isApplicable: (v): v is Decimal => Decimal.isDecimal(v),
66-
serialize: (v) => v.toJSON(),
67-
deserialize: (v) => new Decimal(v),
68-
},
69-
'decimal.js',
70-
);
71-
}
72-
}
73-
74-
export function marshal(value: unknown) {
75-
return superjson.stringify(value);
76-
}
38+
/* eslint-disable */
7739

78-
export function unmarshal(value: string) {
79-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
80-
return superjson.parse<any>(value);
81-
}
40+
import {
41+
createMutation,
42+
createQuery,
43+
useQueryClient,
44+
type MutateFunction,
45+
type MutationOptions,
46+
type QueryClient,
47+
type QueryOptions,
48+
} from '@tanstack/svelte-query';
8249

83-
// register superjson custom serializers
84-
registerSerializers();
50+
/**
51+
* Key for setting and getting the global query context.
52+
*/
53+
export const SvelteQueryContextKey = 'zenstack-svelte-query-context';
8554

8655
/**
87-
* Creates a react-query query.
56+
* Creates a svelte-query query.
8857
*
8958
* @param model The name of the model under query.
9059
* @param url The request URL.
91-
* @param args The request args object, which will be superjson-stringified and appended as "?q=" parameter
92-
* @param options The react-query options object
60+
* @param args The request args object, URL-encoded and appended as "?q=" parameter
61+
* @param options The svelte-query options object
9362
* @returns useQuery hook
9463
*/
9564
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -103,11 +72,11 @@ export function query<R>(model: string, url: string, args?: unknown, options?: Q
10372
}
10473

10574
/**
106-
* Creates a POST mutation with react-query.
75+
* Creates a POST mutation with svelte-query.
10776
*
10877
* @param model The name of the model under mutation.
10978
* @param url The request URL.
110-
* @param options The react-query options.
79+
* @param options The svelte-query options.
11180
* @param invalidateQueries Whether to invalidate queries after mutation.
11281
* @returns useMutation hooks
11382
*/
@@ -133,11 +102,11 @@ export function postMutation<T, R = any>(
133102
}
134103

135104
/**
136-
* Creates a PUT mutation with react-query.
105+
* Creates a PUT mutation with svelte-query.
137106
*
138107
* @param model The name of the model under mutation.
139108
* @param url The request URL.
140-
* @param options The react-query options.
109+
* @param options The svelte-query options.
141110
* @param invalidateQueries Whether to invalidate queries after mutation.
142111
* @returns useMutation hooks
143112
*/
@@ -163,11 +132,11 @@ export function putMutation<T, R = any>(
163132
}
164133

165134
/**
166-
* Creates a DELETE mutation with react-query.
135+
* Creates a DELETE mutation with svelte-query.
167136
*
168137
* @param model The name of the model under mutation.
169138
* @param url The request URL.
170-
* @param options The react-query options.
139+
* @param options The svelte-query options.
171140
* @param invalidateQueries Whether to invalidate queries after mutation.
172141
* @returns useMutation hooks
173142
*/
@@ -206,3 +175,16 @@ function mergeOptions<T, R = any>(
206175
}
207176
return result;
208177
}
178+
179+
function marshal(value: unknown) {
180+
return JSON.stringify(value);
181+
}
182+
183+
function unmarshal(value: string) {
184+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
185+
return JSON.parse(value) as any;
186+
}
187+
188+
function makeUrl(url: string, args: unknown) {
189+
return args ? url + `?q=${encodeURIComponent(JSON.stringify(args))}` : url;
190+
}

src/lib/hooks/list.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import type { Prisma, List } from '@prisma/client';
33
import { getContext } from 'svelte';
44
import { derived } from 'svelte/store';
55
import type { MutationOptions, QueryOptions } from '@tanstack/svelte-query';
6-
import type { RequestHandlerContext } from '@zenstackhq/tanstack-query/runtime';
7-
import { query, postMutation, putMutation, deleteMutation, SvelteQueryContextKey } from './_helper';
6+
import { SvelteQueryContextKey, type RequestHandlerContext } from './_helper';
7+
import { query, postMutation, putMutation, deleteMutation } from './_helper';
88

99
export function useCreateList(
1010
options?: Omit<MutationOptions<List, unknown, Prisma.ListCreateArgs>, 'mutationFn'>,
1111
invalidateQueries: boolean = true,
1212
) {
13-
const endpoint = getContext<RequestHandlerContext>(SvelteQueryContextKey).endpoint;
13+
const { endpoint } = getContext<RequestHandlerContext>(SvelteQueryContextKey);
1414
const _mutation = postMutation<Prisma.ListCreateArgs, List>(
1515
'List',
1616
`${endpoint}/list/create`,
@@ -44,7 +44,7 @@ export function useCreateManyList(
4444
options?: Omit<MutationOptions<Prisma.BatchPayload, unknown, Prisma.ListCreateManyArgs>, 'mutationFn'>,
4545
invalidateQueries: boolean = true,
4646
) {
47-
const endpoint = getContext<RequestHandlerContext>(SvelteQueryContextKey).endpoint;
47+
const { endpoint } = getContext<RequestHandlerContext>(SvelteQueryContextKey);
4848
const _mutation = postMutation<Prisma.ListCreateManyArgs, Prisma.BatchPayload>(
4949
'List',
5050
`${endpoint}/list/createMany`,
@@ -70,31 +70,31 @@ export function useFindManyList<T extends Prisma.ListFindManyArgs>(
7070
args?: Prisma.SelectSubset<T, Prisma.ListFindManyArgs>,
7171
options?: QueryOptions<Array<Prisma.ListGetPayload<T>>>,
7272
) {
73-
const endpoint = getContext<RequestHandlerContext>(SvelteQueryContextKey).endpoint;
73+
const { endpoint } = getContext<RequestHandlerContext>(SvelteQueryContextKey);
7474
return query<Array<Prisma.ListGetPayload<T>>>('List', `${endpoint}/list/findMany`, args, options);
7575
}
7676

7777
export function useFindUniqueList<T extends Prisma.ListFindUniqueArgs>(
7878
args: Prisma.SelectSubset<T, Prisma.ListFindUniqueArgs>,
7979
options?: QueryOptions<Prisma.ListGetPayload<T>>,
8080
) {
81-
const endpoint = getContext<RequestHandlerContext>(SvelteQueryContextKey).endpoint;
81+
const { endpoint } = getContext<RequestHandlerContext>(SvelteQueryContextKey);
8282
return query<Prisma.ListGetPayload<T>>('List', `${endpoint}/list/findUnique`, args, options);
8383
}
8484

8585
export function useFindFirstList<T extends Prisma.ListFindFirstArgs>(
8686
args?: Prisma.SelectSubset<T, Prisma.ListFindFirstArgs>,
8787
options?: QueryOptions<Prisma.ListGetPayload<T>>,
8888
) {
89-
const endpoint = getContext<RequestHandlerContext>(SvelteQueryContextKey).endpoint;
89+
const { endpoint } = getContext<RequestHandlerContext>(SvelteQueryContextKey);
9090
return query<Prisma.ListGetPayload<T>>('List', `${endpoint}/list/findFirst`, args, options);
9191
}
9292

9393
export function useUpdateList(
9494
options?: Omit<MutationOptions<List, unknown, Prisma.ListUpdateArgs>, 'mutationFn'>,
9595
invalidateQueries: boolean = true,
9696
) {
97-
const endpoint = getContext<RequestHandlerContext>(SvelteQueryContextKey).endpoint;
97+
const { endpoint } = getContext<RequestHandlerContext>(SvelteQueryContextKey);
9898
const _mutation = putMutation<Prisma.ListUpdateArgs, List>(
9999
'List',
100100
`${endpoint}/list/update`,
@@ -128,7 +128,7 @@ export function useUpdateManyList(
128128
options?: Omit<MutationOptions<Prisma.BatchPayload, unknown, Prisma.ListUpdateManyArgs>, 'mutationFn'>,
129129
invalidateQueries: boolean = true,
130130
) {
131-
const endpoint = getContext<RequestHandlerContext>(SvelteQueryContextKey).endpoint;
131+
const { endpoint } = getContext<RequestHandlerContext>(SvelteQueryContextKey);
132132
const _mutation = putMutation<Prisma.ListUpdateManyArgs, Prisma.BatchPayload>(
133133
'List',
134134
`${endpoint}/list/updateMany`,
@@ -154,7 +154,7 @@ export function useUpsertList(
154154
options?: Omit<MutationOptions<List, unknown, Prisma.ListUpsertArgs>, 'mutationFn'>,
155155
invalidateQueries: boolean = true,
156156
) {
157-
const endpoint = getContext<RequestHandlerContext>(SvelteQueryContextKey).endpoint;
157+
const { endpoint } = getContext<RequestHandlerContext>(SvelteQueryContextKey);
158158
const _mutation = postMutation<Prisma.ListUpsertArgs, List>(
159159
'List',
160160
`${endpoint}/list/upsert`,
@@ -188,7 +188,7 @@ export function useDeleteList(
188188
options?: Omit<MutationOptions<List, unknown, Prisma.ListDeleteArgs>, 'mutationFn'>,
189189
invalidateQueries: boolean = true,
190190
) {
191-
const endpoint = getContext<RequestHandlerContext>(SvelteQueryContextKey).endpoint;
191+
const { endpoint } = getContext<RequestHandlerContext>(SvelteQueryContextKey);
192192
const _mutation = deleteMutation<Prisma.ListDeleteArgs, List>(
193193
'List',
194194
`${endpoint}/list/delete`,
@@ -222,7 +222,7 @@ export function useDeleteManyList(
222222
options?: Omit<MutationOptions<Prisma.BatchPayload, unknown, Prisma.ListDeleteManyArgs>, 'mutationFn'>,
223223
invalidateQueries: boolean = true,
224224
) {
225-
const endpoint = getContext<RequestHandlerContext>(SvelteQueryContextKey).endpoint;
225+
const { endpoint } = getContext<RequestHandlerContext>(SvelteQueryContextKey);
226226
const _mutation = deleteMutation<Prisma.ListDeleteManyArgs, Prisma.BatchPayload>(
227227
'List',
228228
`${endpoint}/list/deleteMany`,
@@ -248,7 +248,7 @@ export function useAggregateList<T extends Prisma.ListAggregateArgs>(
248248
args: Prisma.SelectSubset<T, Prisma.ListAggregateArgs>,
249249
options?: QueryOptions<Prisma.GetListAggregateType<T>>,
250250
) {
251-
const endpoint = getContext<RequestHandlerContext>(SvelteQueryContextKey).endpoint;
251+
const { endpoint } = getContext<RequestHandlerContext>(SvelteQueryContextKey);
252252
return query<Prisma.GetListAggregateType<T>>('List', `${endpoint}/list/aggregate`, args, options);
253253
}
254254

@@ -317,7 +317,7 @@ export function useGroupByList<
317317
: InputErrors
318318
>,
319319
) {
320-
const endpoint = getContext<RequestHandlerContext>(SvelteQueryContextKey).endpoint;
320+
const { endpoint } = getContext<RequestHandlerContext>(SvelteQueryContextKey);
321321
return query<
322322
{} extends InputErrors
323323
? Array<
@@ -343,7 +343,7 @@ export function useCountList<T extends Prisma.ListCountArgs>(
343343
: number
344344
>,
345345
) {
346-
const endpoint = getContext<RequestHandlerContext>(SvelteQueryContextKey).endpoint;
346+
const { endpoint } = getContext<RequestHandlerContext>(SvelteQueryContextKey);
347347
return query<
348348
T extends { select: any }
349349
? T['select'] extends true

0 commit comments

Comments
 (0)