@zenstackhq/orm 3.5.4 → 3.5.5

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
@@ -1096,6 +1096,9 @@ var BaseCrudDialect = class {
1096
1096
  return this.buildEnumFilter(fieldRef, fieldDef, payload);
1097
1097
  }
1098
1098
  if (isTypeDef(this.schema, fieldDef.type)) {
1099
+ if (payload instanceof DbNullClass || payload instanceof JsonNullClass || payload instanceof AnyNullClass) {
1100
+ return this.buildJsonValueFilterClause(fieldRef, payload);
1101
+ }
1099
1102
  return this.buildJsonFilter(fieldRef, payload, fieldDef);
1100
1103
  }
1101
1104
  return (0, import_ts_pattern2.match)(fieldDef.type).with("String", () => this.buildStringFilter(fieldRef, payload)).with(import_ts_pattern2.P.union("Int", "Float", "Decimal", "BigInt"), (type) => this.buildNumberFilter(fieldRef, type, payload)).with("Boolean", () => this.buildBooleanFilter(fieldRef, payload)).with("DateTime", () => this.buildDateTimeFilter(fieldRef, payload)).with("Bytes", () => this.buildBytesFilter(fieldRef, payload)).with("Json", () => this.buildJsonFilter(fieldRef, payload, fieldDef)).with("Unsupported", () => {
@@ -1573,12 +1576,13 @@ var BaseCrudDialect = class {
1573
1576
  const fieldDef = requireField(this.schema, model, field);
1574
1577
  const fieldModel = fieldDef.type;
1575
1578
  let fieldCountQuery;
1579
+ const subQueryAlias = tmpAlias(`${parentAlias}$_${field}$count`);
1576
1580
  const m2m = getManyToManyRelation(this.schema, model, field);
1577
1581
  if (m2m) {
1578
- fieldCountQuery = this.buildModelSelect(fieldModel, fieldModel, value, false).innerJoin(m2m.joinTable, (join) => join.onRef(`${m2m.joinTable}.${m2m.otherFkName}`, "=", `${fieldModel}.${m2m.otherPKName}`).onRef(`${m2m.joinTable}.${m2m.parentFkName}`, "=", `${parentAlias}.${m2m.parentPKName}`)).select(eb.fn.countAll().as(`_count$${field}`));
1582
+ fieldCountQuery = this.buildModelSelect(fieldModel, subQueryAlias, value, false).innerJoin(m2m.joinTable, (join) => join.onRef(`${m2m.joinTable}.${m2m.otherFkName}`, "=", `${subQueryAlias}.${m2m.otherPKName}`).onRef(`${m2m.joinTable}.${m2m.parentFkName}`, "=", `${parentAlias}.${m2m.parentPKName}`)).select(eb.fn.countAll().as(`_count$${field}`));
1579
1583
  } else {
1580
- fieldCountQuery = this.buildModelSelect(fieldModel, fieldModel, value, false).select(eb.fn.countAll().as(`_count$${field}`));
1581
- const joinPairs = buildJoinPairs(this.schema, model, parentAlias, field, fieldModel);
1584
+ fieldCountQuery = this.buildModelSelect(fieldModel, subQueryAlias, value, false).select(eb.fn.countAll().as(`_count$${field}`));
1585
+ const joinPairs = buildJoinPairs(this.schema, model, parentAlias, field, subQueryAlias);
1582
1586
  for (const [left, right] of joinPairs) {
1583
1587
  fieldCountQuery = fieldCountQuery.whereRef(left, "=", right);
1584
1588
  }
@@ -2331,10 +2335,20 @@ var PostgresCrudDialect = class _PostgresCrudDialect extends LateralJoinDialectB
2331
2335
  return query;
2332
2336
  }
2333
2337
  buildJsonObject(value) {
2334
- return this.eb.fn("jsonb_build_object", Object.entries(value).flatMap(([key, value2]) => [
2335
- import_kysely4.sql.lit(key),
2336
- value2
2337
- ]));
2338
+ const entries = Object.entries(value);
2339
+ const MAX_PAIRS = 50;
2340
+ const buildChunk = /* @__PURE__ */ __name((chunk) => this.eb.fn("jsonb_build_object", chunk.flatMap(([k, v]) => [
2341
+ import_kysely4.sql.lit(k),
2342
+ v
2343
+ ])), "buildChunk");
2344
+ if (entries.length <= MAX_PAIRS) {
2345
+ return buildChunk(entries);
2346
+ }
2347
+ const chunks = [];
2348
+ for (let i = 0; i < entries.length; i += MAX_PAIRS) {
2349
+ chunks.push(buildChunk(entries.slice(i, i + MAX_PAIRS)));
2350
+ }
2351
+ return chunks.reduce((acc, chunk) => import_kysely4.sql`${acc} || ${chunk}`);
2338
2352
  }
2339
2353
  castInt(expression) {
2340
2354
  return this.eb.cast(expression, "integer");
@@ -5427,9 +5441,28 @@ var ZodSchemaFactory = class {
5427
5441
  candidates.push(this.makeJsonFilterSchema(contextModel, field, optional));
5428
5442
  if (optional) {
5429
5443
  candidates.push(import_zod2.z.null());
5444
+ candidates.push(import_zod2.z.instanceof(DbNullClass));
5445
+ candidates.push(import_zod2.z.instanceof(JsonNullClass));
5446
+ candidates.push(import_zod2.z.instanceof(AnyNullClass));
5430
5447
  }
5431
5448
  return import_zod2.z.union(candidates);
5432
5449
  }
5450
+ // For optional typed JSON fields, allow DbNull, JsonNull, and null.
5451
+ // z.union doesn't work here because `z.any()` (returned by `makeScalarSchema`)
5452
+ // always wins, so we create a wrapper superRefine instead.
5453
+ // The caller must pass the already-built fieldSchema so that array/list
5454
+ // mutation shapes (set, push, etc.) are preserved.
5455
+ makeNullableTypedJsonMutationSchema(fieldSchema) {
5456
+ return import_zod2.z.any().superRefine((value, ctx) => {
5457
+ if (value instanceof DbNullClass || value instanceof JsonNullClass || value === null || value === void 0) {
5458
+ return;
5459
+ }
5460
+ const parseResult = fieldSchema.safeParse(value);
5461
+ if (!parseResult.success) {
5462
+ parseResult.error.issues.forEach((issue) => ctx.addIssue(issue));
5463
+ }
5464
+ }).optional();
5465
+ }
5433
5466
  isTypeDefType(type) {
5434
5467
  return this.schema.typeDefs && type in this.schema.typeDefs;
5435
5468
  }
@@ -5921,6 +5954,8 @@ var ZodSchemaFactory = class {
5921
5954
  fieldSchema,
5922
5955
  import_zod2.z.instanceof(DbNullClass)
5923
5956
  ]);
5957
+ } else if (this.isTypeDefType(fieldDef.type)) {
5958
+ fieldSchema = this.makeNullableTypedJsonMutationSchema(fieldSchema);
5924
5959
  } else {
5925
5960
  fieldSchema = fieldSchema.nullable();
5926
5961
  }
@@ -6157,6 +6192,8 @@ var ZodSchemaFactory = class {
6157
6192
  fieldSchema,
6158
6193
  import_zod2.z.instanceof(DbNullClass)
6159
6194
  ]);
6195
+ } else if (this.isTypeDefType(fieldDef.type)) {
6196
+ fieldSchema = this.makeNullableTypedJsonMutationSchema(fieldSchema);
6160
6197
  } else {
6161
6198
  fieldSchema = fieldSchema.nullable();
6162
6199
  }