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

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.
package/dist/index.js CHANGED
@@ -199,23 +199,23 @@ function getRelationForeignKeyFieldPairs(schema, model, relationField) {
199
199
  }
200
200
  __name(getRelationForeignKeyFieldPairs, "getRelationForeignKeyFieldPairs");
201
201
  function isScalarField(schema, model, field) {
202
- const fieldDef = requireField(schema, model, field);
203
- return !fieldDef.relation && !fieldDef.foreignKeyFor;
202
+ const fieldDef = getField(schema, model, field);
203
+ return !fieldDef?.relation && !fieldDef?.foreignKeyFor;
204
204
  }
205
205
  __name(isScalarField, "isScalarField");
206
206
  function isForeignKeyField(schema, model, field) {
207
- const fieldDef = requireField(schema, model, field);
208
- return !!fieldDef.foreignKeyFor;
207
+ const fieldDef = getField(schema, model, field);
208
+ return !!fieldDef?.foreignKeyFor;
209
209
  }
210
210
  __name(isForeignKeyField, "isForeignKeyField");
211
211
  function isRelationField(schema, model, field) {
212
- const fieldDef = requireField(schema, model, field);
213
- return !!fieldDef.relation;
212
+ const fieldDef = getField(schema, model, field);
213
+ return !!fieldDef?.relation;
214
214
  }
215
215
  __name(isRelationField, "isRelationField");
216
216
  function isInheritedField(schema, model, field) {
217
- const fieldDef = requireField(schema, model, field);
218
- return !!fieldDef.originModel;
217
+ const fieldDef = getField(schema, model, field);
218
+ return !!fieldDef?.originModel;
219
219
  }
220
220
  __name(isInheritedField, "isInheritedField");
