Skip to content

Commit 8e7fa11

Browse files
authored
fix: crud operation typing (#195)
1 parent 3faa2cf commit 8e7fa11

File tree

5 files changed

+33
-12
lines changed

5 files changed

+33
-12
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type {
77
FieldIsDelegateDiscriminator,
88
FieldIsDelegateRelation,
99
FieldIsRelation,
10-
FieldIsRelationArray,
1110
FieldType,
1211
ForeignKeyFields,
1312
GetEnum,
@@ -218,11 +217,11 @@ export type WhereInput<
218217
: Key]?: Key extends RelationFields<Schema, Model>
219218
? // relation
220219
RelationFilter<Schema, Model, Key>
221-
: // enum
222-
GetModelFieldType<Schema, Model, Key> extends GetEnums<Schema>
223-
? EnumFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>>
224-
: FieldIsArray<Schema, Model, Key> extends true
225-
? ArrayFilter<GetModelFieldType<Schema, Model, Key>>
220+
: FieldIsArray<Schema, Model, Key> extends true
221+
? ArrayFilter<GetModelFieldType<Schema, Model, Key>>
222+
: // enum
223+
GetModelFieldType<Schema, Model, Key> extends GetEnums<Schema>
224+
? EnumFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>>
226225
: // primitive
227226
PrimitiveFilter<
228227
Schema,
@@ -561,9 +560,9 @@ type OptionalFieldsForCreate<Schema extends SchemaDef, Model extends GetModels<S
561560
? Key
562561
: FieldHasDefault<Schema, Model, Key> extends true
563562
? Key
564-
: GetModelField<Schema, Model, Key>['updatedAt'] extends true
563+
: FieldIsArray<Schema, Model, Key> extends true
565564
? Key
566-
: FieldIsRelationArray<Schema, Model, Key> extends true
565+
: GetModelField<Schema, Model, Key>['updatedAt'] extends true
567566
? Key
568567
: never]: GetModelField<Schema, Model, Key>;
569568
};

packages/runtime/test/schemas/typing/models.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ export type Identity = $TypeDefResult<$Schema, "Identity">;
1717
export type IdentityProvider = $TypeDefResult<$Schema, "IdentityProvider">;
1818
export const Role = $schema.enums.Role;
1919
export type Role = (typeof Role)[keyof typeof Role];
20+
export const Status = $schema.enums.Status;
21+
export type Status = (typeof Status)[keyof typeof Status];

packages/runtime/test/schemas/typing/schema.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import { type SchemaDef, type OperandExpression, ExpressionUtils } from "../../../dist/schema";
99
export const schema = {
1010
provider: {
11-
type: "sqlite"
11+
type: "postgresql"
1212
},
1313
models: {
1414
User: {
@@ -49,6 +49,11 @@ export const schema = {
4949
attributes: [{ name: "@default", args: [{ name: "value", value: ExpressionUtils.literal("USER") }] }],
5050
default: "USER"
5151
},
52+
status: {
53+
name: "status",
54+
type: "Status",
55+
array: true
56+
},
5257
posts: {
5358
name: "posts",
5459
type: "Post",
@@ -325,6 +330,11 @@ export const schema = {
325330
Role: {
326331
ADMIN: "ADMIN",
327332
USER: "USER"
333+
},
334+
Status: {
335+
ACTIVE: "ACTIVE",
336+
INACTIVE: "INACTIVE",
337+
BANNED: "BANNED"
328338
}
329339
},
330340
authType: "User",

packages/runtime/test/schemas/typing/schema.zmodel

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
datasource db {
2-
provider = "sqlite"
3-
url = "file:./test.db"
2+
provider = 'postgresql'
3+
url = env('DATABASE_URL')
44
}
55

66
enum Role {
77
ADMIN
88
USER
99
}
1010

11+
enum Status {
12+
ACTIVE
13+
INACTIVE
14+
BANNED
15+
}
16+
1117
type Identity {
1218
providers IdentityProvider[]
1319
}
@@ -24,6 +30,7 @@ model User {
2430
name String
2531
email String @unique
2632
role Role @default(USER)
33+
status Status[]
2734
posts Post[]
2835
profile Profile?
2936
postCount Int @computed

packages/runtime/test/schemas/typing/typecheck.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import SQLite from 'better-sqlite3';
22
import { SqliteDialect } from 'kysely';
33
import { ZenStackClient } from '../../../dist';
4-
import { Role, type Identity, type IdentityProvider } from './models';
4+
import { Role, Status, type Identity, type IdentityProvider } from './models';
55
import { schema } from './schema';
66

77
const client = new ZenStackClient(schema, {
@@ -35,6 +35,9 @@ async function find() {
3535
where: {
3636
name: 'Alex',
3737
role: Role.USER,
38+
status: {
39+
has: Status.ACTIVE,
40+
},
3841
},
3942
});
4043
console.log(user1?.name);

0 commit comments

Comments
 (0)