Skip to content

Commit 633b54f

Browse files
committed
update
1 parent a51b474 commit 633b54f

File tree

2 files changed

+54
-32
lines changed

2 files changed

+54
-32
lines changed

packages/clients/tanstack-query/src/react.ts

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
type UseSuspenseQueryOptions,
1919
type UseSuspenseQueryResult,
2020
} from '@tanstack/react-query';
21+
import { lowerCaseFirst } from '@zenstackhq/common-helpers';
2122
import type {
2223
AggregateArgs,
2324
AggregateResult,
@@ -104,6 +105,10 @@ export type ModelMutationOptions<T, TArgs> = Omit<UseMutationOptions<T, DefaultE
104105

105106
export type ModelMutationResult<T, TArgs> = UseMutationResult<T, DefaultError, TArgs>;
106107

108+
export type SchemaHooks<Schema extends SchemaDef> = {
109+
[Model in GetModels<Schema> as `${Uncapitalize<Model>}`]: ModelQueryHooks<Schema, Model>;
110+
};
111+
107112
export type ModelQueryHooks<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
108113
useFindUnique<T extends FindUniqueArgs<Schema, Model>>(
109114
args: SelectSubset<T, FindUniqueArgs<Schema, Model>>,
@@ -206,106 +211,121 @@ export type ModelQueryHooks<Schema extends SchemaDef, Model extends GetModels<Sc
206211
): UseSuspenseQueryResult<GroupByResult<Schema, Model, T>>;
207212
};
208213

