Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 13 additions & 26 deletions packages/orm/src/client/crud-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,13 @@ export type OmitInput<Schema extends SchemaDef, Model extends GetModels<Schema>>
[Key in NonRelationFields<Schema, Model>]?: boolean;
};

export type SelectIncludeOmit<Schema extends SchemaDef, Model extends GetModels<Schema>, AllowCount extends boolean> = {
select?: SelectInput<Schema, Model, AllowCount, boolean> | null;
export type SelectIncludeOmit<
Schema extends SchemaDef,
Model extends GetModels<Schema>,
AllowCount extends boolean,
AllowRelation extends boolean = true,
> = {
select?: SelectInput<Schema, Model, AllowCount, AllowRelation> | null;
include?: IncludeInput<Schema, Model, AllowCount> | null;
omit?: OmitInput<Schema, Model> | null;
};
Expand Down Expand Up @@ -698,10 +703,7 @@ export type CreateArgs<
> = SimplifyIf<
{
data: CreateInput<Schema, Model>;
select?: SelectInput<Schema, Model>;
include?: IncludeInput<Schema, Model>;
omit?: OmitInput<Schema, Model>;
},
} & SelectIncludeOmit<Schema, Model, true>,
Simplify
>;

Expand All @@ -716,10 +718,7 @@ export type CreateManyAndReturnArgs<
Model extends GetModels<Schema>,
Simplify extends boolean = false,
> = SimplifyIf<
CreateManyInput<Schema, Model> & {
select?: SelectInput<Schema, Model, false, false>;
omit?: OmitInput<Schema, Model>;
},
CreateManyInput<Schema, Model> & Omit<SelectIncludeOmit<Schema, Model, false, false>, 'include'>,
Simplify
>;

Expand Down Expand Up @@ -855,10 +854,7 @@ export type UpdateArgs<
{
data: UpdateInput<Schema, Model>;
where: WhereUniqueInput<Schema, Model>;
select?: SelectInput<Schema, Model>;
include?: IncludeInput<Schema, Model>;
omit?: OmitInput<Schema, Model>;
},
} & SelectIncludeOmit<Schema, Model, true>,
Simplify
>;

Expand All @@ -873,10 +869,7 @@ export type UpdateManyAndReturnArgs<
Model extends GetModels<Schema>,
Simplify extends boolean = false,
> = SimplifyIf<
UpdateManyPayload<Schema, Model> & {
select?: SelectInput<Schema, Model, false, false>;
omit?: OmitInput<Schema, Model>;
},
UpdateManyPayload<Schema, Model> & Omit<SelectIncludeOmit<Schema, Model, false, false>, 'include'>,
Simplify
>;

Expand All @@ -895,10 +888,7 @@ export type UpsertArgs<
create: CreateInput<Schema, Model>;
update: UpdateInput<Schema, Model>;
where: WhereUniqueInput<Schema, Model>;
select?: SelectInput<Schema, Model>;
include?: IncludeInput<Schema, Model>;
omit?: OmitInput<Schema, Model>;
},
} & SelectIncludeOmit<Schema, Model, true>,
Simplify
>;

Expand Down Expand Up @@ -1019,10 +1009,7 @@ export type DeleteArgs<
> = SimplifyIf<
{
where: WhereUniqueInput<Schema, Model>;
select?: SelectInput<Schema, Model>;
include?: IncludeInput<Schema, Model>;
omit?: OmitInput<Schema, Model>;
},
} & SelectIncludeOmit<Schema, Model, true>,
Simplify
>;

Expand Down
16 changes: 10 additions & 6 deletions packages/orm/src/client/crud/operations/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,17 @@ export class CreateOperationHandler<Schema extends SchemaDef> extends BaseOperat
const createResult = await this.createMany(tx, this.model, args, true, undefined, selectedFields);

if (needReadBack) {
return this.read(tx, this.model, {
select: args.select,
omit: args.omit,
where: {
OR: createResult.map((item) => getIdValues(this.schema, this.model, item) as any),
return this.read(
tx,
this.model,
{
select: args.select,
omit: args.omit,
where: {
OR: createResult.map((item) => getIdValues(this.schema, this.model, item) as any),
},
} as any, // TODO: fix type
});
);
} else {
return createResult;
}
Expand Down
16 changes: 10 additions & 6 deletions packages/orm/src/client/crud/operations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,17 @@ export class UpdateOperationHandler<Schema extends SchemaDef> extends BaseOperat
);

