Skip to content

Commit 6b1724c

Browse files
authored
fix(orm): select/include nullability (#403)
* fix(orm): select/include nullability * update * update * update
1 parent f74b6ee commit 6b1724c

File tree

4 files changed

+60
-56
lines changed

4 files changed

+60
-56
lines changed

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

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,13 @@ export type OmitInput<Schema extends SchemaDef, Model extends GetModels<Schema>>
451451
[Key in NonRelationFields<Schema, Model>]?: boolean;
452452
};
453453

454-
export type SelectIncludeOmit<Schema extends SchemaDef, Model extends GetModels<Schema>, AllowCount extends boolean> = {
455-
select?: SelectInput<Schema, Model, AllowCount, boolean> | null;
454+
export type SelectIncludeOmit<
455+
Schema extends SchemaDef,
456+
Model extends GetModels<Schema>,
457+
AllowCount extends boolean,
458+
AllowRelation extends boolean = true,
459+
> = {
460+
select?: SelectInput<Schema, Model, AllowCount, AllowRelation> | null;
456461
include?: IncludeInput<Schema, Model, AllowCount> | null;
457462
omit?: OmitInput<Schema, Model> | null;
458463
};
@@ -698,10 +703,7 @@ export type CreateArgs<
698703
> = SimplifyIf<
699704
{
700705
data: CreateInput<Schema, Model>;
701-
select?: SelectInput<Schema, Model>;
702-
include?: IncludeInput<Schema, Model>;
703-
omit?: OmitInput<Schema, Model>;
704-
},
706+
} & SelectIncludeOmit<Schema, Model, true>,
705707
Simplify
706708
>;
707709

@@ -716,10 +718,7 @@ export type CreateManyAndReturnArgs<
716718
Model extends GetModels<Schema>,
717719
Simplify extends boolean = false,
718720
> = SimplifyIf<
719-
CreateManyInput<Schema, Model> & {
720-
select?: SelectInput<Schema, Model, false, false>;
721-
omit?: OmitInput<Schema, Model>;
722-
},
721+
CreateManyInput<Schema, Model> & Omit<SelectIncludeOmit<Schema, Model, false, false>, 'include'>,
723722
Simplify
724723
>;
725724

@@ -855,10 +854,7 @@ export type UpdateArgs<
855854
{
856855
data: UpdateInput<Schema, Model>;
857856
where: WhereUniqueInput<Schema, Model>;
858-
select?: SelectInput<Schema, Model>;
859-
include?: IncludeInput<Schema, Model>;
860-
omit?: OmitInput<Schema, Model>;
861-
},
857+
} & SelectIncludeOmit<Schema, Model, true>,
862858
Simplify
863859
>;
864860

@@ -873,10 +869,7 @@ export type UpdateManyAndReturnArgs<
873869
Model extends GetModels<Schema>,
874870
Simplify extends boolean = false,
875871
> = SimplifyIf<
876-
UpdateManyPayload<Schema, Model> & {
877-
select?: SelectInput<Schema, Model, false, false>;
878-
omit?: OmitInput<Schema, Model>;
879-
},
872+
UpdateManyPayload<Schema, Model> & Omit<SelectIncludeOmit<Schema, Model, false, false>, 'include'>,
880873
Simplify
881874
>;
882875

@@ -895,10 +888,7 @@ export type UpsertArgs<
895888
create: CreateInput<Schema, Model>;
896889
update: UpdateInput<Schema, Model>;
897890
where: WhereUniqueInput<Schema, Model>;
898-
select?: SelectInput<Schema, Model>;
899-
include?: IncludeInput<Schema, Model>;
900-
omit?: OmitInput<Schema, Model>;
901-
},
891+
} & SelectIncludeOmit<Schema, Model, true>,
902892
Simplify
903893
>;
904894

