Skip to content

Commit e497061

Browse files
committed
update
1 parent 7b44731 commit e497061

File tree

2 files changed

+72
-43
lines changed

2 files changed

+72
-43
lines changed

packages/runtime/src/client/crud/validator.ts

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -589,34 +589,7 @@ export class InputValidator<Schema extends SchemaDef> {
589589
for (const field of Object.keys(modelDef.fields)) {
590590
const fieldDef = requireField(this.schema, model, field);
591591
if (fieldDef.relation) {
592-
fields[field] = z
593-
.union([
594-
z.literal(true),
595-
z.strictObject({
596-
...(fieldDef.array || fieldDef.optional
597-
? {
598-
// to-many relations and optional to-one relations are filterable
599-
where: z.lazy(() => this.makeWhereSchema(fieldDef.type, false)).optional(),
600-
}
601-
: {}),
602-
select: z.lazy(() => this.makeSelectSchema(fieldDef.type)).optional(),
603-
include: z.lazy(() => this.makeIncludeSchema(fieldDef.type)).optional(),
604-
omit: z.lazy(() => this.makeOmitSchema(fieldDef.type)).optional(),
605-
...(fieldDef.array
606-
? {
607-
// to-many relations can be ordered, skipped, taken, and cursor-located
608-
orderBy: z
609-
.lazy(() => this.makeOrderBySchema(fieldDef.type, true, false))
610-
.optional(),
611-
skip: this.makeSkipSchema().optional(),
612-
take: this.makeTakeSchema().optional(),
613-
cursor: this.makeCursorSchema(fieldDef.type).optional(),
614-
distinct: this.makeDistinctSchema(fieldDef.type).optional(),
615-
}
616-
: {}),
617-
}),
618-
])
619-
.optional();
592+
fields[field] = this.makeRelationSelectIncludeSchema(fieldDef).optional();
620593
} else {
621594
fields[field] = z.boolean().optional();
622595
}
@@ -653,6 +626,33 @@ export class InputValidator<Schema extends SchemaDef> {
653626
return z.strictObject(fields);
654627
}
655628

629+
private makeRelationSelectIncludeSchema(fieldDef: FieldDef) {
630+
return z.union([
631+
z.boolean(),
632+
z.strictObject({
633+
...(fieldDef.array || fieldDef.optional
634+
? {
635+
// to-many relations and optional to-one relations are filterable
636+
where: z.lazy(() => this.makeWhereSchema(fieldDef.type, false)).optional(),
637+
}
638+
: {}),
639+
select: z.lazy(() => this.makeSelectSchema(fieldDef.type)).optional(),
640+
include: z.lazy(() => this.makeIncludeSchema(fieldDef.type)).optional(),
641+
omit: z.lazy(() => this.makeOmitSchema(fieldDef.type)).optional(),
642+
...(fieldDef.array
643+
? {
644+
// to-many relations can be ordered, skipped, taken, and cursor-located
645+
orderBy: z.lazy(() => this.makeOrderBySchema(fieldDef.type, true, false)).optional(),
646+
skip: this.makeSkipSchema().optional(),
647+
take: this.makeTakeSchema().optional(),
648+
cursor: this.makeCursorSchema(fieldDef.type).optional(),
649+
distinct: this.makeDistinctSchema(fieldDef.type).optional(),
650+
}
651+
: {}),
652+
}),
653+
]);
654+
}
655+
656656
private makeOmitSchema(model: string) {
657657
const modelDef = requireModel(this.schema, model);
658658
const fields: Record<string, ZodType> = {};
@@ -671,21 +671,7 @@ export class InputValidator<Schema extends SchemaDef> {
671671
for (const field of Object.keys(modelDef.fields)) {
672672
const fieldDef = requireField(this.schema, model, field);
673673
if (fieldDef.relation) {
674-
fields[field] = z
675-
.union([
676-
z.literal(true),
677-
z.strictObject({
678-
select: z.lazy(() => this.makeSelectSchema(fieldDef.type)).optional(),
679-
include: z.lazy(() => this.makeIncludeSchema(fieldDef.type)).optional(),
680-
omit: z.lazy(() => this.makeOmitSchema(fieldDef.type)).optional(),
681-
where: z.lazy(() => this.makeWhereSchema(fieldDef.type, false)).optional(),
682-
orderBy: z.lazy(() => this.makeOrderBySchema(fieldDef.type, true, false)).optional(),
683-
skip: this.makeSkipSchema().optional(),
684-
take: this.makeTakeSchema().optional(),
685-
distinct: this.makeDistinctSchema(fieldDef.type).optional(),
686-
}),
687-
])
688-
.optional();
674+
fields[field] = this.makeRelationSelectIncludeSchema(fieldDef).optional();
689675
}
690676
}
691677

packages/runtime/test/client-api/find.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,16 @@ describe.each(createClientSpecs(PG_DB_NAME))('Client find tests for $provider',
647647
).resolves.toMatchObject({
648648
posts: [expect.objectContaining({ title: 'Post1' })],
649649
});
650+
await expect(
651+
client.user.findUnique({
652+
where: { id: user.id },
653+
include: {
654+
posts: { where: { published: true }, select: { title: true }, orderBy: { createdAt: 'desc' } },
655+
},
656+
}),
657+
).resolves.toMatchObject({
658+
posts: [expect.objectContaining({ title: 'Post1' })],
659+
});
650660

651661
await expect(
652662
client.user.findUnique({
@@ -658,6 +668,16 @@ describe.each(createClientSpecs(PG_DB_NAME))('Client find tests for $provider',
658668
).resolves.toMatchObject({
659669
posts: [expect.objectContaining({ title: 'Post2' })],
660670
});
671+
await expect(
672+
client.user.findUnique({
673+
where: { id: user.id },
674+
include: {
675+
posts: { orderBy: { title: 'asc' }, skip: 1, take: 1, distinct: ['title'] },
676+
},
677+
}),
678+
).resolves.toMatchObject({
679+
posts: [expect.objectContaining({ title: 'Post2' })],
680+
});
661681

662682
await expect(
663683
client.post.findFirst({
@@ -666,7 +686,22 @@ describe.each(createClientSpecs(PG_DB_NAME))('Client find tests for $provider',
666686
).resolves.toMatchObject({
667687
author: { email: expect.any(String) },
668688
});
689+
await expect(
690+
client.post.findFirst({
691+
include: { author: { select: { email: true } } },
692+
}),
693+
).resolves.toMatchObject({
694+
author: { email: expect.any(String) },
695+
});
669696

697+
await expect(
698+
client.user.findUnique({
699+
where: { id: user.id },
700+
select: {
701+
profile: { where: { bio: 'My bio' } },
702+
},
703+
}),
704+
).resolves.toMatchObject({ profile: expect.any(Object) });
670705
await expect(
671706
client.user.findUnique({
672707
where: { id: user.id },
@@ -676,6 +711,14 @@ describe.each(createClientSpecs(PG_DB_NAME))('Client find tests for $provider',
676711
}),
677712
).resolves.toMatchObject({ profile: expect.any(Object) });
678713

714+
await expect(
715+
client.user.findUnique({
716+
where: { id: user.id },
717+
select: {
718+
profile: { where: { bio: 'Other bio' } },
719+
},
720+
}),
721+
).resolves.toMatchObject({ profile: null });
679722
await expect(
680723
client.user.findUnique({
681724
where: { id: user.id },

0 commit comments

Comments
 (0)