221
221
  function getUniqueFields(schema, model) {
@@ -696,7 +696,7 @@ var BaseCrudDialect = class {
696
696
  buildLiteralFilter(eb, lhs, type, rhs) {
697
697
  return eb(lhs, "=", rhs !== null && rhs !== void 0 ? this.transformPrimitive(rhs, type, false) : rhs);
698
698
  }
699
- buildStandardFilter(eb, type, payload, lhs, getRhs, recurse, throwIfInvalid = false, onlyForKeys = void 0) {
699
+ buildStandardFilter(eb, type, payload, lhs, getRhs, recurse, throwIfInvalid = false, onlyForKeys = void 0, excludeKeys = []) {
700
700
  if (payload === null || !isPlainObject(payload)) {
701
701
  return {
702
702
  conditions: [
@@ -711,6 +711,9 @@ var BaseCrudDialect = class {
711
711
  if (onlyForKeys && !onlyForKeys.includes(op)) {
712
712
  continue;
713
713
  }
714
+ if (excludeKeys.includes(op)) {
715
+ continue;
716
+ }
714
717
  const rhs = Array.isArray(value) ? value.map(getRhs) : getRhs(value);
715
718
  const condition = match(op).with("equals", () => rhs === null ? eb(lhs, "is", null) : eb(lhs, "=", rhs)).with("in", () => {
716
719
  invariant(Array.isArray(rhs), "right hand side must be an array");
@@ -744,21 +747,20 @@ var BaseCrudDialect = class {
744
747
  };
745
748
  }
746
749
  buildStringFilter(eb, fieldRef, payload) {
747
- let insensitive = false;
748
- if (payload && typeof payload === "object" && "mode" in payload && payload.mode === "insensitive") {
749
- insensitive = true;
750
- fieldRef = eb.fn("lower", [
751
- fieldRef
752
- ]);
750
+ let mode;
751
+ if (payload && typeof payload === "object" && "mode" in payload) {
752
+ mode = payload.mode;
753
753
  }
754
- const { conditions, consumedKeys } = this.buildStandardFilter(eb, "String", payload, fieldRef, (value) => this.prepStringCasing(eb, value, insensitive), (value) => this.buildStringFilter(eb, fieldRef, value));
754
+ const { conditions, consumedKeys } = this.buildStandardFilter(eb, "String", payload, mode === "insensitive" ? eb.fn("lower", [
755
+ fieldRef
756
+ ]) : fieldRef, (value) => this.prepStringCasing(eb, value, mode), (value) => this.buildStringFilter(eb, fieldRef, value));
755
757
  if (payload && typeof payload === "object") {
756
758
  for (const [key, value] of Object.entries(payload)) {
757
759
  if (key === "mode" || consumedKeys.includes(key)) {
758
760
  continue;
759
761
  }
760
- const condition = match(key).with("contains", () => insensitive ? eb(fieldRef, "ilike", sql.lit(`%${value}%`)) : eb(fieldRef, "like", sql.lit(`%${value}%`))).with("startsWith", () => insensitive ? eb(fieldRef, "ilike", sql.lit(`${value}%`)) : eb(fieldRef, "like", sql.lit(`${value}%`))).with("endsWith", () => insensitive ? eb(fieldRef, "ilike", sql.lit(`%${value}`)) : eb(fieldRef, "like", sql.lit(`%${value}`))).otherwise(() => {
761
- throw new Error(`Invalid string filter key: ${key}`);
762
+ const condition = match(key).with("contains", () => mode === "insensitive" ? eb(fieldRef, "ilike", sql.val(`%${value}%`)) : eb(fieldRef, "like", sql.val(`%${value}%`))).with("startsWith", () => mode === "insensitive" ? eb(fieldRef, "ilike", sql.val(`${value}%`)) : eb(fieldRef, "like", sql.val(`${value}%`))).with("endsWith", () => mode === "insensitive" ? eb(fieldRef, "ilike", sql.val(`%${value}`)) : eb(fieldRef, "like", sql.val(`%${value}`))).otherwise(() => {
763
+ throw new QueryError(`Invalid string filter key: ${key}`);
762
764
  });
763
765
  if (condition) {
764
766
  conditions.push(condition);
@@ -767,15 +769,18 @@ var BaseCrudDialect = class {
767
769
  }
768
770
  return this.and(eb, ...conditions);
769
771
  }
770
- prepStringCasing(eb, value, toLower = true) {
772
+ prepStringCasing(eb, value, mode) {
773
+ if (!mode || mode === "default") {
774
+ return value === null ? value : sql.val(value);
775
+ }
771
776
  if (typeof value === "string") {
772
- return toLower ? eb.fn("lower", [
773
- sql.lit(value)
774
- ]) : sql.lit(value);
777
+ return eb.fn("lower", [
778
+ sql.val(value)
779
+ ]);
775
780
  } else if (Array.isArray(value)) {
776
- return value.map((v) => this.prepStringCasing(eb, v, toLower));
781
+ return value.map((v) => this.prepStringCasing(eb, v, mode));
777
782
  } else {
778
- return value === null ? null : sql.lit(value);
783
+ return value === null ? null : sql.val(value);
779
784
  }
780
785
  }
781
786
  buildNumberFilter(eb, fieldRef, type, payload) {
@@ -937,6 +942,32 @@ var BaseCrudDialect = class {
937
942
  });
938
943
  return query;
939
944
  }
945
+ buildCountJson(model, eb, parentAlias, payload) {
946
+ const modelDef = requireModel(this.schema, model);
947
+ const toManyRelations = Object.entries(modelDef.fields).filter(([, field]) => field.relation && field.array);
948
+ const selections = payload === true ? {
949
+ select: toManyRelations.reduce((acc, [field]) => {
950
+ acc[field] = true;
951
+ return acc;
952
+ }, {})
953
+ } : payload;
954
+ const jsonObject = {};
955
+ for (const [field, value] of Object.entries(selections.select)) {
956
+ const fieldDef = requireField(this.schema, model, field);
957
+ const fieldModel = fieldDef.type;
958
+ const joinPairs = buildJoinPairs(this.schema, model, parentAlias, field, fieldModel);
959
+ let fieldCountQuery = eb.selectFrom(fieldModel).select(eb.fn.countAll().as(`_count$${field}`));
960
+ for (const [left, right] of joinPairs) {
961
+ fieldCountQuery = fieldCountQuery.whereRef(left, "=", right);
962
+ }
963
+ if (value && typeof value === "object" && "where" in value && value.where && typeof value.where === "object") {
964
+ const filter = this.buildFilter(eb, fieldModel, fieldModel, value.where);
965
+ fieldCountQuery = fieldCountQuery.where(filter);
966
+ }
967
+ jsonObject[field] = fieldCountQuery;
968
+ }
969
+ return this.buildJsonObject(eb, jsonObject);
970
+ }
940
971
  // #endregion
941
972
  // #region utils
942
973
  negateSort(sort, negated) {
@@ -1065,7 +1096,7 @@ var PostgresCrudDialect = class extends BaseCrudDialect {
1065
1096
  });
1066
1097
  return qb;
1067
1098
  }
1068
- buildRelationObjectArgs(relationModel, relationField, eb, payload, parentName) {
1099
+ buildRelationObjectArgs(relationModel, relationField, eb, payload, parentAlias) {
1069
1100
  const relationModelDef = requireModel(this.schema, relationModel);
1070
1101
  const objArgs = [];
1071
1102
  const descendantModels = getDelegateDescendantModels(this.schema, relationModel);
@@ -1081,20 +1112,28 @@ var PostgresCrudDialect = class extends BaseCrudDialect {
1081
1112
  buildFieldRef(this.schema, relationModel, field, this.options, eb)
1082
1113
  ]).flatMap((v) => v));
1083
1114
  } else if (payload.select) {
1084
- objArgs.push(...Object.entries(payload.select).filter(([, value]) => value).map(([field]) => {
1085
- const fieldDef = requireField(this.schema, relationModel, field);
1086
- const fieldValue = fieldDef.relation ? eb.ref(`${parentName}$${relationField}$${field}.$j`) : buildFieldRef(this.schema, relationModel, field, this.options, eb);
1087
- return [
1088
- sql2.lit(field),
1089
- fieldValue
1090
- ];
1115
+ objArgs.push(...Object.entries(payload.select).filter(([, value]) => value).map(([field, value]) => {
1116
+ if (field === "_count") {
1117
+ const subJson = this.buildCountJson(relationModel, eb, `${parentAlias}$${relationField}`, value);
1118
+ return [
1119
+ sql2.lit(field),
1120
+ subJson
1121
+ ];
1122
+ } else {
1123
+ const fieldDef = requireField(this.schema, relationModel, field);
1124
+ const fieldValue = fieldDef.relation ? eb.ref(`${parentAlias}$${relationField}$${field}.$j`) : buildFieldRef(this.schema, relationModel, field, this.options, eb);
1125
+ return [
1126
+ sql2.lit(field),
1127
+ fieldValue
1128
+ ];
1129
+ }
1091
1130
  }).flatMap((v) => v));
1092
1131
  }
1093
1132
  if (typeof payload === "object" && payload.include && typeof payload.include === "object") {
1094
1133
  objArgs.push(...Object.entries(payload.include).filter(([, value]) => value).map(([field]) => [
1095
1134
  sql2.lit(field),
1096
1135
  // reference the synthesized JSON field
1097
- eb.ref(`${parentName}$${relationField}$${field}.$j`)
1136
+ eb.ref(`${parentAlias}$${relationField}$${field}.$j`)
1098
1137
  ]).flatMap((v) => v));
1099
1138
  }
1100
1139
  return objArgs;
@@ -1147,6 +1186,9 @@ var PostgresCrudDialect = class extends BaseCrudDialect {
1147
1186
  return `ARRAY[${values.map((v) => typeof v === "string" ? `'${v}'` : v)}]`;
1148
1187
  }
1149
1188
  }
1189
+ get supportInsertWithDefault() {
1190
+ return true;
1191
+ }
1150
1192
  };
1151
1193
 
1152
1194
  // src/client/crud/dialects/sqlite.ts
@@ -1177,11 +1219,11 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
1177
1219
  buildRelationSelection(query, model, relationField, parentAlias, payload) {
1178
1220
  return query.select((eb) => this.buildRelationJSON(model, eb, relationField, parentAlias, payload).as(relationField));
1179
1221
  }
1180
- buildRelationJSON(model, eb, relationField, parentName, payload) {
1222
+ buildRelationJSON(model, eb, relationField, parentAlias, payload) {
1181
1223
  const relationFieldDef = requireField(this.schema, model, relationField);
1182
1224
  const relationModel = relationFieldDef.type;
1183
1225
  const relationModelDef = requireModel(this.schema, relationModel);
1184
- const subQueryName = `${parentName}$${relationField}`;
1226
+ const subQueryName = `${parentAlias}$${relationField}`;
1185
1227
  let tbl = eb.selectFrom(() => {
1186
1228
  let subQuery = this.buildSelectModel(eb, relationModel);
1187
1229
  subQuery = this.buildSelectAllFields(relationModel, subQuery, typeof payload === "object" ? payload?.omit : void 0);
@@ -1205,14 +1247,14 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
1205
1247
  const relationIds = getIdFields(this.schema, relationModel);
1206
1248
  invariant3(parentIds.length === 1, "many-to-many relation must have exactly one id field");
1207
1249
  invariant3(relationIds.length === 1, "many-to-many relation must have exactly one id field");
1208
- subQuery = subQuery.where(eb(eb.ref(`${relationModel}.${relationIds[0]}`), "in", eb.selectFrom(m2m.joinTable).select(`${m2m.joinTable}.${m2m.otherFkName}`).whereRef(`${parentName}.${parentIds[0]}`, "=", `${m2m.joinTable}.${m2m.parentFkName}`)));
1250
+ subQuery = subQuery.where(eb(eb.ref(`${relationModel}.${relationIds[0]}`), "in", eb.selectFrom(m2m.joinTable).select(`${m2m.joinTable}.${m2m.otherFkName}`).whereRef(`${parentAlias}.${parentIds[0]}`, "=", `${m2m.joinTable}.${m2m.parentFkName}`)));
1209
1251
  } else {
1210
1252
  const { keyPairs, ownedByModel } = getRelationForeignKeyFieldPairs(this.schema, model, relationField);
1211
1253
  keyPairs.forEach(({ fk, pk }) => {
1212
1254
  if (ownedByModel) {
1213
- subQuery = subQuery.whereRef(`${relationModel}.${pk}`, "=", `${parentName}.${fk}`);
1255
+ subQuery = subQuery.whereRef(`${relationModel}.${pk}`, "=", `${parentAlias}.${fk}`);
1214
1256
  } else {
1215
- subQuery = subQuery.whereRef(`${relationModel}.${fk}`, "=", `${parentName}.${pk}`);
1257
+ subQuery = subQuery.whereRef(`${relationModel}.${fk}`, "=", `${parentAlias}.${pk}`);
1216
1258
  }
1217
1259
  });
1218
1260
  }
@@ -1234,24 +1276,32 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
1234
1276
  ]).flatMap((v) => v));
1235
1277
  } else if (payload.select) {
1236
1278
  objArgs.push(...Object.entries(payload.select).filter(([, value]) => value).map(([field, value]) => {
1237
- const fieldDef = requireField(this.schema, relationModel, field);
1238
- if (fieldDef.relation) {
1239
- const subJson = this.buildRelationJSON(relationModel, eb, field, `${parentName}$${relationField}`, value);
1279
+ if (field === "_count") {
1280
+ const subJson = this.buildCountJson(relationModel, eb, `${parentAlias}$${relationField}`, value);
1240
1281
  return [
1241
1282
  sql3.lit(field),
1242
1283
  subJson
1243
1284
  ];
1244
1285
  } else {
1245
- return [
1246
- sql3.lit(field),
1247
- buildFieldRef(this.schema, relationModel, field, this.options, eb)
1248
- ];
1286
+ const fieldDef = requireField(this.schema, relationModel, field);
1287
+ if (fieldDef.relation) {
1288
+ const subJson = this.buildRelationJSON(relationModel, eb, field, `${parentAlias}$${relationField}`, value);
1289
+ return [
1290
+ sql3.lit(field),
1291
+ subJson
1292
+ ];
1293
+ } else {
1294
+ return [
1295
+ sql3.lit(field),
1296
+ buildFieldRef(this.schema, relationModel, field, this.options, eb)
1297
+ ];
1298
+ }
1249
1299
  }
1250
1300
  }).flatMap((v) => v));
1251
1301
  }
1252
1302
  if (typeof payload === "object" && payload.include && typeof payload.include === "object") {
1253
1303
  objArgs.push(...Object.entries(payload.include).filter(([, value]) => value).map(([field, value]) => {
1254
- const subJson = this.buildRelationJSON(relationModel, eb, field, `${parentName}$${relationField}`, value);
1304
+ const subJson = this.buildRelationJSON(relationModel, eb, field, `${parentAlias}$${relationField}`, value);
1255
1305
  return [
1256
1306
  sql3.lit(field),
1257
1307
  subJson
@@ -1301,6 +1351,9 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
1301
1351
  buildArrayLiteralSQL(_values) {
1302
1352
  throw new Error("SQLite does not support array literals");
1303
1353
  }
1354
+ get supportInsertWithDefault() {
1355
+ return false;
1356
+ }
1304
1357
  };
1305
1358
 
1306
1359
  // src/client/crud/dialects/index.ts
@@ -2660,6 +2713,7 @@ var BaseOperationHandler = class {
2660
2713
  query = query.distinctOn(distinct.map((f) => sql4.ref(`${model}.${f}`)));
2661
2714
  } else {
2662
2715
  inMemoryDistinct = distinct;
2716
+ query = distinct.reduce((acc, field) => acc.select((eb) => buildFieldRef(this.schema, model, field, this.options, eb).as(`$distinct$${field}`)), query);
2663
2717
  }
2664
2718
  }
2665
2719
  if (args && "select" in args && args.select) {
@@ -2697,13 +2751,16 @@ ${compiled.parameters.map((p) => inspect(p)).join("\n")}`;
2697
2751
  const distinctResult = [];
2698
2752
  const seen = /* @__PURE__ */ new Set();
2699
2753
  for (const r of result) {
2700
- const key = safeJSONStringify(inMemoryDistinct.map((f) => r[f]));
2754
+ const key = safeJSONStringify(inMemoryDistinct.map((f) => r[`$distinct$${f}`]));
2701
2755
  if (!seen.has(key)) {
2702
2756
  distinctResult.push(r);
2703
2757
  seen.add(key);
2704
2758
  }
2705
2759
  }
2706
2760
  result = distinctResult;
2761
+ for (const r of result) {
2762
+ Object.keys(r).filter((k) => k.startsWith("$distinct$")).forEach((k) => delete r[k]);
2763
+ }
2707
2764
  }
2708
2765
  return result;
2709
2766
  }
@@ -2741,32 +2798,7 @@ ${compiled.parameters.map((p) => inspect(p)).join("\n")}`;
2741
2798
  return result;
2742
2799
  }
2743
2800
  buildCountSelection(query, model, parentAlias, payload) {
2744
- const modelDef = requireModel(this.schema, model);
2745
- const toManyRelations = Object.entries(modelDef.fields).filter(([, field]) => field.relation && field.array);
2746
- const selections = payload === true ? {
2747
- select: toManyRelations.reduce((acc, [field]) => {
2748
- acc[field] = true;
2749
- return acc;
2750
- }, {})
2751
- } : payload;
2752
- const eb = expressionBuilder2();
2753
- const jsonObject = {};
2754
- for (const [field, value] of Object.entries(selections.select)) {
2755
- const fieldDef = requireField(this.schema, model, field);
2756
- const fieldModel = fieldDef.type;
2757
- const joinPairs = buildJoinPairs(this.schema, model, parentAlias, field, fieldModel);
2758
- let fieldCountQuery = eb.selectFrom(fieldModel).select(eb.fn.countAll().as(`_count$${field}`));
2759
- for (const [left, right] of joinPairs) {
2760
- fieldCountQuery = fieldCountQuery.whereRef(left, "=", right);
2761
- }
2762
- if (value && typeof value === "object" && "where" in value && value.where && typeof value.where === "object") {
2763
- const filter = this.dialect.buildFilter(eb, fieldModel, fieldModel, value.where);
2764
- fieldCountQuery = fieldCountQuery.where(filter);
2765
- }
2766
- jsonObject[field] = fieldCountQuery;
2767
- }
2768
- query = query.select((eb2) => this.dialect.buildJsonObject(eb2, jsonObject).as("_count"));
2769
- return query;
2801
+ return query.select((eb) => this.dialect.buildCountJson(model, eb, parentAlias, payload).as("_count"));
2770
2802
  }
2771
2803
  buildCursorFilter(model, query, cursor, orderBy, negateOrderBy) {
2772
2804
  if (!orderBy) {
@@ -3073,6 +3105,29 @@ ${compiled.parameters.map((p) => inspect(p)).join("\n")}`;
3073
3105
  }
3074
3106
  return this.fillGeneratedValues(modelDef, newItem);
3075
3107
  });
3108
+ if (!this.dialect.supportInsertWithDefault) {
3109
+ const allPassedFields = createData.reduce((acc, item) => {
3110
+ Object.keys(item).forEach((field) => {
3111
+ if (!acc.includes(field)) {
3112
+ acc.push(field);
3113
+ }
3114
+ });
3115
+ return acc;
3116
+ }, []);
3117
+ for (const item of createData) {
3118
+ if (Object.keys(item).length === allPassedFields.length) {
3119
+ continue;
3120
+ }
3121
+ for (const field of allPassedFields) {
3122
+ if (!(field in item)) {
3123
+ const fieldDef = this.requireField(model, field);
3124
+ if (fieldDef.default !== void 0 && fieldDef.default !== null && typeof fieldDef.default !== "object") {
3125
+ item[field] = this.dialect.transformPrimitive(fieldDef.default, fieldDef.type, !!fieldDef.array);
3126
+ }
3127
+ }
3128
+ }
3129
+ }
3130
+ }
3076
3131
  if (modelDef.baseModel) {
3077
3132
  if (input.skipDuplicates) {
3078
3133
  throw new QueryError('"skipDuplicates" options is not supported for polymorphic models');
@@ -4469,11 +4524,11 @@ var InputValidator = class {
4469
4524
  fields["distinct"] = this.makeDistinctSchema(model).optional();
4470
4525
  fields["cursor"] = this.makeCursorSchema(model).optional();
4471
4526
  if (options.collection) {
4472
- fields["skip"] = z.number().int().nonnegative().optional();
4473
- fields["take"] = z.number().int().optional();
4527
+ fields["skip"] = this.makeSkipSchema().optional();
4528
+ fields["take"] = this.makeTakeSchema().optional();
4474
4529
  fields["orderBy"] = this.orArray(this.makeOrderBySchema(model, true, false), true).optional();
4475
4530
  }
4476
- let result = z.object(fields).strict();
4531
+ let result = z.strictObject(fields);
4477
4532
  result = this.refineForSelectIncludeMutuallyExclusive(result);
4478
4533
  result = this.refineForSelectOmitMutuallyExclusive(result);
4479
4534
  if (!options.unique) {
@@ -4540,7 +4595,7 @@ var InputValidator = class {
4540
4595
  if (fieldDef.array) {
4541
4596
  fieldSchema = z.union([
4542
4597
  fieldSchema,
4543
- z.object({
4598
+ z.strictObject({
4544
4599
  some: fieldSchema.optional(),
4545
4600
  every: fieldSchema.optional(),
4546
4601
  none: fieldSchema.optional()
@@ -4549,7 +4604,7 @@ var InputValidator = class {
4549
4604
  } else {
4550
4605
  fieldSchema = z.union([
4551
4606
  fieldSchema,
4552
- z.object({
4607
+ z.strictObject({
4553
4608
  is: fieldSchema.optional(),
4554
4609
  isNot: fieldSchema.optional()
4555
4610
  })
@@ -4600,7 +4655,7 @@ var InputValidator = class {
4600
4655
  fields["AND"] = this.orArray(z.lazy(() => this.makeWhereSchema(model, false, withoutRelationFields)), true).optional();
4601
4656
  fields["OR"] = z.lazy(() => this.makeWhereSchema(model, false, withoutRelationFields)).array().optional();
4602
4657
  fields["NOT"] = this.orArray(z.lazy(() => this.makeWhereSchema(model, false, withoutRelationFields)), true).optional();
4603
- const baseWhere = z.object(fields).strict();
4658
+ const baseWhere = z.strictObject(fields);
4604
4659
  let result = baseWhere;
4605
4660
  if (unique) {
4606
4661
  const uniqueFields = getUniqueFields(this.schema, model);
@@ -4624,7 +4679,7 @@ var InputValidator = class {
4624
4679
  const components = this.makeCommonPrimitiveFilterComponents(baseSchema, optional, () => z.lazy(() => this.makeEnumFilterSchema(enumDef, optional)));
4625
4680
  return z.union([
4626
4681
  this.nullableIf(baseSchema, optional),
4627
- z.object({
4682
+ z.strictObject({
4628
4683
  equals: components.equals,
4629
4684
  in: components.in,
4630
4685
  notIn: components.notIn,
@@ -4633,7 +4688,7 @@ var InputValidator = class {
4633
4688
  ]);
4634
4689
  }
4635
4690
  makeArrayFilterSchema(type) {
4636
- return z.object({
4691
+ return z.strictObject({
4637
4692
  equals: this.makePrimitiveSchema(type).array().optional(),
4638
4693
  has: this.makePrimitiveSchema(type).optional(),
4639
4694
  hasEvery: this.makePrimitiveSchema(type).array().optional(),
@@ -4659,7 +4714,7 @@ var InputValidator = class {
4659
4714
  makeBooleanFilterSchema(optional) {
4660
4715
  return z.union([
4661
4716
  this.nullableIf(z.boolean(), optional),
4662
- z.object({
4717
+ z.strictObject({
4663
4718
  equals: this.nullableIf(z.boolean(), optional).optional(),
4664
4719
  not: z.lazy(() => this.makeBooleanFilterSchema(optional)).optional()
4665
4720
  })
@@ -4670,7 +4725,7 @@ var InputValidator = class {
4670
4725
  const components = this.makeCommonPrimitiveFilterComponents(baseSchema, optional, () => z.instanceof(Uint8Array));
4671
4726
  return z.union([
4672
4727
  this.nullableIf(baseSchema, optional),
4673
- z.object({
4728
+ z.strictObject({
4674
4729
  equals: components.equals,
4675
4730
  in: components.in,
4676
4731
  notIn: components.notIn,
@@ -4694,14 +4749,31 @@ var InputValidator = class {
4694
4749
  makeCommonPrimitiveFilterSchema(baseSchema, optional, makeThis) {
4695
4750
  return z.union([
4696
4751
  this.nullableIf(baseSchema, optional),
4697
- z.object(this.makeCommonPrimitiveFilterComponents(baseSchema, optional, makeThis))
4752
+ z.strictObject(this.makeCommonPrimitiveFilterComponents(baseSchema, optional, makeThis))
4698
4753
  ]);
4699
4754
  }
4700
4755
  makeNumberFilterSchema(baseSchema, optional) {
4701
4756
  return this.makeCommonPrimitiveFilterSchema(baseSchema, optional, () => z.lazy(() => this.makeNumberFilterSchema(baseSchema, optional)));
4702
4757
  }
4703
4758
  makeStringFilterSchema(optional) {
4704
- return this.makeCommonPrimitiveFilterSchema(z.string(), optional, () => z.lazy(() => this.makeStringFilterSchema(optional)));
4759
+ return z.union([
4760
+ this.nullableIf(z.string(), optional),
4761
+ z.strictObject({
4762
+ ...this.makeCommonPrimitiveFilterComponents(z.string(), optional, () => z.lazy(() => this.makeStringFilterSchema(optional))),
4763
+ startsWith: z.string().optional(),
4764
+ endsWith: z.string().optional(),
4765
+ contains: z.string().optional(),
4766
+ ...this.providerSupportsCaseSensitivity ? {
4767
+ mode: this.makeStringModeSchema().optional()
4768
+ } : {}
4769
+ })
4770
+ ]);
4771
+ }
4772
+ makeStringModeSchema() {
4773
+ return z.union([
4774
+ z.literal("default"),
4775
+ z.literal("insensitive")
4776
+ ]);
4705
4777
  }
4706
4778
  makeSelectSchema(model) {
4707
4779
  const modelDef = requireModel(this.schema, model);
@@ -4711,7 +4783,7 @@ var InputValidator = class {
4711
4783
  if (fieldDef.relation) {
4712
4784
  fields[field] = z.union([
4713
4785
  z.literal(true),
4714
- z.object({
4786
+ z.strictObject({
4715
4787
  select: z.lazy(() => this.makeSelectSchema(fieldDef.type)).optional(),
4716
4788
  include: z.lazy(() => this.makeIncludeSchema(fieldDef.type)).optional()
4717
4789
  })
@@ -4720,22 +4792,24 @@ var InputValidator = class {
4720
4792
  fields[field] = z.boolean().optional();
4721
4793
  }
4722
4794
  }
4723
- const toManyRelations = Object.entries(modelDef.fields).filter(([, value]) => value.relation && value.array);
4795
+ const toManyRelations = Object.values(modelDef.fields).filter((def) => def.relation && def.array);
4724
4796
  if (toManyRelations.length > 0) {
4725
4797
  fields["_count"] = z.union([
4726
4798
  z.literal(true),
4727
- z.object(toManyRelations.reduce((acc, [name, fieldDef]) => ({
4728
- ...acc,
4729
- [name]: z.union([
4730
- z.boolean(),
4731
- z.object({
4732
- where: this.makeWhereSchema(fieldDef.type, false, false)
4733
- })
4734
- ]).optional()
4735
- }), {}))
4799
+ z.strictObject({
4800
+ select: z.strictObject(toManyRelations.reduce((acc, fieldDef) => ({
4801
+ ...acc,
4802
+ [fieldDef.name]: z.union([
4803
+ z.boolean(),
4804
+ z.strictObject({
4805
+ where: this.makeWhereSchema(fieldDef.type, false, false)
4806
+ })
4807
+ ]).optional()
4808
+ }), {}))
4809
+ })
4736
4810
  ]).optional();
4737
4811
  }
4738
- return z.object(fields).strict();
4812
+ return z.strictObject(fields);
4739
4813
  }
4740
4814
  makeOmitSchema(model) {
4741
4815
  const modelDef = requireModel(this.schema, model);
@@ -4746,7 +4820,7 @@ var InputValidator = class {
4746
4820
  fields[field] = z.boolean().optional();
4747
4821
  }
4748
4822
  }
4749
- return z.object(fields).strict();
4823
+ return z.strictObject(fields);
4750
4824
  }
4751
4825
  makeIncludeSchema(model) {
4752
4826
  const modelDef = requireModel(this.schema, model);
@@ -4756,15 +4830,20 @@ var InputValidator = class {
4756
4830
  if (fieldDef.relation) {
4757
4831
  fields[field] = z.union([
4758
4832
  z.literal(true),
4759
- z.object({
4833
+ z.strictObject({
4760
4834
  select: z.lazy(() => this.makeSelectSchema(fieldDef.type)).optional(),
4761
4835
  include: z.lazy(() => this.makeIncludeSchema(fieldDef.type)).optional(),
4762
- where: z.lazy(() => this.makeWhereSchema(fieldDef.type, false)).optional()
4836
+ omit: z.lazy(() => this.makeOmitSchema(fieldDef.type)).optional(),
4837
+ where: z.lazy(() => this.makeWhereSchema(fieldDef.type, false)).optional(),
4838
+ orderBy: z.lazy(() => this.makeOrderBySchema(fieldDef.type, true, false)).optional(),
4839
+ skip: this.makeSkipSchema().optional(),
4840
+ take: this.makeTakeSchema().optional(),
4841
+ distinct: this.makeDistinctSchema(fieldDef.type).optional()
4763
4842
  })
4764
4843
  ]).optional();
4765
4844
  }
4766
4845
  }
4767
- return z.object(fields).strict();
4846
+ return z.strictObject(fields);
4768
4847
  }
4769
4848
  makeOrderBySchema(model, withRelation, WithAggregation) {
4770
4849
  const modelDef = requireModel(this.schema, model);
@@ -4777,13 +4856,21 @@ var InputValidator = class {
4777
4856
  const fieldDef = requireField(this.schema, model, field);
4778
4857
  if (fieldDef.relation) {
4779
4858
  if (withRelation) {
4780
- fields[field] = z.lazy(() => this.makeOrderBySchema(fieldDef.type, withRelation, WithAggregation).optional());
4859
+ fields[field] = z.lazy(() => {
4860
+ let relationOrderBy = this.makeOrderBySchema(fieldDef.type, withRelation, WithAggregation);
4861
+ if (fieldDef.array) {
4862
+ relationOrderBy = relationOrderBy.extend({
4863
+ _count: sort
4864
+ });
4865
+ }
4866
+ return relationOrderBy.optional();
4867
+ });
4781
4868
  }
4782
4869
  } else {
4783
4870
  if (fieldDef.optional) {
4784
4871
  fields[field] = z.union([
4785
4872
  sort,
4786
- z.object({
4873
+ z.strictObject({
4787
4874
  sort,
4788
4875
  nulls: z.union([
4789
4876
  z.literal("first"),
@@ -4808,7 +4895,7 @@ var InputValidator = class {
4808
4895
  fields[agg] = z.lazy(() => this.makeOrderBySchema(model, true, false).optional());
4809
4896
  }
4810
4897
  }
4811
- return z.object(fields);
4898
+ return z.strictObject(fields);
4812
4899
  }
4813
4900
  makeDistinctSchema(model) {
4814
4901
  const modelDef = requireModel(this.schema, model);
@@ -4827,7 +4914,7 @@ var InputValidator = class {
4827
4914
  select: this.makeSelectSchema(model).optional(),
4828
4915
  include: this.makeIncludeSchema(model).optional(),
4829
4916
  omit: this.makeOmitSchema(model).optional()
4830
- }).strict();
4917
+ });
4831
4918
  return this.refineForSelectIncludeMutuallyExclusive(schema);
4832
4919
  }
4833
4920
  makeCreateManySchema(model) {
@@ -4835,7 +4922,7 @@ var InputValidator = class {
4835
4922
  }
4836
4923
  makeCreateManyAndReturnSchema(model) {
4837
4924
  const base = this.makeCreateManyDataSchema(model, []);
4838
- const result = base.merge(z.object({
4925
+ const result = base.merge(z.strictObject({
4839
4926
  select: this.makeSelectSchema(model).optional(),
4840
4927
  omit: this.makeOmitSchema(model).optional()
4841
4928
  }));
@@ -4897,7 +4984,7 @@ var InputValidator = class {
4897
4984
  if (fieldDef.array) {
4898
4985
  fieldSchema = z.union([
4899
4986
  z.array(fieldSchema),
4900
- z.object({
4987
+ z.strictObject({
4901
4988
  set: z.array(fieldSchema)
4902
4989
  })
4903
4990
  ]).optional();
@@ -4915,16 +5002,16 @@ var InputValidator = class {
4915
5002
  }
4916
5003
  });
4917
5004
  if (!hasRelation) {
4918
- return this.orArray(z.object(uncheckedVariantFields).strict(), canBeArray);
5005
+ return this.orArray(z.strictObject(uncheckedVariantFields), canBeArray);
4919
5006
  } else {
4920
5007
  return z.union([
4921
- z.object(uncheckedVariantFields).strict(),
4922
- z.object(checkedVariantFields).strict(),
5008
+ z.strictObject(uncheckedVariantFields),
5009
+ z.strictObject(checkedVariantFields),
4923
5010
  ...canBeArray ? [
4924
- z.array(z.object(uncheckedVariantFields).strict())
5011
+ z.array(z.strictObject(uncheckedVariantFields))
4925
5012
  ] : [],
4926
5013
  ...canBeArray ? [
4927
- z.array(z.object(checkedVariantFields).strict())
5014
+ z.array(z.strictObject(checkedVariantFields))
4928
5015
  ] : []
4929
5016
  ]);
4930
5017
  }
@@ -4952,31 +5039,31 @@ var InputValidator = class {
4952
5039
  fields["disconnect"] = this.makeDisconnectDataSchema(fieldType, array).optional();
4953
5040
  fields["delete"] = this.makeDeleteRelationDataSchema(fieldType, array, true).optional();
4954
5041
  }
4955
- fields["update"] = array ? this.orArray(z.object({
5042
+ fields["update"] = array ? this.orArray(z.strictObject({
4956
5043
  where: this.makeWhereSchema(fieldType, true),
4957
5044
  data: this.makeUpdateDataSchema(fieldType, withoutFields)
4958
5045
  }), true).optional() : z.union([
4959
- z.object({
5046
+ z.strictObject({
4960
5047
  where: this.makeWhereSchema(fieldType, true),
4961
5048
  data: this.makeUpdateDataSchema(fieldType, withoutFields)
4962
5049
  }),
4963
5050
  this.makeUpdateDataSchema(fieldType, withoutFields)
4964
5051
  ]).optional();
4965
- fields["upsert"] = this.orArray(z.object({
5052
+ fields["upsert"] = this.orArray(z.strictObject({
4966
5053
  where: this.makeWhereSchema(fieldType, true),
4967
5054
  create: this.makeCreateDataSchema(fieldType, false, withoutFields),
4968
5055
  update: this.makeUpdateDataSchema(fieldType, withoutFields)
4969
5056
  }), true).optional();
4970
5057
  if (array) {
4971
5058
  fields["set"] = this.makeSetDataSchema(fieldType, true).optional();
4972
- fields["updateMany"] = this.orArray(z.object({
5059
+ fields["updateMany"] = this.orArray(z.strictObject({
4973
5060
  where: this.makeWhereSchema(fieldType, false, true),
4974
5061
  data: this.makeUpdateDataSchema(fieldType, withoutFields)
4975
5062
  }), true).optional();
4976
5063
  fields["deleteMany"] = this.makeDeleteRelationDataSchema(fieldType, true, false).optional();
4977
5064
  }
4978
5065
  }
4979
- return z.object(fields).strict();
5066
+ return z.strictObject(fields);
4980
5067
  }
4981
5068
  makeSetDataSchema(model, canBeArray) {
4982
5069
  return this.orArray(this.makeWhereSchema(model, true), canBeArray);
@@ -5006,13 +5093,13 @@ var InputValidator = class {
5006
5093
  return this.orArray(z.object({
5007
5094
  where: whereSchema,
5008
5095
  create: createSchema
5009
- }).strict(), canBeArray);
5096
+ }), canBeArray);
5010
5097
  }
5011
5098
  makeCreateManyDataSchema(model, withoutFields) {
5012
5099
  return z.object({
5013
5100
  data: this.makeCreateDataSchema(model, true, withoutFields, true),
5014
5101
  skipDuplicates: z.boolean().optional()
5015
- }).strict();
5102
+ });
5016
5103
  }
5017
5104
  // #endregion
5018
5105
  // #region Update
@@ -5023,7 +5110,7 @@ var InputValidator = class {
5023
5110
  select: this.makeSelectSchema(model).optional(),
5024
5111
  include: this.makeIncludeSchema(model).optional(),
5025
5112
  omit: this.makeOmitSchema(model).optional()
5026
- }).strict();
5113
+ });
5027
5114
  return this.refineForSelectIncludeMutuallyExclusive(schema);
5028
5115
  }
5029
5116
  makeUpdateManySchema(model) {
@@ -5031,11 +5118,11 @@ var InputValidator = class {
5031
5118
  where: this.makeWhereSchema(model, false).optional(),
5032
5119
  data: this.makeUpdateDataSchema(model, [], true),
5033
5120
  limit: z.number().int().nonnegative().optional()
5034
- }).strict();
5121
+ });
5035
5122
  }
5036
5123
  makeUpdateManyAndReturnSchema(model) {
5037
5124
  const base = this.makeUpdateManySchema(model);
5038
- const result = base.merge(z.object({
5125
+ const result = base.merge(z.strictObject({
5039
5126
  select: this.makeSelectSchema(model).optional(),
5040
5127
  omit: this.makeOmitSchema(model).optional()
5041
5128
  }));
@@ -5049,7 +5136,7 @@ var InputValidator = class {
5049
5136
  select: this.makeSelectSchema(model).optional(),
5050
5137
  include: this.makeIncludeSchema(model).optional(),
5051
5138
  omit: this.makeOmitSchema(model).optional()
5052
- }).strict();
5139
+ });
5053
5140
  return this.refineForSelectIncludeMutuallyExclusive(schema);
5054
5141
  }
5055
5142
  makeUpdateDataSchema(model, withoutFields = [], withoutRelationFields = false) {
@@ -5116,11 +5203,11 @@ var InputValidator = class {
5116
5203
  }
5117
5204
  });
5118
5205
  if (!hasRelation) {
5119
- return z.object(uncheckedVariantFields).strict();
5206
+ return z.strictObject(uncheckedVariantFields);
5120
5207
  } else {
5121
5208
  return z.union([
5122
- z.object(uncheckedVariantFields).strict(),
5123
- z.object(checkedVariantFields).strict()
5209
+ z.strictObject(uncheckedVariantFields),
5210
+ z.strictObject(checkedVariantFields)
5124
5211
  ]);
5125
5212
  }
5126
5213
  }
@@ -5131,25 +5218,25 @@ var InputValidator = class {
5131
5218
  where: this.makeWhereSchema(model, true),
5132
5219
  select: this.makeSelectSchema(model).optional(),
5133
5220
  include: this.makeIncludeSchema(model).optional()
5134
- }).strict();
5221
+ });
5135
5222
  return this.refineForSelectIncludeMutuallyExclusive(schema);
5136
5223
  }
5137
5224
  makeDeleteManySchema(model) {
5138
5225
  return z.object({
5139
5226
  where: this.makeWhereSchema(model, false).optional(),
5140
5227
  limit: z.number().int().nonnegative().optional()
5141
- }).strict().optional();
5228
+ }).optional();
5142
5229
  }
5143
5230
  // #endregion
5144
5231
  // #region Count
5145
5232
  makeCountSchema(model) {
5146
5233
  return z.object({
5147
5234
  where: this.makeWhereSchema(model, false).optional(),
5148
- skip: z.number().int().nonnegative().optional(),
5149
- take: z.number().int().optional(),
5235
+ skip: this.makeSkipSchema().optional(),
5236
+ take: this.makeTakeSchema().optional(),
5150
5237
  orderBy: this.orArray(this.makeOrderBySchema(model, true, false), true).optional(),
5151
5238
  select: this.makeCountAggregateInputSchema(model).optional()
5152
- }).strict().optional();
5239
+ }).optional();
5153
5240
  }
5154
5241
  makeCountAggregateInputSchema(model) {
5155
5242
  const modelDef = requireModel(this.schema, model);
@@ -5161,7 +5248,7 @@ var InputValidator = class {
5161
5248
  acc[field] = z.literal(true).optional();
5162
5249
  return acc;
5163
5250
  }, {})
5164
- }).strict()
5251
+ })
5165
5252
  ]);
5166
5253
  }
5167
5254
  // #endregion
@@ -5169,19 +5256,19 @@ var InputValidator = class {
5169
5256
  makeAggregateSchema(model) {
5170
5257
  return z.object({
5171
5258
  where: this.makeWhereSchema(model, false).optional(),
5172
- skip: z.number().int().nonnegative().optional(),
5173
- take: z.number().int().optional(),
5259
+ skip: this.makeSkipSchema().optional(),
5260
+ take: this.makeTakeSchema().optional(),
5174
5261
  orderBy: this.orArray(this.makeOrderBySchema(model, true, false), true).optional(),
5175
5262
  _count: this.makeCountAggregateInputSchema(model).optional(),
5176
5263
  _avg: this.makeSumAvgInputSchema(model).optional(),
5177
5264
  _sum: this.makeSumAvgInputSchema(model).optional(),
5178
5265
  _min: this.makeMinMaxInputSchema(model).optional(),
5179
5266
  _max: this.makeMinMaxInputSchema(model).optional()
5180
- }).strict().optional();
5267
+ }).optional();
5181
5268
  }
5182
5269
  makeSumAvgInputSchema(model) {
5183
5270
  const modelDef = requireModel(this.schema, model);
5184
- return z.object(Object.keys(modelDef.fields).reduce((acc, field) => {
5271
+ return z.strictObject(Object.keys(modelDef.fields).reduce((acc, field) => {
5185
5272
  const fieldDef = requireField(this.schema, model, field);
5186
5273
  if (this.isNumericField(fieldDef)) {
5187
5274
  acc[field] = z.literal(true).optional();
@@ -5191,7 +5278,7 @@ var InputValidator = class {
5191
5278
  }
5192
5279
  makeMinMaxInputSchema(model) {
5193
5280
  const modelDef = requireModel(this.schema, model);
5194
- return z.object(Object.keys(modelDef.fields).reduce((acc, field) => {
5281
+ return z.strictObject(Object.keys(modelDef.fields).reduce((acc, field) => {
5195
5282
  const fieldDef = requireField(this.schema, model, field);
5196
5283
  if (!fieldDef.relation && !fieldDef.array) {
5197
5284
  acc[field] = z.literal(true).optional();
@@ -5207,14 +5294,14 @@ var InputValidator = class {
5207
5294
  orderBy: this.orArray(this.makeOrderBySchema(model, false, true), true).optional(),
5208
5295
  by: this.orArray(z.enum(nonRelationFields), true),
5209
5296
  having: this.makeWhereSchema(model, false, true).optional(),
5210
- skip: z.number().int().nonnegative().optional(),
5211
- take: z.number().int().optional(),
5297
+ skip: this.makeSkipSchema().optional(),
5298
+ take: this.makeTakeSchema().optional(),
5212
5299
  _count: this.makeCountAggregateInputSchema(model).optional(),
5213
5300
  _avg: this.makeSumAvgInputSchema(model).optional(),
5214
5301
  _sum: this.makeSumAvgInputSchema(model).optional(),
5215
5302
  _min: this.makeMinMaxInputSchema(model).optional(),
5216
5303
  _max: this.makeMinMaxInputSchema(model).optional()
5217
- }).strict();
5304
+ });
5218
5305
  schema = schema.refine((value) => {
5219
5306
  const bys = typeof value.by === "string" ? [
5220
5307
  value.by
@@ -5239,6 +5326,12 @@ var InputValidator = class {
5239
5326
  }
5240
5327
  // #endregion
5241
5328
  // #region Helpers
5329
+ makeSkipSchema() {
5330
+ return z.number().int().nonnegative();
5331
+ }
5332
+ makeTakeSchema() {
5333
+ return z.number().int();
5334
+ }
5242
5335
  refineForSelectIncludeMutuallyExclusive(schema) {
5243
5336
  return schema.refine((value) => !(value["select"] && value["include"]), '"select" and "include" cannot be used together');
5244
5337
  }
@@ -5257,6 +5350,9 @@ var InputValidator = class {
5257
5350
  isNumericField(fieldDef) {
5258
5351
  return NUMERIC_FIELD_TYPES.includes(fieldDef.type) && !fieldDef.array;
5259
5352
  }
5353
+ get providerSupportsCaseSensitivity() {
5354
+ return this.schema.provider.type === "postgresql";
5355
+ }
5260
5356
  };
5261
5357
 
5262
5358
  // src/client/executor/zenstack-driver.ts