@@ -1019,10 +1009,7 @@ export type DeleteArgs<
10191009
> = SimplifyIf<
10201010
{
10211011
where: WhereUniqueInput<Schema, Model>;
1022-
select?: SelectInput<Schema, Model>;
1023-
include?: IncludeInput<Schema, Model>;
1024-
omit?: OmitInput<Schema, Model>;
1025-
},
1012+
} & SelectIncludeOmit<Schema, Model, true>,
10261013
Simplify
10271014
>;
10281015

packages/orm/src/client/crud/operations/create.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,17 @@ export class CreateOperationHandler<Schema extends SchemaDef> extends BaseOperat
7878
const createResult = await this.createMany(tx, this.model, args, true, undefined, selectedFields);
7979

8080
if (needReadBack) {
81-
return this.read(tx, this.model, {
82-
select: args.select,
83-
omit: args.omit,
84-
where: {
85-
OR: createResult.map((item) => getIdValues(this.schema, this.model, item) as any),
81+
return this.read(
82+
tx,
83+
this.model,
84+
{
85+
select: args.select,
86+
omit: args.omit,
87+
where: {
88+
OR: createResult.map((item) => getIdValues(this.schema, this.model, item) as any),
89+
},
8690
} as any, // TODO: fix type
87-
});
91+
);
8892
} else {
8993
return createResult;
9094
}

packages/orm/src/client/crud/operations/update.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,17 @@ export class UpdateOperationHandler<Schema extends SchemaDef> extends BaseOperat
104104
);
105105

