Skip to content

Commit b9575e0

Browse files
authored
fix: misc code review issues (#202)
* fix: misc code review issues * fix lint
1 parent b9bcbf8 commit b9575e0

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,12 @@ export type WhereInput<
221221
? ArrayFilter<Schema, GetModelFieldType<Schema, Model, Key>>
222222
: // enum
223223
GetModelFieldType<Schema, Model, Key> extends GetEnums<Schema>
224-
? EnumFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>>
224+
? EnumFilter<
225+
Schema,
226+
GetModelFieldType<Schema, Model, Key>,
227+
ModelFieldIsOptional<Schema, Model, Key>,
228+
WithAggregations
229+
>
225230
: // primitive
226231
PrimitiveFilter<
227232
Schema,
@@ -237,14 +242,25 @@ export type WhereInput<
237242
NOT?: OrArray<WhereInput<Schema, Model, ScalarOnly>>;
238243
};
239244

240-
type EnumFilter<Schema extends SchemaDef, T extends GetEnums<Schema>, Nullable extends boolean> =
245+
type EnumFilter<
246+
Schema extends SchemaDef,
247+
T extends GetEnums<Schema>,
248+
Nullable extends boolean,
249+
WithAggregations extends boolean,
250+
> =
241251
| NullableIf<keyof GetEnum<Schema, T>, Nullable>
242-
| {
252+
| ({
243253
equals?: NullableIf<keyof GetEnum<Schema, T>, Nullable>;
244254
in?: (keyof GetEnum<Schema, T>)[];
245255
notIn?: (keyof GetEnum<Schema, T>)[];
246-
not?: EnumFilter<Schema, T, Nullable>;
247-
};
256+
not?: EnumFilter<Schema, T, Nullable, WithAggregations>;
257+
} & (WithAggregations extends true
258+
? {
259+
_count?: NumberFilter<Schema, 'Int', false, false>;
260+
_min?: EnumFilter<Schema, T, false, false>;
261+
_max?: EnumFilter<Schema, T, false, false>;
262+
}
263+
: {}));
248264

249265
type ArrayFilter<Schema extends SchemaDef, T extends string> = {
250266
equals?: MapScalarType<Schema, T>[] | null;

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1256,11 +1256,15 @@ export class InputValidator<Schema extends SchemaDef> {
12561256
private makeGroupBySchema(model: GetModels<Schema>) {
12571257
const modelDef = requireModel(this.schema, model);
12581258
const nonRelationFields = Object.keys(modelDef.fields).filter((field) => !modelDef.fields[field]?.relation);
1259+
const bySchema =
1260+
nonRelationFields.length > 0
1261+
? this.orArray(z.enum(nonRelationFields as [string, ...string[]]), true)
1262+
: z.never();
12591263

12601264
let schema: z.ZodSchema = z.strictObject({
12611265
where: this.makeWhereSchema(model, false).optional(),
12621266
orderBy: this.orArray(this.makeOrderBySchema(model, false, true), true).optional(),
1263-
by: this.orArray(z.enum(nonRelationFields as [string, ...string[]]), true),
1267+
by: bySchema,
12641268
having: this.makeHavingSchema(model).optional(),
12651269
skip: this.makeSkipSchema().optional(),
12661270
take: this.makeTakeSchema().optional(),

0 commit comments

Comments
 (0)