@zenstackhq/orm 3.10.0 → 3.10.1

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.cjs CHANGED
@@ -2436,6 +2436,20 @@ function createQuerySchemaFactory(clientOrSchema, options) {
2436
2436
  return new ZodSchemaFactory(clientOrSchema, options);
2437
2437
  }
2438
2438
  /**
2439
+ * Whether two client options produce the same query-args schemas, i.e. a `ZodSchemaFactory`
2440
+ * built for one can serve the other. Only `omit`, `slicing`, `allowQueryTimeOmitOverride` and
2441
+ * the plugins that extend query args or results shape the schemas; dialect, auth, hooks and
2442
+ * validation flags do not.
2443
+ */
2444
+ function haveSameSchemaInputs(a, b) {
2445
+ if (a === b) return true;
2446
+ if (a.omit !== b.omit || a.slicing !== b.slicing || a.allowQueryTimeOmitOverride !== b.allowQueryTimeOmitOverride) return false;
2447
+ const shaping = (options) => (options.plugins ?? []).filter((plugin) => plugin.queryArgs || plugin.result);
2448
+ const pa = shaping(a);
2449
+ const pb = shaping(b);
2450
+ return pa.length === pb.length && pa.every((plugin, index) => plugin === pb[index]);
2451
+ }
2452
+ /**
2439
2453
  * Builds a `DateTime` value schema that accepts a `Date` object or any string
2440
2454
  * the JS `Date` constructor parses, and coerces it to a `Date`. ISO datetime,
2441
2455
  * ISO date, and time-only strings (e.g. `"09:00:00"` for `@db.Time` fields,
@@ -2660,12 +2674,13 @@ var ZodSchemaFactory = class {
2660
2674
  const schema = this.makeScalarSchema(typeDef.base, [...typeDef.fields["this"]?.attributes ?? [], ...fieldAttributes ?? []]);
2661
2675
  return this.extraValidationsEnabled ? _zenstackhq_zod.ZodUtils.addCustomValidation(schema, typeDef.attributes) : schema;
2662
2676
  }
2663
- const schema = (typeDef.strict ? zod.z.strictObject : zod.z.looseObject)(Object.fromEntries(Object.entries(typeDef.fields).map(([field, def]) => {
2664
- let fieldSchema = isTypeDef(this.schema, def.type) ? zod.z.lazy(() => this.makeTypeDefSchema(def.type)) : this.makeScalarSchema(def.type);
2665
- if (def.array) fieldSchema = fieldSchema.array();
2677
+ const objectSchema = (typeDef.strict ? zod.z.strictObject : zod.z.looseObject)(Object.fromEntries(Object.entries(typeDef.fields).map(([field, def]) => {
2678
+ let fieldSchema = isTypeDef(this.schema, def.type) && !isPrimitiveTypeDef(this.schema, def.type) ? zod.z.lazy(() => this.makeTypeDefSchema(def.type)) : this.makeScalarSchema(def.type, this.getItemAttributes(def));
2679
+ if (def.array) fieldSchema = _zenstackhq_zod.ZodUtils.addListValidation(fieldSchema.array(), def.attributes);
2666
2680
  if (def.optional) fieldSchema = fieldSchema.nullish();
2667
2681
  return [field, fieldSchema];
2668
2682
  })));
2683
+ const schema = this.extraValidationsEnabled ? _zenstackhq_zod.ZodUtils.addCustomValidation(objectSchema, typeDef.attributes) : objectSchema;
2669
2684
  const finalSchema = zod.z.any().superRefine((value, ctx) => {
2670
2685
  const parseResult = schema.safeParse(value);
2671
2686
  if (!parseResult.success) parseResult.error.issues.forEach((issue) => ctx.addIssue(issue));
@@ -4217,7 +4232,7 @@ var InputValidator = class {
4217
4232
  enabled;
4218
4233
  constructor(client, options) {
4219
4234
  this.client = client;
4220
- this.zodFactory = new ZodSchemaFactory(client);
4235
+ this.zodFactory = options?.zodFactory ?? new ZodSchemaFactory(client);
4221
4236
  this.enabled = options?.enabled !== false;
4222
4237
  }
4223
4238
  validateFindArgs(model, args, operation) {
@@ -4370,6 +4385,7 @@ var BaseOperationHandler = class {
4370
4385
  try {
4371
4386
  result = (await kysely$4.getExecutor().executeQuery(compiled)).rows;
4372
4387
  } catch (err) {
4388
+ if (err instanceof ORMError) throw err;
4373
4389
  throw createDBQueryError(`Failed to execute query: ${err}`, err, compiled.sql, compiled.parameters);
4374
4390
  }
4375
4391
  return !!result[0]?.$exists;
@@ -4389,6 +4405,7 @@ var BaseOperationHandler = class {
4389
4405
  try {
4390
4406
  result = (await kysely$5.getExecutor().executeQuery(compiled)).rows;
4391
4407
  } catch (err) {
4408
+ if (err instanceof ORMError) throw err;
4392
4409
  throw createDBQueryError(`Failed to execute query: ${err}`, err, compiled.sql, compiled.parameters);
4393
4410
  }
4394
4411
  return result;
@@ -6800,6 +6817,7 @@ var ZenStackQueryExecutor = class ZenStackQueryExecutor extends kysely.DefaultQu
6800
6817
  if (this.dialect.supportsReturning) return queryResult.rows.map((row) => ({ ...row }));
6801
6818
  else {
6802
6819
  const mutatedIds = kysely.InsertQueryNode.is(queryNode) ? this.getInsertIds(mutationInfo.model, queryNode, queryResult) : preUpdateIds;
6820
+ if (mutatedIds && mutatedIds.length === 0) return [];
6803
6821
  if (mutatedIds) {
6804
6822
  const idFields = requireIdFields(this.client.$schema, mutationInfo.model);
6805
6823
  const eb = (0, kysely.expressionBuilder)();
@@ -7718,7 +7736,7 @@ var ClientImpl = class ClientImpl {
7718
7736
  plugins: newPlugins
7719
7737
  };
7720
7738
  const newClient = new ClientImpl(this.schema, newOptions, this);
7721
- newClient.inputValidator = new InputValidator(newClient, { enabled: newOptions.validateInput !== false });
7739
+ newClient.inputValidator = this.deriveInputValidator(newClient);
7722
7740
  return newClient;
7723
7741
  }
7724
7742
  $unuse(pluginId) {
@@ -7729,7 +7747,7 @@ var ClientImpl = class ClientImpl {
7729
7747
  plugins: newPlugins
7730
7748
  };
7731
7749
  const newClient = new ClientImpl(this.schema, newOptions, this);
7732
- newClient.inputValidator = new InputValidator(newClient, { enabled: newClient.$options.validateInput !== false });
7750
+ newClient.inputValidator = this.deriveInputValidator(newClient);
7733
7751
  return newClient;
7734
7752
  }
7735
7753
  $unuseAll() {
@@ -7738,9 +7756,22 @@ var ClientImpl = class ClientImpl {
7738
7756
  plugins: []
7739
7757
  };
7740
7758
  const newClient = new ClientImpl(this.schema, newOptions, this);
7741
- newClient.inputValidator = new InputValidator(newClient, { enabled: newOptions.validateInput !== false });
7759
+ newClient.inputValidator = this.deriveInputValidator(newClient);
7742
7760
  return newClient;
7743
7761
  }
7762
+ /**
7763
+ * Validator for a client derived from this one. The derived client reuses this client's Zod
7764
+ * schema factory, and so its schema cache, whenever its options shape the same query-args
7765
+ * schemas; it only gets a fresh factory when a plugin that extends query args or results was
7766
+ * added or removed, or `omit`/`slicing` changed. The validator itself is always new because
7767
+ * `validateInput` may differ.
7768
+ */
7769
+ deriveInputValidator(newClient) {
7770
+ return new InputValidator(newClient, {
7771
+ enabled: newClient.$options.validateInput !== false,
7772
+ zodFactory: haveSameSchemaInputs(this.$options, newClient.$options) ? this.inputValidator.zodFactory : void 0
7773
+ });
7774
+ }
7744
7775
  $setAuth(auth) {
7745
7776
  if (auth !== void 0 && (typeof auth !== "object" || auth === null || Array.isArray(auth))) throw new Error("Invalid auth object");
7746
7777
  const newClient = new ClientImpl(this.schema, this.$options, this);
@@ -7762,7 +7793,7 @@ var ClientImpl = class ClientImpl {
7762
7793
  }
7763
7794
  $setOptions(options) {
7764
7795
  const newClient = new ClientImpl(this.schema, options, this);
7765
- newClient.inputValidator = new InputValidator(newClient, { enabled: newClient.$options.validateInput !== false });
7796
+ newClient.inputValidator = this.deriveInputValidator(newClient);
7766
7797
  return newClient;
7767
7798
  }
7768
7799
  $setInputValidation(enable) {