Skip to content

Commit e7f191c

Browse files
authored
fix(tanstack): delegate hooks typing (#370)
1 parent 5ff17ce commit e7f191c

File tree

11 files changed

+148
-26
lines changed

11 files changed

+148
-26
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -344,28 +344,28 @@ export function useModelQueries<Schema extends SchemaDef, Model extends GetModel
344344
return useInternalMutation(schema, modelName, 'DELETE', 'deleteMany', options);
345345
},
346346

347-
useCount: (options?: any) => {
348-
return useInternalQuery(schema, modelName, 'count', undefined, options);
347+
useCount: (args: any, options?: any) => {
348+
return useInternalQuery(schema, modelName, 'count', args, options);
349349
},
350350

351-
useSuspenseCount: (options?: any) => {
352-
return useInternalSuspenseQuery(schema, modelName, 'count', undefined, options);
351+
useSuspenseCount: (args: any, options?: any) => {
352+
return useInternalSuspenseQuery(schema, modelName, 'count', args, options);
353353
},
354354

355-
useAggregate: (options?: any) => {
356-
return useInternalQuery(schema, modelName, 'aggregate', undefined, options);
355+
useAggregate: (args: any, options?: any) => {
356+
return useInternalQuery(schema, modelName, 'aggregate', args, options);
357357
},
358358

359-
useSuspenseAggregate: (options?: any) => {
360-
return useInternalSuspenseQuery(schema, modelName, 'aggregate', undefined, options);
359+
useSuspenseAggregate: (args: any, options?: any) => {
360+
return useInternalSuspenseQuery(schema, modelName, 'aggregate', args, options);
361361
},
362362

363-
useGroupBy: (options?: any) => {
364-
return useInternalQuery(schema, modelName, 'groupBy', undefined, options);
363+
useGroupBy: (args: any, options?: any) => {
364+
return useInternalQuery(schema, modelName, 'groupBy', args, options);
365365
},
366366

367-
useSuspenseGroupBy: (options?: any) => {
368-
return useInternalSuspenseQuery(schema, modelName, 'groupBy', undefined, options);
367+
useSuspenseGroupBy: (args: any, options?: any) => {
368+
return useInternalSuspenseQuery(schema, modelName, 'groupBy', args, options);
369369
},
370370
} as ModelQueryHooks<Schema, Model>;
371371
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,16 +283,16 @@ export function useModelQueries<Schema extends SchemaDef, Model extends GetModel
283283
return useInternalMutation(schema, modelName, 'DELETE', 'deleteMany', options);
284284
},
285285

286-
useCount: (options?: any) => {
287-
return useInternalQuery(schema, modelName, 'count', undefined, options);
286+
useCount: (args: any, options?: any) => {
287+
return useInternalQuery(schema, modelName, 'count', args, options);
288288
},
289289

290-
useAggregate: (options?: any) => {
291-
return useInternalQuery(schema, modelName, 'aggregate', undefined, options);
290+
useAggregate: (args: any, options?: any) => {
291+
return useInternalQuery(schema, modelName, 'aggregate', args, options);
292292
},
293293

294-
useGroupBy: (options?: any) => {
295-
return useInternalQuery(schema, modelName, 'groupBy', undefined, options);
294+
useGroupBy: (args: any, options?: any) => {
295+
return useInternalQuery(schema, modelName, 'groupBy', args, options);
296296
},
297297
} as ModelQueryHooks<Schema, Model>;
298298
}

packages/clients/tanstack-query/src/utils/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ export const ORMWriteActions = [
2121

2222
export type ORMWriteActionType = (typeof ORMWriteActions)[number];
2323

24+
type HooksOperationsIneligibleForDelegateModels = OperationsIneligibleForDelegateModels extends any
25+
? `use${Capitalize<OperationsIneligibleForDelegateModels>}`
26+
: never;
27+
2428
export type TrimDelegateModelOperations<
2529
Schema extends SchemaDef,
2630
Model extends GetModels<Schema>,
2731
T extends Record<string, unknown>,
28-
> = IsDelegateModel<Schema, Model> extends true ? Omit<T, OperationsIneligibleForDelegateModels> : T;
32+
> = IsDelegateModel<Schema, Model> extends true ? Omit<T, HooksOperationsIneligibleForDelegateModels> : T;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,16 +272,16 @@ export function useModelQueries<Schema extends SchemaDef, Model extends GetModel
272272
return useInternalMutation(schema, modelName, 'DELETE', 'deleteMany', options);
273273
},
274274

275-
useCount: (options?: any) => {
276-
return useInternalQuery(schema, modelName, 'count', undefined, options);
275+
useCount: (args: any, options?: any) => {
276+
return useInternalQuery(schema, modelName, 'count', args, options);
277277
},
278278

279-
useAggregate: (options?: any) => {
280-
return useInternalQuery(schema, modelName, 'aggregate', undefined, options);
279+
useAggregate: (args: any, options?: any) => {
280+
return useInternalQuery(schema, modelName, 'aggregate', args, options);
281281
},
282282

283-
useGroupBy: (options?: any) => {
284-
return useInternalQuery(schema, modelName, 'groupBy', undefined, options);
283+
useGroupBy: (args: any, options?: any) => {
284+
return useInternalQuery(schema, modelName, 'groupBy', args, options);
285285
},
286286
} as ModelQueryHooks<Schema, Model>;
287287
}

packages/clients/tanstack-query/test/react-typing-test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,9 @@ client.user.useDeleteMany().mutate({ where: { email: '[email protected]' } });
101101
function check(_value: unknown) {
102102
// noop
103103
}
104+
105+
// @ts-expect-error delegate model
106+
client.foo.useCreate();
107+
108+
client.foo.useUpdate();
109+
client.bar.useCreate();

packages/clients/tanstack-query/test/schemas/basic/input.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,43 @@ export type CategorySelect = $SelectInput<$Schema, "Category">;
6868
export type CategoryInclude = $IncludeInput<$Schema, "Category">;
6969
export type CategoryOmit = $OmitInput<$Schema, "Category">;
7070
export type CategoryGetPayload<Args extends $SelectIncludeOmit<$Schema, "Category", true>> = $SimplifiedModelResult<$Schema, "Category", Args>;
71+
export type FooFindManyArgs = $FindManyArgs<$Schema, "Foo">;
72+
export type FooFindUniqueArgs = $FindUniqueArgs<$Schema, "Foo">;
73+
export type FooFindFirstArgs = $FindFirstArgs<$Schema, "Foo">;
74+
export type FooCreateArgs = $CreateArgs<$Schema, "Foo">;
75+
export type FooCreateManyArgs = $CreateManyArgs<$Schema, "Foo">;
76+
export type FooCreateManyAndReturnArgs = $CreateManyAndReturnArgs<$Schema, "Foo">;
77+
export type FooUpdateArgs = $UpdateArgs<$Schema, "Foo">;
78+
export type FooUpdateManyArgs = $UpdateManyArgs<$Schema, "Foo">;
79+
export type FooUpdateManyAndReturnArgs = $UpdateManyAndReturnArgs<$Schema, "Foo">;
80+
export type FooUpsertArgs = $UpsertArgs<$Schema, "Foo">;
81+
export type FooDeleteArgs = $DeleteArgs<$Schema, "Foo">;
82+
export type FooDeleteManyArgs = $DeleteManyArgs<$Schema, "Foo">;
83+
export type FooCountArgs = $CountArgs<$Schema, "Foo">;
84+
export type FooAggregateArgs = $AggregateArgs<$Schema, "Foo">;
85+
export type FooGroupByArgs = $GroupByArgs<$Schema, "Foo">;
86+
export type FooWhereInput = $WhereInput<$Schema, "Foo">;
87+
export type FooSelect = $SelectInput<$Schema, "Foo">;
88+
export type FooInclude = $IncludeInput<$Schema, "Foo">;
89+
export type FooOmit = $OmitInput<$Schema, "Foo">;
90+
export type FooGetPayload<Args extends $SelectIncludeOmit<$Schema, "Foo", true>> = $SimplifiedModelResult<$Schema, "Foo", Args>;
91+
export type BarFindManyArgs = $FindManyArgs<$Schema, "Bar">;
92+
export type BarFindUniqueArgs = $FindUniqueArgs<$Schema, "Bar">;
93+
export type BarFindFirstArgs = $FindFirstArgs<$Schema, "Bar">;
94+
export type BarCreateArgs = $CreateArgs<$Schema, "Bar">;
95+
export type BarCreateManyArgs = $CreateManyArgs<$Schema, "Bar">;
96+
export type BarCreateManyAndReturnArgs = $CreateManyAndReturnArgs<$Schema, "Bar">;
97+
export type BarUpdateArgs = $UpdateArgs<$Schema, "Bar">;
98+
export type BarUpdateManyArgs = $UpdateManyArgs<$Schema, "Bar">;
99+
export type BarUpdateManyAndReturnArgs = $UpdateManyAndReturnArgs<$Schema, "Bar">;
100+
export type BarUpsertArgs = $UpsertArgs<$Schema, "Bar">;
101+
export type BarDeleteArgs = $DeleteArgs<$Schema, "Bar">;
102+
export type BarDeleteManyArgs = $DeleteManyArgs<$Schema, "Bar">;
103+
export type BarCountArgs = $CountArgs<$Schema, "Bar">;
104+
export type BarAggregateArgs = $AggregateArgs<$Schema, "Bar">;
105+
export type BarGroupByArgs = $GroupByArgs<$Schema, "Bar">;
106+
export type BarWhereInput = $WhereInput<$Schema, "Bar">;
107+
export type BarSelect = $SelectInput<$Schema, "Bar">;
108+
export type BarInclude = $IncludeInput<$Schema, "Bar">;
109+
export type BarOmit = $OmitInput<$Schema, "Bar">;
110+
export type BarGetPayload<Args extends $SelectIncludeOmit<$Schema, "Bar", true>> = $SimplifiedModelResult<$Schema, "Bar", Args>;

packages/clients/tanstack-query/test/schemas/basic/models.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ import { type ModelResult as $ModelResult } from "@zenstackhq/orm";
1010
export type User = $ModelResult<$Schema, "User">;
1111
export type Post = $ModelResult<$Schema, "Post">;
1212
export type Category = $ModelResult<$Schema, "Category">;
13+
export type Foo = $ModelResult<$Schema, "Foo">;
14+
export type Bar = $ModelResult<$Schema, "Bar">;

packages/clients/tanstack-query/test/schemas/basic/schema-lite.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,54 @@ export const schema = {
116116
id: { type: "String" },
117117
name: { type: "String" }
118118
}
119+
},
120+
Foo: {
121+
name: "Foo",
122+
fields: {
123+
id: {
124+
name: "id",
125+
type: "String",
126+
id: true,
127+
default: ExpressionUtils.call("cuid")
128+
},
129+
type: {
130+
name: "type",
131+
type: "String",
132+
isDiscriminator: true
133+
}
134+
},
135+
idFields: ["id"],
136+
uniqueFields: {
137+
id: { type: "String" }
138+
},
139+
isDelegate: true,
140+
subModels: ["Bar"]
141+
},
142+
Bar: {
143+
name: "Bar",
144+
baseModel: "Foo",
145+
fields: {
146+
id: {
147+
name: "id",
148+
type: "String",
149+
id: true,
150+
default: ExpressionUtils.call("cuid")
151+
},
152+
type: {
153+
name: "type",
154+
type: "String",
155+
originModel: "Foo",
156+
isDiscriminator: true
157+
},
158+
title: {
159+
name: "title",
160+
type: "String"
161+
}
162+
},
163+
idFields: ["id"],
164+
uniqueFields: {
165+
id: { type: "String" }
166+
}
119167
}
120168
},
121169
authType: "User",

packages/clients/tanstack-query/test/schemas/basic/schema.zmodel

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,14 @@ model Category {
2222
id String @id @default(cuid())
2323
name String @unique
2424
posts Post[]
25-
}
25+
}
26+
27+
model Foo {
28+
id String @id @default(cuid())
29+
type String
30+
@@delegate(type)
31+
}
32+
33+
model Bar extends Foo {
34+
title String
35+
}

packages/clients/tanstack-query/test/svelte-typing-test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,9 @@ get(client.user.useDeleteMany()).mutate({ where: { email: '[email protected]' } }
9898
function check(_value: unknown) {
9999
// noop
100100
}
101+
102+
// @ts-expect-error delegate model
103+
client.foo.useCreate();
104+
105+
client.foo.useUpdate();
106+
client.bar.useCreate();

0 commit comments

Comments
 (0)