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
23 changes: 13 additions & 10 deletions packages/runtime/src/client/crud/operations/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@ export class FindOperationHandler<Schema extends SchemaDef> extends BaseOperatio
// normalize args to strip `undefined` fields
const normalizedArgs = this.normalizeArgs(args);

const findOne = operation === 'findFirst' || operation === 'findUnique';

// parse args
const parsedArgs = (validateArgs
? this.inputValidator.validateFindArgs(this.model, operation === 'findUnique', normalizedArgs)
: normalizedArgs) as FindArgs<Schema, GetModels<Schema>, true>;
let parsedArgs = validateArgs
? this.inputValidator.validateFindArgs(this.model, normalizedArgs, {
unique: operation === 'findUnique',
findOne,
})
: (normalizedArgs as FindArgs<Schema, GetModels<Schema>, true> | undefined);

if (operation === 'findFirst') {
if (findOne) {
// ensure "limit 1"
parsedArgs = parsedArgs ?? {};
parsedArgs.take = 1;
}

// run query
const result = await this.read(
this.client.$qb,
this.model,
parsedArgs,
);
const result = await this.read(this.client.$qb, this.model, parsedArgs);

const finalResult = operation === 'findMany' ? result : (result[0] ?? null);
const finalResult = findOne ? (result[0] ?? null) : result;
return finalResult;
}
}
21 changes: 11 additions & 10 deletions packages/runtime/src/client/crud/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,11 @@ export class InputValidator<Schema extends SchemaDef> {

constructor(private readonly schema: Schema) {}

validateFindArgs(model: GetModels<Schema>, unique: boolean, args: unknown) {
return this.validate<FindArgs<Schema, GetModels<Schema>, true>, Parameters<typeof this.makeFindSchema>[1]>(
model,
'find',
{ unique },
(model, options) => this.makeFindSchema(model, options),
args,
);
validateFindArgs(model: GetModels<Schema>, args: unknown, options: { unique: boolean; findOne: boolean }) {
return this.validate<
FindArgs<Schema, GetModels<Schema>, true> | undefined,
Parameters<typeof this.makeFindSchema>[1]
>(model, 'find', options, (model, options) => this.makeFindSchema(model, options), args);
}

validateCreateArgs(model: GetModels<Schema>, args: unknown) {
Expand Down Expand Up @@ -196,7 +193,7 @@ export class InputValidator<Schema extends SchemaDef> {

// #region Find

private makeFindSchema(model: string, options: { unique: boolean }) {
private makeFindSchema(model: string, options: { unique: boolean; findOne: boolean }) {
const fields: Record<string, z.ZodSchema> = {};
const where = this.makeWhereSchema(model, options.unique);
if (options.unique) {
Expand All @@ -211,7 +208,11 @@ export class InputValidator<Schema extends SchemaDef> {

if (!options.unique) {
fields['skip'] = this.makeSkipSchema().optional();
fields['take'] = this.makeTakeSchema().optional();
if (options.findOne) {
fields['take'] = z.literal(1).optional();
} else {
fields['take'] = this.makeTakeSchema().optional();
}
fields['orderBy'] = this.orArray(this.makeOrderBySchema(model, true, false), true).optional();
fields['cursor'] = this.makeCursorSchema(model).optional();
fields['distinct'] = this.makeDistinctSchema(model).optional();
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/test/client-api/computed-fields.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ model User {
await expect(
db.user.findFirst({
orderBy: { upperName: 'desc' },
take: -1,
take: 1,
}),
).resolves.toMatchObject({
upperName: 'ALEX',
Expand Down
6 changes: 5 additions & 1 deletion packages/runtime/test/client-api/find.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import type { ClientContract } from '../../src/client';
import { NotFoundError } from '../../src/client/errors';
import { InputValidationError, NotFoundError } from '../../src/client/errors';
import { schema } from '../schemas/basic';
import { createClientSpecs } from './client-specs';
import { createPosts, createUser } from './utils';
Expand Down Expand Up @@ -54,6 +54,10 @@ describe.each(createClientSpecs(PG_DB_NAME))('Client find tests for $provider',
await expect(client.user.findMany({ take: 2 })).resolves.toHaveLength(2);
await expect(client.user.findMany({ take: 4 })).resolves.toHaveLength(3);

// findFirst's take must be 1
await expect(client.user.findFirst({ take: 2 })).rejects.toThrow(InputValidationError);
await expect(client.user.findFirst({ take: 1 })).toResolveTruthy();

// skip
await expect(client.user.findMany({ skip: 1 })).resolves.toHaveLength(2);
await expect(client.user.findMany({ skip: 2 })).resolves.toHaveLength(1);
Expand Down