214+
/**
215+
* Gets data query hooks for all models in the schema.
216+
*/
217+
export function useClientQueries<Schema extends SchemaDef>(schema: Schema): SchemaHooks<Schema> {
218+
return Object.keys(schema.models).reduce((acc, model) => {
219+
(acc as any)[lowerCaseFirst(model)] = useModelQueries(schema, model as GetModels<Schema>);
220+
return acc;
221+
}, {} as SchemaHooks<Schema>);
222+
}
223+
224+
/**
225+
* Gets data query hooks for a specific model in the schema.
226+
*/
209227
export function useModelQueries<Schema extends SchemaDef, Model extends GetModels<Schema>>(
210228
schema: Schema,
211229
model: Model,
212230
): ModelQueryHooks<Schema, Model> {
213-
const modelDef = schema.models[model];
231+
const modelDef = Object.values(schema.models).find((m) => m.name.toLowerCase() === model.toLowerCase());
214232
if (!modelDef) {
215-
throw new Error(`Model ${model} not found in schema`);
233+
throw new Error(`Model "${model}" not found in schema`);
216234
}
217235

236+
const modelName = lowerCaseFirst(modelDef.name);
237+
218238
return {
219239
useFindUnique: (args: any, options?: any) => {
220-
return useInternalQuery(schema, model, 'findUnique', args, options);
240+
return useInternalQuery(schema, modelName, 'findUnique', args, options);
221241
},
222242

223243
useSuspenseFindUnique: (args: any, options?: any) => {
224-
return useInternalSuspenseQuery(schema, model, 'findUnique', args, options);
244+
return useInternalSuspenseQuery(schema, modelName, 'findUnique', args, options);
225245
},
226246

227247
useFindFirst: (args: any, options?: any) => {
228-
return useInternalQuery(schema, model, 'findFirst', args, options);
248+
return useInternalQuery(schema, modelName, 'findFirst', args, options);
229249
},
230250

231251
useSuspenseFindFirst: (args: any, options?: any) => {
232-
return useInternalSuspenseQuery(schema, model, 'findFirst', args, options);
252+
return useInternalSuspenseQuery(schema, modelName, 'findFirst', args, options);
233253
},
234254

235255
useFindMany: (args: any, options?: any) => {
236-
return useInternalQuery(schema, model, 'findMany', args, options);
256+
return useInternalQuery(schema, modelName, 'findMany', args, options);
237257
},
238258

239259
useSuspenseFindMany: (args: any, options?: any) => {
240-
return useInternalSuspenseQuery(schema, model, 'findMany', args, options);
260+
return useInternalSuspenseQuery(schema, modelName, 'findMany', args, options);
241261
},
242262

243263
useInfiniteFindMany: (args: any, options?: any) => {
244-
return useInternalInfiniteQuery(schema, model, 'findMany', args, options);
264+
return useInternalInfiniteQuery(schema, modelName, 'findMany', args, options);
245265
},
246266

247267
useSuspenseInfiniteFindMany: (args: any, options?: any) => {
248-
return useInternalSuspenseInfiniteQuery(schema, model, 'findMany', args, options);
268+
return useInternalSuspenseInfiniteQuery(schema, modelName, 'findMany', args, options);
249269
},
250270

251271
useCreate: (options?: any) => {
252-
return useInternalMutation(schema, model, 'POST', 'create', options, true);
272+
return useInternalMutation(schema, modelName, 'POST', 'create', options, true);
253273
},
254274

255275
useCreateMany: (options?: any) => {
256-
return useInternalMutation(schema, model, 'POST', 'createMany', options, false);
276+
return useInternalMutation(schema, modelName, 'POST', 'createMany', options, false);
257277
},
258278

259279
useCreateManyAndReturn: (options?: any) => {
260-
return useInternalMutation(schema, model, 'POST', 'createManyAndReturn', options, true);
280+
return useInternalMutation(schema, modelName, 'POST', 'createManyAndReturn', options, true);
261281
},
262282

263283
useUpdate: (options?: any) => {
264-
return useInternalMutation(schema, model, 'PUT', 'update', options, true);
284+
return useInternalMutation(schema, modelName, 'PUT', 'update', options, true);
265285
},
266286

267287
useUpdateMany: (options?: any) => {
268-
return useInternalMutation(schema, model, 'PUT', 'updateMany', options, false);
288+
return useInternalMutation(schema, modelName, 'PUT', 'updateMany', options, false);
269289
},
270290

271291
useUpdateManyAndReturn: (options?: any) => {
272-
return useInternalMutation(schema, model, 'PUT', 'updateManyAndReturn', options, true);
292+
return useInternalMutation(schema, modelName, 'PUT', 'updateManyAndReturn', options, true);
273293
},
274294

275295
useUpsert: (options?: any) => {
276-
return useInternalMutation(schema, model, 'POST', 'upsert', options, true);
296+
return useInternalMutation(schema, modelName, 'POST', 'upsert', options, true);
277297
},
278298

279299
useDelete: (options?: any) => {
280-
return useInternalMutation(schema, model, 'DELETE', 'delete', options, true);
300+
return useInternalMutation(schema, modelName, 'DELETE', 'delete', options, true);
281301
},
282302

283303
useDeleteMany: (options?: any) => {
284-
return useInternalMutation(schema, model, 'DELETE', 'deleteMany', options, false);
304+
return useInternalMutation(schema, modelName, 'DELETE', 'deleteMany', options, false);
285305
},
286306

287307
useCount: (options?: any) => {
288-
return useInternalQuery(schema, model, 'count', undefined, options);
308+
return useInternalQuery(schema, modelName, 'count', undefined, options);
289309
},
290310

291311
useSuspenseCount: (options?: any) => {
292-
return useInternalSuspenseQuery(schema, model, 'count', undefined, options);
312+
return useInternalSuspenseQuery(schema, modelName, 'count', undefined, options);
293313
},
294314

295315
useAggregate: (options?: any) => {
296-
return useInternalQuery(schema, model, 'aggregate', undefined, options);
316+
return useInternalQuery(schema, modelName, 'aggregate', undefined, options);
297317
},
298318

299319
useSuspenseAggregate: (options?: any) => {
300-
return useInternalSuspenseQuery(schema, model, 'aggregate', undefined, options);
320+
return useInternalSuspenseQuery(schema, modelName, 'aggregate', undefined, options);
301321
},
302322

303323
useGroupBy: (options?: any) => {
304-
return useInternalQuery(schema, model, 'groupBy', undefined, options);
324+
return useInternalQuery(schema, modelName, 'groupBy', undefined, options);
305325
},
306326

307327
useSuspenseGroupBy: (options?: any) => {
308-
return useInternalSuspenseQuery(schema, model, 'groupBy', undefined, options);
328+
return useInternalSuspenseQuery(schema, modelName, 'groupBy', undefined, options);
309329
},
310330
} as ModelQueryHooks<Schema, Model>;
311331
}

samples/next.js/app/page.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
'use client';
22

33
import { schema } from '@/zenstack/schema';
4-
import { useModelQueries } from '@zenstackhq/tanstack-query/react';
4+
import { useClientQueries } from '@zenstackhq/tanstack-query/react';
55
import { LoremIpsum } from 'lorem-ipsum';
66
import Image from 'next/image';
77

88
const lorem = new LoremIpsum({ wordsPerSentence: { max: 6, min: 4 } });
99

1010
export default function Home() {
11-
const userQueries = useModelQueries(schema, 'User');
12-
const postQueries = useModelQueries(schema, 'Post');
13-
const { data: users, isFetched: isUsersFetched } = userQueries.useFindMany();
14-
const { data: posts } = postQueries.useFindMany({ orderBy: { createdAt: 'desc' }, include: { author: true } });
15-
const createPost = postQueries.useCreate();
16-
const deletePost = postQueries.useDelete();
11+
const clientQueries = useClientQueries(schema);
12+
const { data: users, isFetched: isUsersFetched } = clientQueries.user.useFindMany();
13+
const { data: posts } = clientQueries.post.useFindMany({
14+
orderBy: { createdAt: 'desc' },
15+
include: { author: true },
16+
});
17+
const createPost = clientQueries.post.useCreate();
18+
const deletePost = clientQueries.post.useDelete();
1719

1820
const onCreatePost = () => {
1921
if (!users) {

0 commit comments

Comments
 (0)