106106
if (needReadBack) {
107-
const readBackResult = await this.read(tx, this.model, {
108-
select: args.select,
109-
omit: args.omit,
110-
where: {
111-
OR: updateResult.map((item) => getIdValues(this.schema, this.model, item) as any),
107+
const readBackResult = await this.read(
108+
tx,
109+
this.model,
110+
{
111+
select: args.select,
112+
omit: args.omit,
113+
where: {
114+
OR: updateResult.map((item) => getIdValues(this.schema, this.model, item) as any),
115+
},
112116
} as any, // TODO: fix type
113-
});
117+
);
114118

115119
return { readBackResult, updateResult };
116120
} else {

packages/orm/src/client/crud/validator/index.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -745,9 +745,18 @@ export class InputValidator<Schema extends SchemaDef> {
745745
where: z.lazy(() => this.makeWhereSchema(fieldDef.type, false)).optional(),
746746
}
747747
: {}),
748-
select: z.lazy(() => this.makeSelectSchema(fieldDef.type)).optional(),
749-
include: z.lazy(() => this.makeIncludeSchema(fieldDef.type)).optional(),
750-
omit: z.lazy(() => this.makeOmitSchema(fieldDef.type)).optional(),
748+
select: z
749+
.lazy(() => this.makeSelectSchema(fieldDef.type))
750+
.optional()
751+
.nullable(),
752+
include: z
753+
.lazy(() => this.makeIncludeSchema(fieldDef.type))
754+
.optional()
755+
.nullable(),
756+
omit: z
757+
.lazy(() => this.makeOmitSchema(fieldDef.type))
758+
.optional()
759+
.nullable(),
751760
...(fieldDef.array
752761
? {
753762
// to-many relations can be ordered, skipped, taken, and cursor-located
@@ -864,9 +873,9 @@ export class InputValidator<Schema extends SchemaDef> {
864873
const dataSchema = this.makeCreateDataSchema(model, false);
865874
let schema: ZodType = z.strictObject({
866875
data: dataSchema,
867-
select: this.makeSelectSchema(model).optional(),
868-
include: this.makeIncludeSchema(model).optional(),
869-
omit: this.makeOmitSchema(model).optional(),
876+
select: this.makeSelectSchema(model).optional().nullable(),
877+
include: this.makeIncludeSchema(model).optional().nullable(),
878+
omit: this.makeOmitSchema(model).optional().nullable(),
870879
});
871880
schema = this.refineForSelectIncludeMutuallyExclusive(schema);
872881
schema = this.refineForSelectOmitMutuallyExclusive(schema);
@@ -880,8 +889,8 @@ export class InputValidator<Schema extends SchemaDef> {
880889
private makeCreateManyAndReturnSchema(model: string) {
881890
const base = this.makeCreateManyDataSchema(model, []);
882891
const result = base.extend({
883-
select: this.makeSelectSchema(model).optional(),
884-
omit: this.makeOmitSchema(model).optional(),
892+
select: this.makeSelectSchema(model).optional().nullable(),
893+
omit: this.makeOmitSchema(model).optional().nullable(),
885894
});
886895
return this.refineForSelectOmitMutuallyExclusive(result).optional();
887896
}
@@ -1142,9 +1151,9 @@ export class InputValidator<Schema extends SchemaDef> {
11421151
let schema: ZodType = z.strictObject({
11431152
where: this.makeWhereSchema(model, true),
11441153
data: this.makeUpdateDataSchema(model),
1145-
select: this.makeSelectSchema(model).optional(),
1146-
include: this.makeIncludeSchema(model).optional(),
1147-
omit: this.makeOmitSchema(model).optional(),
1154+
select: this.makeSelectSchema(model).optional().nullable(),
1155+
include: this.makeIncludeSchema(model).optional().nullable(),
1156+
omit: this.makeOmitSchema(model).optional().nullable(),
11481157
});
11491158
schema = this.refineForSelectIncludeMutuallyExclusive(schema);
11501159
schema = this.refineForSelectOmitMutuallyExclusive(schema);
@@ -1162,8 +1171,8 @@ export class InputValidator<Schema extends SchemaDef> {
11621171
private makeUpdateManyAndReturnSchema(model: string) {
11631172
const base = this.makeUpdateManySchema(model);
11641173
let schema: ZodType = base.extend({
1165-
select: this.makeSelectSchema(model).optional(),
1166-
omit: this.makeOmitSchema(model).optional(),
1174+
select: this.makeSelectSchema(model).optional().nullable(),
1175+
omit: this.makeOmitSchema(model).optional().nullable(),
11671176
});
11681177
schema = this.refineForSelectOmitMutuallyExclusive(schema);
11691178
return schema;
@@ -1174,9 +1183,9 @@ export class InputValidator<Schema extends SchemaDef> {
11741183
where: this.makeWhereSchema(model, true),
11751184
create: this.makeCreateDataSchema(model, false),
11761185
update: this.makeUpdateDataSchema(model),
1177-
select: this.makeSelectSchema(model).optional(),
1178-
include: this.makeIncludeSchema(model).optional(),
1179-
omit: this.makeOmitSchema(model).optional(),
1186+
select: this.makeSelectSchema(model).optional().nullable(),
1187+
include: this.makeIncludeSchema(model).optional().nullable(),
1188+
omit: this.makeOmitSchema(model).optional().nullable(),
11801189
});
11811190
schema = this.refineForSelectIncludeMutuallyExclusive(schema);
11821191
schema = this.refineForSelectOmitMutuallyExclusive(schema);
@@ -1291,8 +1300,8 @@ export class InputValidator<Schema extends SchemaDef> {
12911300
private makeDeleteSchema(model: GetModels<Schema>) {
12921301
let schema: ZodType = z.strictObject({
12931302
where: this.makeWhereSchema(model, true),
1294-
select: this.makeSelectSchema(model).optional(),
1295-
include: this.makeIncludeSchema(model).optional(),
1303+
select: this.makeSelectSchema(model).optional().nullable(),
1304+
include: this.makeIncludeSchema(model).optional().nullable(),
12961305
});
12971306
schema = this.refineForSelectIncludeMutuallyExclusive(schema);
12981307
schema = this.refineForSelectOmitMutuallyExclusive(schema);

0 commit comments

Comments
 (0)