if (needReadBack) {
const readBackResult = await this.read(tx, this.model, {
select: args.select,
omit: args.omit,
where: {
OR: updateResult.map((item) => getIdValues(this.schema, this.model, item) as any),
const readBackResult = await this.read(
tx,
this.model,
{
select: args.select,
omit: args.omit,
where: {
OR: updateResult.map((item) => getIdValues(this.schema, this.model, item) as any),
},
} as any, // TODO: fix type
});
);

return { readBackResult, updateResult };
} else {
Expand Down
45 changes: 27 additions & 18 deletions packages/orm/src/client/crud/validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -745,9 +745,18 @@ export class InputValidator<Schema extends SchemaDef> {
where: z.lazy(() => this.makeWhereSchema(fieldDef.type, false)).optional(),
}
: {}),
select: z.lazy(() => this.makeSelectSchema(fieldDef.type)).optional(),
include: z.lazy(() => this.makeIncludeSchema(fieldDef.type)).optional(),
omit: z.lazy(() => this.makeOmitSchema(fieldDef.type)).optional(),
select: z
.lazy(() => this.makeSelectSchema(fieldDef.type))
.optional()
.nullable(),
include: z
.lazy(() => this.makeIncludeSchema(fieldDef.type))
.optional()
.nullable(),
omit: z
.lazy(() => this.makeOmitSchema(fieldDef.type))
.optional()
.nullable(),
...(fieldDef.array
? {
// to-many relations can be ordered, skipped, taken, and cursor-located
Expand Down Expand Up @@ -864,9 +873,9 @@ export class InputValidator<Schema extends SchemaDef> {
const dataSchema = this.makeCreateDataSchema(model, false);
let schema: ZodType = z.strictObject({
data: dataSchema,
select: this.makeSelectSchema(model).optional(),
include: this.makeIncludeSchema(model).optional(),
omit: this.makeOmitSchema(model).optional(),
select: this.makeSelectSchema(model).optional().nullable(),
include: this.makeIncludeSchema(model).optional().nullable(),
omit: this.makeOmitSchema(model).optional().nullable(),
});
schema = this.refineForSelectIncludeMutuallyExclusive(schema);
schema = this.refineForSelectOmitMutuallyExclusive(schema);
Expand All @@ -880,8 +889,8 @@ export class InputValidator<Schema extends SchemaDef> {
private makeCreateManyAndReturnSchema(model: string) {
const base = this.makeCreateManyDataSchema(model, []);
const result = base.extend({
select: this.makeSelectSchema(model).optional(),
omit: this.makeOmitSchema(model).optional(),
select: this.makeSelectSchema(model).optional().nullable(),
omit: this.makeOmitSchema(model).optional().nullable(),
});
return this.refineForSelectOmitMutuallyExclusive(result).optional();
}
Expand Down Expand Up @@ -1142,9 +1151,9 @@ export class InputValidator<Schema extends SchemaDef> {
let schema: ZodType = z.strictObject({
where: this.makeWhereSchema(model, true),
data: this.makeUpdateDataSchema(model),
select: this.makeSelectSchema(model).optional(),
include: this.makeIncludeSchema(model).optional(),
omit: this.makeOmitSchema(model).optional(),
select: this.makeSelectSchema(model).optional().nullable(),
include: this.makeIncludeSchema(model).optional().nullable(),
omit: this.makeOmitSchema(model).optional().nullable(),
});
schema = this.refineForSelectIncludeMutuallyExclusive(schema);
schema = this.refineForSelectOmitMutuallyExclusive(schema);
Expand All @@ -1162,8 +1171,8 @@ export class InputValidator<Schema extends SchemaDef> {
private makeUpdateManyAndReturnSchema(model: string) {
const base = this.makeUpdateManySchema(model);
let schema: ZodType = base.extend({
select: this.makeSelectSchema(model).optional(),
omit: this.makeOmitSchema(model).optional(),
select: this.makeSelectSchema(model).optional().nullable(),
omit: this.makeOmitSchema(model).optional().nullable(),
});
schema = this.refineForSelectOmitMutuallyExclusive(schema);
return schema;
Expand All @@ -1174,9 +1183,9 @@ export class InputValidator<Schema extends SchemaDef> {
where: this.makeWhereSchema(model, true),
create: this.makeCreateDataSchema(model, false),
update: this.makeUpdateDataSchema(model),
select: this.makeSelectSchema(model).optional(),
include: this.makeIncludeSchema(model).optional(),
omit: this.makeOmitSchema(model).optional(),
select: this.makeSelectSchema(model).optional().nullable(),
include: this.makeIncludeSchema(model).optional().nullable(),
omit: this.makeOmitSchema(model).optional().nullable(),
});
schema = this.refineForSelectIncludeMutuallyExclusive(schema);
schema = this.refineForSelectOmitMutuallyExclusive(schema);
Expand Down Expand Up @@ -1291,8 +1300,8 @@ export class InputValidator<Schema extends SchemaDef> {
private makeDeleteSchema(model: GetModels<Schema>) {
let schema: ZodType = z.strictObject({
where: this.makeWhereSchema(model, true),
select: this.makeSelectSchema(model).optional(),
include: this.makeIncludeSchema(model).optional(),
select: this.makeSelectSchema(model).optional().nullable(),
include: this.makeIncludeSchema(model).optional().nullable(),
});
schema = this.refineForSelectIncludeMutuallyExclusive(schema);
schema = this.refineForSelectOmitMutuallyExclusive(schema);
Expand Down