@zenstackhq/runtime 3.0.0-alpha.19 → 3.0.0-alpha.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -95,8 +95,8 @@ type TypeDefResult<Schema extends SchemaDef, TypeDef extends GetTypeDefs<Schema>
95
95
  type BatchResult = {
96
96
  count: number;
97
97
  };
98
- type WhereInput<Schema extends SchemaDef, Model extends GetModels<Schema>, ScalarOnly extends boolean = false> = {
99
- [Key in GetModelFields<Schema, Model> as ScalarOnly extends true ? Key extends RelationFields<Schema, Model> ? never : Key : Key]?: Key extends RelationFields<Schema, Model> ? RelationFilter<Schema, Model, Key> : GetModelFieldType<Schema, Model, Key> extends GetEnums<Schema> ? EnumFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>> : FieldIsArray<Schema, Model, Key> extends true ? ArrayFilter<GetModelFieldType<Schema, Model, Key>> : PrimitiveFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>>;
98
+ type WhereInput<Schema extends SchemaDef, Model extends GetModels<Schema>, ScalarOnly extends boolean = false, WithAggregations extends boolean = false> = {
99
+ [Key in GetModelFields<Schema, Model> as ScalarOnly extends true ? Key extends RelationFields<Schema, Model> ? never : Key : Key]?: Key extends RelationFields<Schema, Model> ? RelationFilter<Schema, Model, Key> : GetModelFieldType<Schema, Model, Key> extends GetEnums<Schema> ? EnumFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>> : FieldIsArray<Schema, Model, Key> extends true ? ArrayFilter<GetModelFieldType<Schema, Model, Key>> : PrimitiveFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>, WithAggregations>;
100
100
  } & {
101
101
  $expr?: (eb: ExpressionBuilder<ToKyselySchema<Schema>, Model>) => OperandExpression<SqlBool>;
102
102
  } & {
@@ -117,8 +117,8 @@ type ArrayFilter<T extends string> = {
117
117
  hasSome?: MapBaseType$1<T>[];
118
118
  isEmpty?: boolean;
119
119
  };
120
- type PrimitiveFilter<Schema extends SchemaDef, T extends string, Nullable extends boolean> = T extends 'String' ? StringFilter<Schema, Nullable> : T extends 'Int' | 'Float' | 'Decimal' | 'BigInt' ? NumberFilter<Schema, T, Nullable> : T extends 'Boolean' ? BooleanFilter<Nullable> : T extends 'DateTime' ? DateTimeFilter<Schema, Nullable> : T extends 'Bytes' ? BytesFilter<Nullable> : T extends 'Json' ? 'Not implemented yet' : never;
121
- type CommonPrimitiveFilter<Schema extends SchemaDef, DataType, T extends BuiltinType, Nullable extends boolean> = {
120
+ type PrimitiveFilter<Schema extends SchemaDef, T extends string, Nullable extends boolean, WithAggregations extends boolean> = T extends 'String' ? StringFilter<Schema, Nullable, WithAggregations> : T extends 'Int' | 'Float' | 'Decimal' | 'BigInt' ? NumberFilter<Schema, T, Nullable, WithAggregations> : T extends 'Boolean' ? BooleanFilter<Schema, Nullable, WithAggregations> : T extends 'DateTime' ? DateTimeFilter<Schema, Nullable, WithAggregations> : T extends 'Bytes' ? BytesFilter<Schema, Nullable, WithAggregations> : T extends 'Json' ? 'Not implemented yet' : never;
121
+ type CommonPrimitiveFilter<Schema extends SchemaDef, DataType, T extends BuiltinType, Nullable extends boolean, WithAggregations extends boolean> = {
122
122
  equals?: NullableIf<DataType, Nullable>;
123
123
  in?: DataType[];
124
124
  notIn?: DataType[];
@@ -126,27 +126,49 @@ type CommonPrimitiveFilter<Schema extends SchemaDef, DataType, T extends Builtin
126
126
  lte?: DataType;
127
127
  gt?: DataType;
128
128
  gte?: DataType;
129
- not?: PrimitiveFilter<Schema, T, Nullable>;
129
+ not?: PrimitiveFilter<Schema, T, Nullable, WithAggregations>;
130
130
  };
131
- type StringFilter<Schema extends SchemaDef, Nullable extends boolean> = NullableIf<string, Nullable> | (CommonPrimitiveFilter<Schema, string, 'String', Nullable> & {
131
+ type StringFilter<Schema extends SchemaDef, Nullable extends boolean, WithAggregations extends boolean> = NullableIf<string, Nullable> | (CommonPrimitiveFilter<Schema, string, 'String', Nullable, WithAggregations> & {
132
132
  contains?: string;
133
133
  startsWith?: string;
134
134
  endsWith?: string;
135
- } & (ProviderSupportsCaseSensitivity<Schema> extends true ? {
135
+ } & (WithAggregations extends true ? {
136
+ _count?: NumberFilter<Schema, 'Int', false, false>;
137
+ _min?: StringFilter<Schema, false, false>;
138
+ _max?: StringFilter<Schema, false, false>;
139
+ } : {}) & (ProviderSupportsCaseSensitivity<Schema> extends true ? {
136
140
  mode?: 'default' | 'insensitive';
137
141
  } : {}));
138
- type NumberFilter<Schema extends SchemaDef, T extends 'Int' | 'Float' | 'Decimal' | 'BigInt', Nullable extends boolean> = NullableIf<number | bigint, Nullable> | CommonPrimitiveFilter<Schema, number, T, Nullable>;
139
- type DateTimeFilter<Schema extends SchemaDef, Nullable extends boolean> = NullableIf<Date | string, Nullable> | CommonPrimitiveFilter<Schema, Date | string, 'DateTime', Nullable>;
140
- type BytesFilter<Nullable extends boolean> = NullableIf<Uint8Array | Buffer, Nullable> | {
142
+ type NumberFilter<Schema extends SchemaDef, T extends 'Int' | 'Float' | 'Decimal' | 'BigInt', Nullable extends boolean, WithAggregations extends boolean> = NullableIf<number | bigint, Nullable> | (CommonPrimitiveFilter<Schema, number, T, Nullable, WithAggregations> & (WithAggregations extends true ? {
143
+ _count?: NumberFilter<Schema, 'Int', false, false>;
144
+ _avg?: NumberFilter<Schema, T, false, false>;
145
+ _sum?: NumberFilter<Schema, T, false, false>;
146
+ _min?: NumberFilter<Schema, T, false, false>;
147
+ _max?: NumberFilter<Schema, T, false, false>;
148
+ } : {}));
149
+ type DateTimeFilter<Schema extends SchemaDef, Nullable extends boolean, WithAggregations extends boolean> = NullableIf<Date | string, Nullable> | (CommonPrimitiveFilter<Schema, Date | string, 'DateTime', Nullable, WithAggregations> & (WithAggregations extends true ? {
150
+ _count?: NumberFilter<Schema, 'Int', false, false>;
151
+ _min?: DateTimeFilter<Schema, false, false>;
152
+ _max?: DateTimeFilter<Schema, false, false>;
153
+ } : {}));
154
+ type BytesFilter<Schema extends SchemaDef, Nullable extends boolean, WithAggregations extends boolean> = NullableIf<Uint8Array | Buffer, Nullable> | ({
141
155
  equals?: NullableIf<Uint8Array, Nullable>;
142
156
  in?: Uint8Array[];
143
157
  notIn?: Uint8Array[];
144
- not?: BytesFilter<Nullable>;
145
- };
146
- type BooleanFilter<Nullable extends boolean> = NullableIf<boolean, Nullable> | {
158
+ not?: BytesFilter<Schema, Nullable, WithAggregations>;
159
+ } & (WithAggregations extends true ? {
160
+ _count?: NumberFilter<Schema, 'Int', false, false>;
161
+ _min?: BytesFilter<Schema, false, false>;
162
+ _max?: BytesFilter<Schema, false, false>;
163
+ } : {}));
164
+ type BooleanFilter<Schema extends SchemaDef, Nullable extends boolean, WithAggregations extends boolean> = NullableIf<boolean, Nullable> | ({
147
165
  equals?: NullableIf<boolean, Nullable>;
148
- not?: BooleanFilter<Nullable>;
149
- };
166
+ not?: BooleanFilter<Schema, Nullable, WithAggregations>;
167
+ } & (WithAggregations extends true ? {
168
+ _count?: NumberFilter<Schema, 'Int', false, false>;
169
+ _min?: BooleanFilter<Schema, false, false>;
170
+ _max?: BooleanFilter<Schema, false, false>;
171
+ } : {}));
150
172
  type SortOrder = 'asc' | 'desc';
151
173
  type NullsOrder = 'first' | 'last';
152
174
  type OrderBy<Schema extends SchemaDef, Model extends GetModels<Schema>, WithRelation extends boolean, WithAggregation extends boolean> = {
@@ -159,12 +181,12 @@ type OrderBy<Schema extends SchemaDef, Model extends GetModels<Schema>, WithRela
159
181
  _count?: SortOrder;
160
182
  } : OrderBy<Schema, RelationFieldType<Schema, Model, Key>, WithRelation, WithAggregation>;
161
183
  } : {}) & (WithAggregation extends true ? {
162
- _count?: OrderBy<Schema, Model, WithRelation, false>;
184
+ _count?: OrderBy<Schema, Model, false, false>;
185
+ _min?: MinMaxInput<Schema, Model, SortOrder>;
186
+ _max?: MinMaxInput<Schema, Model, SortOrder>;
163
187
  } & (NumericFields<Schema, Model> extends never ? {} : {
164
- _avg?: SumAvgInput<Schema, Model>;
165
- _sum?: SumAvgInput<Schema, Model>;
166
- _min?: MinMaxInput<Schema, Model>;
167
- _max?: MinMaxInput<Schema, Model>;
188
+ _avg?: SumAvgInput<Schema, Model, SortOrder>;
189
+ _sum?: SumAvgInput<Schema, Model, SortOrder>;
168
190
  }) : {});
169
191
  type WhereUniqueInput<Schema extends SchemaDef, Model extends GetModels<Schema>> = AtLeast<{
170
192
  [Key in keyof GetModel<Schema, Model>['uniqueFields']]?: GetModel<Schema, Model>['uniqueFields'][Key] extends Pick<FieldDef, 'type'> ? MapFieldDefType<Schema, GetModel<Schema, Model>['uniqueFields'][Key]> : {
@@ -387,20 +409,20 @@ type AggregateArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> =
387
409
  orderBy?: OrArray<OrderBy<Schema, Model, true, false>>;
388
410
  } & {
389
411
  _count?: true | CountAggregateInput<Schema, Model>;
412
+ _min?: MinMaxInput<Schema, Model, true>;
413
+ _max?: MinMaxInput<Schema, Model, true>;
390
414
  } & (NumericFields<Schema, Model> extends never ? {} : {
391
- _avg?: SumAvgInput<Schema, Model>;
392
- _sum?: SumAvgInput<Schema, Model>;
393
- _min?: MinMaxInput<Schema, Model>;
394
- _max?: MinMaxInput<Schema, Model>;
415
+ _avg?: SumAvgInput<Schema, Model, true>;
416
+ _sum?: SumAvgInput<Schema, Model, true>;
395
417
  });
396
418
  type NumericFields<Schema extends SchemaDef, Model extends GetModels<Schema>> = keyof {
397
419
  [Key in GetModelFields<Schema, Model> as GetModelFieldType<Schema, Model, Key> extends 'Int' | 'Float' | 'BigInt' | 'Decimal' ? FieldIsArray<Schema, Model, Key> extends true ? never : Key : never]: GetModelField<Schema, Model, Key>;
398
420
  };
399
- type SumAvgInput<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
400
- [Key in NumericFields<Schema, Model>]?: true;
421
+ type SumAvgInput<Schema extends SchemaDef, Model extends GetModels<Schema>, ValueType> = {
422
+ [Key in NumericFields<Schema, Model>]?: ValueType;
401
423
  };
402
- type MinMaxInput<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
403
- [Key in GetModelFields<Schema, Model> as FieldIsArray<Schema, Model, Key> extends true ? never : FieldIsRelation<Schema, Model, Key> extends true ? never : Key]?: true;
424
+ type MinMaxInput<Schema extends SchemaDef, Model extends GetModels<Schema>, ValueType> = {
425
+ [Key in GetModelFields<Schema, Model> as FieldIsArray<Schema, Model, Key> extends true ? never : FieldIsRelation<Schema, Model, Key> extends true ? never : Key]?: ValueType;
404
426
  };
405
427
  type AggregateResult<Schema extends SchemaDef, Model extends GetModels<Schema>, Args extends AggregateArgs<Schema, Model>> = (Args extends {
406
428
  _count: infer Count;
@@ -426,19 +448,20 @@ type AggregateResult<Schema extends SchemaDef, Model extends GetModels<Schema>,
426
448
  type AggCommonOutput<Input> = Input extends true ? number : Input extends {} ? {
427
449
  [Key in keyof Input]: number;
428
450
  } : never;
451
+ type GroupByHaving<Schema extends SchemaDef, Model extends GetModels<Schema>> = Omit<WhereInput<Schema, Model, true, true>, '$expr'>;
429
452
  type GroupByArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
430
453
  where?: WhereInput<Schema, Model>;
431
454
  orderBy?: OrArray<OrderBy<Schema, Model, false, true>>;
432
455
  by: NonRelationFields<Schema, Model> | NonEmptyArray<NonRelationFields<Schema, Model>>;
433
- having?: WhereInput<Schema, Model, true>;
456
+ having?: GroupByHaving<Schema, Model>;
434
457
  take?: number;
435
458
  skip?: number;
436
459
  _count?: true | CountAggregateInput<Schema, Model>;
460
+ _min?: MinMaxInput<Schema, Model, true>;
461
+ _max?: MinMaxInput<Schema, Model, true>;
437
462
  } & (NumericFields<Schema, Model> extends never ? {} : {
438
- _avg?: SumAvgInput<Schema, Model>;
439
- _sum?: SumAvgInput<Schema, Model>;
440
- _min?: MinMaxInput<Schema, Model>;
441
- _max?: MinMaxInput<Schema, Model>;
463
+ _avg?: SumAvgInput<Schema, Model, true>;
464
+ _sum?: SumAvgInput<Schema, Model, true>;
442
465
  });
443
466
  type GroupByResult<Schema extends SchemaDef, Model extends GetModels<Schema>, Args extends GroupByArgs<Schema, Model>> = Array<{
444
467
  [Key in NonRelationFields<Schema, Model> as Key extends ValueOfPotentialTuple<Args['by']> ? Key : never]: MapModelFieldType<Schema, Model, Key>;
@@ -498,6 +521,10 @@ type AuthType<Schema extends SchemaDef> = string extends GetModels<Schema> ? Rec
498
521
  * Client API methods that are not supported in transactions.
499
522
  */
500
523
  declare const TRANSACTION_UNSUPPORTED_METHODS: readonly ["$transaction", "$disconnect", "$use"];
524
+ /**
525
+ * Logical combinators used in filters.
526
+ */
527
+ declare const LOGICAL_COMBINATORS: readonly ["AND", "OR", "NOT"];
501
528
 
502
529
  declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
503
530
  protected readonly schema: Schema;
@@ -506,7 +533,8 @@ declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
506
533
  transformPrimitive(value: unknown, _type: BuiltinType, _forArrayField: boolean): unknown;
507
534
  buildSelectModel(eb: ExpressionBuilder<any, any>, model: string): SelectQueryBuilder<any, any, {}>;
508
535
  buildFilter(eb: ExpressionBuilder<any, any>, model: string, modelAlias: string, where: boolean | object | undefined): Expression<SqlBool>;
509
- protected buildCompositeFilter(eb: ExpressionBuilder<any, any>, model: string, modelAlias: string, key: 'AND' | 'OR' | 'NOT', payload: any): Expression<SqlBool>;
536
+ private isLogicalCombinator;
537
+ protected buildCompositeFilter(eb: ExpressionBuilder<any, any>, model: string, modelAlias: string, key: (typeof LOGICAL_COMBINATORS)[number], payload: any): Expression<SqlBool>;
510
538
  private buildRelationFilter;
511
539
  private buildToOneRelationFilter;
512
540
  private buildToManyRelationFilter;
@@ -1409,17 +1437,18 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1409
1437
  * _count: true
1410
1438
  * }); // result: `Array<{ country: string, city: string, _count: number }>`
1411
1439
  *
1412
- * // group by with sorting, the `orderBy` fields must be in the `by` list
1440
+ * // group by with sorting, the `orderBy` fields must be either an aggregation
1441
+ * // or a field used in the `by` list
1413
1442
  * await db.profile.groupBy({
1414
1443
  * by: 'country',
1415
1444
  * orderBy: { country: 'desc' }
1416
1445
  * });
1417
1446
  *
1418
- * // group by with having (post-aggregation filter), the `having` fields must
1419
- * // be in the `by` list
1447
+ * // group by with having (post-aggregation filter), the fields used in `having` must
1448
+ * // be either an aggregation, or a field used in the `by` list
1420
1449
  * await db.profile.groupBy({
1421
1450
  * by: 'country',
1422
- * having: { country: 'US' }
1451
+ * having: { country: 'US', age: { _avg: { gte: 18 } } }
1423
1452
  * });
1424
1453
  */
1425
1454
  groupBy<T extends GroupByArgs<Schema, Model>>(args: Subset<T, GroupByArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<GroupByResult<Schema, Model, T>>>;
@@ -95,8 +95,8 @@ type TypeDefResult<Schema extends SchemaDef, TypeDef extends GetTypeDefs<Schema>
95
95
  type BatchResult = {
96
96
  count: number;
97
97
  };
98
- type WhereInput<Schema extends SchemaDef, Model extends GetModels<Schema>, ScalarOnly extends boolean = false> = {
99
- [Key in GetModelFields<Schema, Model> as ScalarOnly extends true ? Key extends RelationFields<Schema, Model> ? never : Key : Key]?: Key extends RelationFields<Schema, Model> ? RelationFilter<Schema, Model, Key> : GetModelFieldType<Schema, Model, Key> extends GetEnums<Schema> ? EnumFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>> : FieldIsArray<Schema, Model, Key> extends true ? ArrayFilter<GetModelFieldType<Schema, Model, Key>> : PrimitiveFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>>;
98
+ type WhereInput<Schema extends SchemaDef, Model extends GetModels<Schema>, ScalarOnly extends boolean = false, WithAggregations extends boolean = false> = {
99
+ [Key in GetModelFields<Schema, Model> as ScalarOnly extends true ? Key extends RelationFields<Schema, Model> ? never : Key : Key]?: Key extends RelationFields<Schema, Model> ? RelationFilter<Schema, Model, Key> : GetModelFieldType<Schema, Model, Key> extends GetEnums<Schema> ? EnumFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>> : FieldIsArray<Schema, Model, Key> extends true ? ArrayFilter<GetModelFieldType<Schema, Model, Key>> : PrimitiveFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>, WithAggregations>;
100
100
  } & {
101
101
  $expr?: (eb: ExpressionBuilder<ToKyselySchema<Schema>, Model>) => OperandExpression<SqlBool>;
102
102
  } & {
@@ -117,8 +117,8 @@ type ArrayFilter<T extends string> = {
117
117
  hasSome?: MapBaseType$1<T>[];
118
118
  isEmpty?: boolean;
119
119
  };
120
- type PrimitiveFilter<Schema extends SchemaDef, T extends string, Nullable extends boolean> = T extends 'String' ? StringFilter<Schema, Nullable> : T extends 'Int' | 'Float' | 'Decimal' | 'BigInt' ? NumberFilter<Schema, T, Nullable> : T extends 'Boolean' ? BooleanFilter<Nullable> : T extends 'DateTime' ? DateTimeFilter<Schema, Nullable> : T extends 'Bytes' ? BytesFilter<Nullable> : T extends 'Json' ? 'Not implemented yet' : never;
121
- type CommonPrimitiveFilter<Schema extends SchemaDef, DataType, T extends BuiltinType, Nullable extends boolean> = {
120
+ type PrimitiveFilter<Schema extends SchemaDef, T extends string, Nullable extends boolean, WithAggregations extends boolean> = T extends 'String' ? StringFilter<Schema, Nullable, WithAggregations> : T extends 'Int' | 'Float' | 'Decimal' | 'BigInt' ? NumberFilter<Schema, T, Nullable, WithAggregations> : T extends 'Boolean' ? BooleanFilter<Schema, Nullable, WithAggregations> : T extends 'DateTime' ? DateTimeFilter<Schema, Nullable, WithAggregations> : T extends 'Bytes' ? BytesFilter<Schema, Nullable, WithAggregations> : T extends 'Json' ? 'Not implemented yet' : never;
121
+ type CommonPrimitiveFilter<Schema extends SchemaDef, DataType, T extends BuiltinType, Nullable extends boolean, WithAggregations extends boolean> = {
122
122
  equals?: NullableIf<DataType, Nullable>;
123
123
  in?: DataType[];
124
124
  notIn?: DataType[];
@@ -126,27 +126,49 @@ type CommonPrimitiveFilter<Schema extends SchemaDef, DataType, T extends Builtin
126
126
  lte?: DataType;
127
127
  gt?: DataType;
128
128
  gte?: DataType;
129
- not?: PrimitiveFilter<Schema, T, Nullable>;
129
+ not?: PrimitiveFilter<Schema, T, Nullable, WithAggregations>;
130
130
  };
131
- type StringFilter<Schema extends SchemaDef, Nullable extends boolean> = NullableIf<string, Nullable> | (CommonPrimitiveFilter<Schema, string, 'String', Nullable> & {
131
+ type StringFilter<Schema extends SchemaDef, Nullable extends boolean, WithAggregations extends boolean> = NullableIf<string, Nullable> | (CommonPrimitiveFilter<Schema, string, 'String', Nullable, WithAggregations> & {
132
132
  contains?: string;
133
133
  startsWith?: string;
134
134
  endsWith?: string;
135
- } & (ProviderSupportsCaseSensitivity<Schema> extends true ? {
135
+ } & (WithAggregations extends true ? {
136
+ _count?: NumberFilter<Schema, 'Int', false, false>;
137
+ _min?: StringFilter<Schema, false, false>;
138
+ _max?: StringFilter<Schema, false, false>;
139
+ } : {}) & (ProviderSupportsCaseSensitivity<Schema> extends true ? {
136
140
  mode?: 'default' | 'insensitive';
137
141
  } : {}));
138
- type NumberFilter<Schema extends SchemaDef, T extends 'Int' | 'Float' | 'Decimal' | 'BigInt', Nullable extends boolean> = NullableIf<number | bigint, Nullable> | CommonPrimitiveFilter<Schema, number, T, Nullable>;
139
- type DateTimeFilter<Schema extends SchemaDef, Nullable extends boolean> = NullableIf<Date | string, Nullable> | CommonPrimitiveFilter<Schema, Date | string, 'DateTime', Nullable>;
140
- type BytesFilter<Nullable extends boolean> = NullableIf<Uint8Array | Buffer, Nullable> | {
142
+ type NumberFilter<Schema extends SchemaDef, T extends 'Int' | 'Float' | 'Decimal' | 'BigInt', Nullable extends boolean, WithAggregations extends boolean> = NullableIf<number | bigint, Nullable> | (CommonPrimitiveFilter<Schema, number, T, Nullable, WithAggregations> & (WithAggregations extends true ? {
143
+ _count?: NumberFilter<Schema, 'Int', false, false>;
144
+ _avg?: NumberFilter<Schema, T, false, false>;
145
+ _sum?: NumberFilter<Schema, T, false, false>;
146
+ _min?: NumberFilter<Schema, T, false, false>;
147
+ _max?: NumberFilter<Schema, T, false, false>;
148
+ } : {}));
149
+ type DateTimeFilter<Schema extends SchemaDef, Nullable extends boolean, WithAggregations extends boolean> = NullableIf<Date | string, Nullable> | (CommonPrimitiveFilter<Schema, Date | string, 'DateTime', Nullable, WithAggregations> & (WithAggregations extends true ? {
150
+ _count?: NumberFilter<Schema, 'Int', false, false>;
151
+ _min?: DateTimeFilter<Schema, false, false>;
152
+ _max?: DateTimeFilter<Schema, false, false>;
153
+ } : {}));
154
+ type BytesFilter<Schema extends SchemaDef, Nullable extends boolean, WithAggregations extends boolean> = NullableIf<Uint8Array | Buffer, Nullable> | ({
141
155
  equals?: NullableIf<Uint8Array, Nullable>;
142
156
  in?: Uint8Array[];
143
157
  notIn?: Uint8Array[];
144
- not?: BytesFilter<Nullable>;
145
- };
146
- type BooleanFilter<Nullable extends boolean> = NullableIf<boolean, Nullable> | {
158
+ not?: BytesFilter<Schema, Nullable, WithAggregations>;
159
+ } & (WithAggregations extends true ? {
160
+ _count?: NumberFilter<Schema, 'Int', false, false>;
161
+ _min?: BytesFilter<Schema, false, false>;
162
+ _max?: BytesFilter<Schema, false, false>;
163
+ } : {}));
164
+ type BooleanFilter<Schema extends SchemaDef, Nullable extends boolean, WithAggregations extends boolean> = NullableIf<boolean, Nullable> | ({
147
165
  equals?: NullableIf<boolean, Nullable>;
148
- not?: BooleanFilter<Nullable>;
149
- };
166
+ not?: BooleanFilter<Schema, Nullable, WithAggregations>;
167
+ } & (WithAggregations extends true ? {
168
+ _count?: NumberFilter<Schema, 'Int', false, false>;
169
+ _min?: BooleanFilter<Schema, false, false>;
170
+ _max?: BooleanFilter<Schema, false, false>;
171
+ } : {}));
150
172
  type SortOrder = 'asc' | 'desc';
151
173
  type NullsOrder = 'first' | 'last';
152
174
  type OrderBy<Schema extends SchemaDef, Model extends GetModels<Schema>, WithRelation extends boolean, WithAggregation extends boolean> = {
@@ -159,12 +181,12 @@ type OrderBy<Schema extends SchemaDef, Model extends GetModels<Schema>, WithRela
159
181
  _count?: SortOrder;
160
182
  } : OrderBy<Schema, RelationFieldType<Schema, Model, Key>, WithRelation, WithAggregation>;
161
183
  } : {}) & (WithAggregation extends true ? {
162
- _count?: OrderBy<Schema, Model, WithRelation, false>;
184
+ _count?: OrderBy<Schema, Model, false, false>;
185
+ _min?: MinMaxInput<Schema, Model, SortOrder>;
186
+ _max?: MinMaxInput<Schema, Model, SortOrder>;
163
187
  } & (NumericFields<Schema, Model> extends never ? {} : {
164
- _avg?: SumAvgInput<Schema, Model>;
165
- _sum?: SumAvgInput<Schema, Model>;
166
- _min?: MinMaxInput<Schema, Model>;
167
- _max?: MinMaxInput<Schema, Model>;
188
+ _avg?: SumAvgInput<Schema, Model, SortOrder>;
189
+ _sum?: SumAvgInput<Schema, Model, SortOrder>;
168
190
  }) : {});
169
191
  type WhereUniqueInput<Schema extends SchemaDef, Model extends GetModels<Schema>> = AtLeast<{
170
192
  [Key in keyof GetModel<Schema, Model>['uniqueFields']]?: GetModel<Schema, Model>['uniqueFields'][Key] extends Pick<FieldDef, 'type'> ? MapFieldDefType<Schema, GetModel<Schema, Model>['uniqueFields'][Key]> : {
@@ -387,20 +409,20 @@ type AggregateArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> =
387
409
  orderBy?: OrArray<OrderBy<Schema, Model, true, false>>;
388
410
  } & {
389
411
  _count?: true | CountAggregateInput<Schema, Model>;
412
+ _min?: MinMaxInput<Schema, Model, true>;
413
+ _max?: MinMaxInput<Schema, Model, true>;
390
414
  } & (NumericFields<Schema, Model> extends never ? {} : {
391
- _avg?: SumAvgInput<Schema, Model>;
392
- _sum?: SumAvgInput<Schema, Model>;
393
- _min?: MinMaxInput<Schema, Model>;
394
- _max?: MinMaxInput<Schema, Model>;
415
+ _avg?: SumAvgInput<Schema, Model, true>;
416
+ _sum?: SumAvgInput<Schema, Model, true>;
395
417
  });
396
418
  type NumericFields<Schema extends SchemaDef, Model extends GetModels<Schema>> = keyof {
397
419
  [Key in GetModelFields<Schema, Model> as GetModelFieldType<Schema, Model, Key> extends 'Int' | 'Float' | 'BigInt' | 'Decimal' ? FieldIsArray<Schema, Model, Key> extends true ? never : Key : never]: GetModelField<Schema, Model, Key>;
398
420
  };
399
- type SumAvgInput<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
400
- [Key in NumericFields<Schema, Model>]?: true;
421
+ type SumAvgInput<Schema extends SchemaDef, Model extends GetModels<Schema>, ValueType> = {
422
+ [Key in NumericFields<Schema, Model>]?: ValueType;
401
423
  };
402
- type MinMaxInput<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
403
- [Key in GetModelFields<Schema, Model> as FieldIsArray<Schema, Model, Key> extends true ? never : FieldIsRelation<Schema, Model, Key> extends true ? never : Key]?: true;
424
+ type MinMaxInput<Schema extends SchemaDef, Model extends GetModels<Schema>, ValueType> = {
425
+ [Key in GetModelFields<Schema, Model> as FieldIsArray<Schema, Model, Key> extends true ? never : FieldIsRelation<Schema, Model, Key> extends true ? never : Key]?: ValueType;
404
426
  };
405
427
  type AggregateResult<Schema extends SchemaDef, Model extends GetModels<Schema>, Args extends AggregateArgs<Schema, Model>> = (Args extends {
406
428
  _count: infer Count;
@@ -426,19 +448,20 @@ type AggregateResult<Schema extends SchemaDef, Model extends GetModels<Schema>,
426
448
  type AggCommonOutput<Input> = Input extends true ? number : Input extends {} ? {
427
449
  [Key in keyof Input]: number;
428
450
  } : never;
451
+ type GroupByHaving<Schema extends SchemaDef, Model extends GetModels<Schema>> = Omit<WhereInput<Schema, Model, true, true>, '$expr'>;
429
452
  type GroupByArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
430
453
  where?: WhereInput<Schema, Model>;
431
454
  orderBy?: OrArray<OrderBy<Schema, Model, false, true>>;
432
455
  by: NonRelationFields<Schema, Model> | NonEmptyArray<NonRelationFields<Schema, Model>>;
433
- having?: WhereInput<Schema, Model, true>;
456
+ having?: GroupByHaving<Schema, Model>;
434
457
  take?: number;
435
458
  skip?: number;
436
459
  _count?: true | CountAggregateInput<Schema, Model>;
460
+ _min?: MinMaxInput<Schema, Model, true>;
461
+ _max?: MinMaxInput<Schema, Model, true>;
437
462
  } & (NumericFields<Schema, Model> extends never ? {} : {
438
- _avg?: SumAvgInput<Schema, Model>;
439
- _sum?: SumAvgInput<Schema, Model>;
440
- _min?: MinMaxInput<Schema, Model>;
441
- _max?: MinMaxInput<Schema, Model>;
463
+ _avg?: SumAvgInput<Schema, Model, true>;
464
+ _sum?: SumAvgInput<Schema, Model, true>;
442
465
  });
443
466
  type GroupByResult<Schema extends SchemaDef, Model extends GetModels<Schema>, Args extends GroupByArgs<Schema, Model>> = Array<{
444
467
  [Key in NonRelationFields<Schema, Model> as Key extends ValueOfPotentialTuple<Args['by']> ? Key : never]: MapModelFieldType<Schema, Model, Key>;
@@ -498,6 +521,10 @@ type AuthType<Schema extends SchemaDef> = string extends GetModels<Schema> ? Rec
498
521
  * Client API methods that are not supported in transactions.
499
522
  */
500
523
  declare const TRANSACTION_UNSUPPORTED_METHODS: readonly ["$transaction", "$disconnect", "$use"];
524
+ /**
525
+ * Logical combinators used in filters.
526
+ */
527
+ declare const LOGICAL_COMBINATORS: readonly ["AND", "OR", "NOT"];
501
528
 
502
529
  declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
503
530
  protected readonly schema: Schema;
@@ -506,7 +533,8 @@ declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
506
533
  transformPrimitive(value: unknown, _type: BuiltinType, _forArrayField: boolean): unknown;
507
534
  buildSelectModel(eb: ExpressionBuilder<any, any>, model: string): SelectQueryBuilder<any, any, {}>;
508
535
  buildFilter(eb: ExpressionBuilder<any, any>, model: string, modelAlias: string, where: boolean | object | undefined): Expression<SqlBool>;
509
- protected buildCompositeFilter(eb: ExpressionBuilder<any, any>, model: string, modelAlias: string, key: 'AND' | 'OR' | 'NOT', payload: any): Expression<SqlBool>;
536
+ private isLogicalCombinator;
537
+ protected buildCompositeFilter(eb: ExpressionBuilder<any, any>, model: string, modelAlias: string, key: (typeof LOGICAL_COMBINATORS)[number], payload: any): Expression<SqlBool>;
510
538
  private buildRelationFilter;
511
539
  private buildToOneRelationFilter;
512
540
  private buildToManyRelationFilter;
@@ -1409,17 +1437,18 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1409
1437
  * _count: true
1410
1438
  * }); // result: `Array<{ country: string, city: string, _count: number }>`
1411
1439
  *
1412
- * // group by with sorting, the `orderBy` fields must be in the `by` list
1440
+ * // group by with sorting, the `orderBy` fields must be either an aggregation
1441
+ * // or a field used in the `by` list
1413
1442
  * await db.profile.groupBy({
1414
1443
  * by: 'country',
1415
1444
  * orderBy: { country: 'desc' }
1416
1445
  * });
1417
1446
  *
1418
- * // group by with having (post-aggregation filter), the `having` fields must
1419
- * // be in the `by` list
1447
+ * // group by with having (post-aggregation filter), the fields used in `having` must
1448
+ * // be either an aggregation, or a field used in the `by` list
1420
1449
  * await db.profile.groupBy({
1421
1450
  * by: 'country',
1422
- * having: { country: 'US' }
1451
+ * having: { country: 'US', age: { _avg: { gte: 18 } } }
1423
1452
  * });
1424
1453
  */
1425
1454
  groupBy<T extends GroupByArgs<Schema, Model>>(args: Subset<T, GroupByArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<GroupByResult<Schema, Model, T>>>;