Skip to content

Commit d73c49e

Browse files
authored
fix(orm): typing issue when auth type is a typedef (#444)
* fix(orm): typing issue when auth type is a typedef * update
1 parent fbcc6e4 commit d73c49e

File tree

9 files changed

+204
-24
lines changed

9 files changed

+204
-24
lines changed

packages/orm/src/client/contract.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type Decimal from 'decimal.js';
22
import {
33
type FieldIsArray,
44
type GetModels,
5+
type GetTypeDefs,
56
type IsDelegateModel,
67
type ProcedureDef,
78
type RelationFields,
@@ -32,6 +33,7 @@ import type {
3233
SelectSubset,
3334
SimplifiedModelResult,
3435
Subset,
36+
TypeDefResult,
3537
UpdateArgs,
3638
UpdateManyAndReturnArgs,
3739
UpdateManyArgs,
@@ -854,10 +856,10 @@ type AuthModelType<Schema extends SchemaDef, Model extends GetModels<Schema>> =
854856
};
855857

856858
export type AuthType<Schema extends SchemaDef> =
857-
string extends GetModels<Schema>
858-
? Record<string, unknown>
859-
: Schema['authType'] extends GetModels<Schema>
860-
? AuthModelType<Schema, Schema['authType']>
861-
: never;
859+
Schema['authType'] extends GetModels<Schema>
860+
? AuthModelType<Schema, Schema['authType']>
861+
: Schema['authType'] extends GetTypeDefs<Schema>
862+
? TypeDefResult<Schema, Schema['authType'], true>
863+
: Record<string, unknown>;
862864

863865
//#endregion

packages/orm/src/client/crud-types.ts

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import type {
3838
NullableIf,
3939
Optional,
4040
OrArray,
41+
PartialIf,
4142
Simplify,
4243
ValueOfPotentialTuple,
4344
WrapType,
@@ -246,17 +247,31 @@ export type SimplifiedModelResult<
246247
Array = false,
247248
> = Simplify<ModelResult<Schema, Model, Options, Args, Optional, Array>>;
248249

249-
export type TypeDefResult<Schema extends SchemaDef, TypeDef extends GetTypeDefs<Schema>> = Optional<
250-
{
251-
[Key in GetTypeDefFields<Schema, TypeDef>]: MapTypeDefFieldType<Schema, TypeDef, Key>;
252-
},
253-
// optionality
254-
keyof {
255-
[Key in GetTypeDefFields<Schema, TypeDef> as TypeDefFieldIsOptional<Schema, TypeDef, Key> extends true
256-
? Key
257-
: never]: true;
258-
}
259-
>;
250+
export type TypeDefResult<
251+
Schema extends SchemaDef,
252+
TypeDef extends GetTypeDefs<Schema>,
253+
Partial extends boolean = false,
254+
> = PartialIf<
255+
Optional<
256+
{
257+
[Key in GetTypeDefFields<Schema, TypeDef>]: MapFieldDefType<
258+
Schema,
259+
GetTypeDefField<Schema, TypeDef, Key>,
260+
Partial
261+
>;
262+
},
263+
// optionality
264+
Partial extends true
265+
? never
266+
: keyof {
267+
[Key in GetTypeDefFields<Schema, TypeDef> as TypeDefFieldIsOptional<Schema, TypeDef, Key> extends true
268+
? Key
269+
: never]: true;
270+
}
271+
>,
272+
Partial
273+
> &
274+
Record<string, unknown>;
260275

261276
export type BatchResult = { count: number };
262277

@@ -617,17 +632,15 @@ type MapModelFieldType<
617632
Field extends GetModelFields<Schema, Model>,
618633
> = MapFieldDefType<Schema, GetModelField<Schema, Model, Field>>;
619634

620-
type MapTypeDefFieldType<
635+
type MapFieldDefType<
621636
Schema extends SchemaDef,
622-
TypeDef extends GetTypeDefs<Schema>,
623-
Field extends GetTypeDefFields<Schema, TypeDef>,
624-
> = MapFieldDefType<Schema, GetTypeDefField<Schema, TypeDef, Field>>;
625-
626-
type MapFieldDefType<Schema extends SchemaDef, T extends Pick<FieldDef, 'type' | 'optional' | 'array'>> = WrapType<
637+
T extends Pick<FieldDef, 'type' | 'optional' | 'array'>,
638+
Partial extends boolean = false,
639+
> = WrapType<
627640
T['type'] extends GetEnums<Schema>
628641
? keyof GetEnum<Schema, T['type']>
629642
: T['type'] extends GetTypeDefs<Schema>
630-
? TypeDefResult<Schema, T['type']> & Record<string, unknown>
643+
? TypeDefResult<Schema, T['type'], Partial> & Record<string, unknown>
631644
: MapBaseType<T['type']>,
632645
T['optional'],
633646
T['array']

packages/orm/src/utils/type-utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ import type Decimal from 'decimal.js';
22

33
export type Optional<T extends object, K extends keyof T = keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
44

5+
export type PartialIf<T, Condition extends boolean> = Condition extends true ? Partial<T> : T;
6+
57
export type NullableIf<T, Condition extends boolean> = Condition extends true ? T | null : T;
68

9+
export type ArrayIf<T, Condition extends boolean> = Condition extends true ? T[] : T;
10+
711
export type PartialRecord<K extends string | number | symbol, T> = Partial<Record<K, T>>;
812

913
type _Preserve = Date | Function | Decimal | Uint8Array | JsonObject | JsonValue;

packages/schema/src/schema.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export type SchemaDef = {
1515
typeDefs?: Record<string, TypeDefDef>;
1616
plugins: Record<string, unknown>;
1717
procedures?: Record<string, ProcedureDef>;
18-
authType?: GetModels<SchemaDef>;
18+
authType?: GetModels<SchemaDef> | GetTypeDefs<SchemaDef>;
1919
};
2020

2121
export type ModelDef = {
@@ -241,6 +241,12 @@ export type TypeDefFieldIsOptional<
241241
Field extends GetTypeDefFields<Schema, TypeDef>,
242242
> = GetTypeDefField<Schema, TypeDef, Field>['optional'] extends true ? true : false;
243243

244+
export type TypeDefFieldIsArray<
245+
Schema extends SchemaDef,
246+
TypeDef extends GetTypeDefs<Schema>,
247+
Field extends GetTypeDefFields<Schema, TypeDef>,
248+
> = GetTypeDefField<Schema, TypeDef, Field>['array'] extends true ? true : false;
249+
244250
export type FieldIsRelation<
245251
Schema extends SchemaDef,
246252
Model extends GetModels<Schema>,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { createTestClient } from '@zenstackhq/testtools';
2+
import { describe, it } from 'vitest';
3+
import { schema } from '../schemas/auth-type/schema';
4+
5+
describe('Custom auth typing tests', () => {
6+
it('works with custom auth typing', async () => {
7+
const db = await createTestClient(schema);
8+
db.$setAuth({
9+
id: 1,
10+
role: 'ADMIN',
11+
permissions: [
12+
{
13+
actionCode: 'MANAGE',
14+
},
15+
],
16+
});
17+
});
18+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//////////////////////////////////////////////////////////////////////////////////////////////
2+
// DO NOT MODIFY THIS FILE //
3+
// This file is automatically generated by ZenStack CLI and should not be manually updated. //
4+
//////////////////////////////////////////////////////////////////////////////////////////////
5+
6+
/* eslint-disable */
7+
8+
import { type SchemaType as $Schema } from "./schema";
9+
import type { FindManyArgs as $FindManyArgs, FindUniqueArgs as $FindUniqueArgs, FindFirstArgs as $FindFirstArgs, CreateArgs as $CreateArgs, CreateManyArgs as $CreateManyArgs, CreateManyAndReturnArgs as $CreateManyAndReturnArgs, UpdateArgs as $UpdateArgs, UpdateManyArgs as $UpdateManyArgs, UpdateManyAndReturnArgs as $UpdateManyAndReturnArgs, UpsertArgs as $UpsertArgs, DeleteArgs as $DeleteArgs, DeleteManyArgs as $DeleteManyArgs, CountArgs as $CountArgs, AggregateArgs as $AggregateArgs, GroupByArgs as $GroupByArgs, WhereInput as $WhereInput, SelectInput as $SelectInput, IncludeInput as $IncludeInput, OmitInput as $OmitInput, ClientOptions as $ClientOptions } from "@zenstackhq/orm";
10+
import type { SimplifiedModelResult as $SimplifiedModelResult, SelectIncludeOmit as $SelectIncludeOmit } from "@zenstackhq/orm";
11+
export type FooFindManyArgs = $FindManyArgs<$Schema, "Foo">;
12+
export type FooFindUniqueArgs = $FindUniqueArgs<$Schema, "Foo">;
13+
export type FooFindFirstArgs = $FindFirstArgs<$Schema, "Foo">;
14+
export type FooCreateArgs = $CreateArgs<$Schema, "Foo">;
15+
export type FooCreateManyArgs = $CreateManyArgs<$Schema, "Foo">;
16+
export type FooCreateManyAndReturnArgs = $CreateManyAndReturnArgs<$Schema, "Foo">;
17+
export type FooUpdateArgs = $UpdateArgs<$Schema, "Foo">;
18+
export type FooUpdateManyArgs = $UpdateManyArgs<$Schema, "Foo">;
19+
export type FooUpdateManyAndReturnArgs = $UpdateManyAndReturnArgs<$Schema, "Foo">;
20+
export type FooUpsertArgs = $UpsertArgs<$Schema, "Foo">;
21+
export type FooDeleteArgs = $DeleteArgs<$Schema, "Foo">;
22+
export type FooDeleteManyArgs = $DeleteManyArgs<$Schema, "Foo">;
23+
export type FooCountArgs = $CountArgs<$Schema, "Foo">;
24+
export type FooAggregateArgs = $AggregateArgs<$Schema, "Foo">;
25+
export type FooGroupByArgs = $GroupByArgs<$Schema, "Foo">;
26+
export type FooWhereInput = $WhereInput<$Schema, "Foo">;
27+
export type FooSelect = $SelectInput<$Schema, "Foo">;
28+
export type FooInclude = $IncludeInput<$Schema, "Foo">;
29+
export type FooOmit = $OmitInput<$Schema, "Foo">;
30+
export type FooGetPayload<Args extends $SelectIncludeOmit<$Schema, "Foo", true>, Options extends $ClientOptions<$Schema> = $ClientOptions<$Schema>> = $SimplifiedModelResult<$Schema, "Foo", Options, Args>;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//////////////////////////////////////////////////////////////////////////////////////////////
2+
// DO NOT MODIFY THIS FILE //
3+
// This file is automatically generated by ZenStack CLI and should not be manually updated. //
4+
//////////////////////////////////////////////////////////////////////////////////////////////
5+
6+
/* eslint-disable */
7+
8+
import { type SchemaType as $Schema } from "./schema";
9+
import { type ModelResult as $ModelResult, type TypeDefResult as $TypeDefResult } from "@zenstackhq/orm";
10+
export type Foo = $ModelResult<$Schema, "Foo">;
11+
export type Permission = $TypeDefResult<$Schema, "Permission">;
12+
export type Auth = $TypeDefResult<$Schema, "Auth">;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//////////////////////////////////////////////////////////////////////////////////////////////
2+
// DO NOT MODIFY THIS FILE //
3+
// This file is automatically generated by ZenStack CLI and should not be manually updated. //
4+
//////////////////////////////////////////////////////////////////////////////////////////////
5+
6+
/* eslint-disable */
7+
8+
import { type SchemaDef } from "@zenstackhq/orm/schema";
9+
const _schema = {
10+
provider: {
11+
type: "sqlite"
12+
},
13+
models: {
14+
Foo: {
15+
name: "Foo",
16+
fields: {
17+
id: {
18+
name: "id",
19+
type: "Int",
20+
id: true,
21+
attributes: [{ name: "@id" }]
22+
}
23+
},
24+
idFields: ["id"],
25+
uniqueFields: {
26+
id: { type: "Int" }
27+
}
28+
}
29+
},
30+
typeDefs: {
31+
Permission: {
32+
name: "Permission",
33+
fields: {
34+
actionCode: {
35+
name: "actionCode",
36+
type: "String"
37+
}
38+
}
39+
},
40+
Auth: {
41+
name: "Auth",
42+
fields: {
43+
id: {
44+
name: "id",
45+
type: "Int",
46+
attributes: [{ name: "@id" }]
47+
},
48+
name: {
49+
name: "name",
50+
type: "String"
51+
},
52+
permissions: {
53+
name: "permissions",
54+
type: "Permission",
55+
array: true
56+
},
57+
role: {
58+
name: "role",
59+
type: "String"
60+
}
61+
},
62+
attributes: [
63+
{ name: "@@auth" }
64+
]
65+
}
66+
},
67+
authType: "Auth",
68+
plugins: {}
69+
} as const satisfies SchemaDef;
70+
type Schema = typeof _schema & {
71+
__brand?: "schema";
72+
};
73+
export const schema: Schema = _schema;
74+
export type SchemaType = Schema;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
datasource db {
2+
provider = "sqlite"
3+
url = "file:./dev.db"
4+
}
5+
6+
type Permission {
7+
actionCode String
8+
}
9+
10+
type Auth {
11+
id Int @id
12+
name String
13+
permissions Permission[]
14+
role String
15+
16+
@@auth
17+
}
18+
19+
model Foo {
20+
id Int @id
21+
}

0 commit comments

Comments
 (0)