@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.d.cts CHANGED
@@ -1431,7 +1431,7 @@ type JsonFilter<AllowedKinds extends FilterKind = FilterKind> = ('Equality' exte
1431
1431
  array_ends_with?: JsonValue;
1432
1432
  } : {});
1433
1433
  type TypedJsonFilter<Schema extends SchemaDef, TypeDefName extends GetTypeDefs<Schema>, Array extends boolean, Optional extends boolean, AllowedKinds extends FilterKind> = XOR<JsonFilter<AllowedKinds>, TypedJsonTypedFilter<Schema, TypeDefName, Array, Optional, AllowedKinds>>;
1434
- type TypedJsonTypedFilter<Schema extends SchemaDef, TypeDefName extends GetTypeDefs<Schema>, Array extends boolean, Optional extends boolean, AllowedKinds extends FilterKind> = 'Json' extends AllowedKinds ? (Array extends true ? ArrayTypedJsonFilter<Schema, TypeDefName, AllowedKinds> : NonArrayTypedJsonFilter<Schema, TypeDefName, AllowedKinds>) | (Optional extends true ? null : never) : {};
1434
+ type TypedJsonTypedFilter<Schema extends SchemaDef, TypeDefName extends GetTypeDefs<Schema>, Array extends boolean, Optional extends boolean, AllowedKinds extends FilterKind> = 'Json' extends AllowedKinds ? (Array extends true ? ArrayTypedJsonFilter<Schema, TypeDefName, AllowedKinds> : NonArrayTypedJsonFilter<Schema, TypeDefName, AllowedKinds>) | (Optional extends true ? null | JsonNullValues : never) : {};
1435
1435
  type ArrayTypedJsonFilter<Schema extends SchemaDef, TypeDefName extends GetTypeDefs<Schema>, AllowedKinds extends FilterKind> = {
1436
1436
  some?: TypedJsonFieldsFilter<Schema, TypeDefName, AllowedKinds>;
1437
1437
  every?: TypedJsonFieldsFilter<Schema, TypeDefName, AllowedKinds>;
@@ -1610,8 +1610,9 @@ type CreateScalarPayload<Schema extends SchemaDef, Model extends GetModels<Schem
1610
1610
  type ScalarCreatePayload<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends ScalarFields<Schema, Model, false>> = ScalarFieldMutationPayload<Schema, Model, Field> | (FieldIsArray<Schema, Model, Field> extends true ? {
1611
1611
  set?: MapModelFieldType<Schema, Model, Field>;
1612
1612
  } : never);
1613
- type ScalarFieldMutationPayload<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetModelFields<Schema, Model>> = IsJsonField<Schema, Model, Field> extends true ? ModelFieldIsOptional<Schema, Model, Field> extends true ? JsonValue | JsonNull | DbNull : JsonValue | JsonNull : MapModelFieldType<Schema, Model, Field>;
1613
+ type ScalarFieldMutationPayload<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetModelFields<Schema, Model>> = IsJsonField<Schema, Model, Field> extends true ? ModelFieldIsOptional<Schema, Model, Field> extends true ? JsonValue | JsonNull | DbNull : JsonValue | JsonNull : IsTypedJsonField<Schema, Model, Field> extends true ? ModelFieldIsOptional<Schema, Model, Field> extends true ? MapModelFieldType<Schema, Model, Field> | JsonNull | DbNull : MapModelFieldType<Schema, Model, Field> : MapModelFieldType<Schema, Model, Field>;
1614
1614
  type IsJsonField<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetModelFields<Schema, Model>> = GetModelFieldType<Schema, Model, Field> extends 'Json' ? true : false;
1615
+ type IsTypedJsonField<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetModelFields<Schema, Model>> = GetModelFieldType<Schema, Model, Field> extends GetTypeDefs<Schema> ? true : false;
1615
1616
  type CreateFKPayload<Schema extends SchemaDef, Model extends GetModels<Schema>> = OptionalWrap<Schema, Model, {
1616
1617
  [Key in ForeignKeyFields<Schema, Model>]: MapModelFieldType<Schema, Model, Key>;
1617
1618
  }>;
@@ -3013,6 +3014,7 @@ declare class ZodSchemaFactory<Schema extends SchemaDef, Options extends ClientO
3013
3014
  private makeTypeDefSchema;
3014
3015
  makeWhereSchema(model: string, unique: boolean, withoutRelationFields?: boolean, withAggregations?: boolean, options?: CreateSchemaOptions): ZodType;
3015
3016
  private makeTypedJsonFilterSchema;
3017
+ private makeNullableTypedJsonMutationSchema;
3016
3018
  private isTypeDefType;
3017
3019
  private makeEnumFilterSchema;
3018
3020
  private makeArrayFilterSchema;
package/dist/index.d.ts CHANGED
@@ -1431,7 +1431,7 @@ type JsonFilter<AllowedKinds extends FilterKind = FilterKind> = ('Equality' exte
1431
1431
  array_ends_with?: JsonValue;
1432
1432
  } : {});
1433
1433
  type TypedJsonFilter<Schema extends SchemaDef, TypeDefName extends GetTypeDefs<Schema>, Array extends boolean, Optional extends boolean, AllowedKinds extends FilterKind> = XOR<JsonFilter<AllowedKinds>, TypedJsonTypedFilter<Schema, TypeDefName, Array, Optional, AllowedKinds>>;
1434
- type TypedJsonTypedFilter<Schema extends SchemaDef, TypeDefName extends GetTypeDefs<Schema>, Array extends boolean, Optional extends boolean, AllowedKinds extends FilterKind> = 'Json' extends AllowedKinds ? (Array extends true ? ArrayTypedJsonFilter<Schema, TypeDefName, AllowedKinds> : NonArrayTypedJsonFilter<Schema, TypeDefName, AllowedKinds>) | (Optional extends true ? null : never) : {};
1434
+ type TypedJsonTypedFilter<Schema extends SchemaDef, TypeDefName extends GetTypeDefs<Schema>, Array extends boolean, Optional extends boolean, AllowedKinds extends FilterKind> = 'Json' extends AllowedKinds ? (Array extends true ? ArrayTypedJsonFilter<Schema, TypeDefName, AllowedKinds> : NonArrayTypedJsonFilter<Schema, TypeDefName, AllowedKinds>) | (Optional extends true ? null | JsonNullValues : never) : {};
1435
1435
  type ArrayTypedJsonFilter<Schema extends SchemaDef, TypeDefName extends GetTypeDefs<Schema>, AllowedKinds extends FilterKind> = {
1436
1436
  some?: TypedJsonFieldsFilter<Schema, TypeDefName, AllowedKinds>;
1437
1437
  every?: TypedJsonFieldsFilter<Schema, TypeDefName, AllowedKinds>;
@@ -1610,8 +1610,9 @@ type CreateScalarPayload<Schema extends SchemaDef, Model extends GetModels<Schem
1610
1610
  type ScalarCreatePayload<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends ScalarFields<Schema, Model, false>> = ScalarFieldMutationPayload<Schema, Model, Field> | (FieldIsArray<Schema, Model, Field> extends true ? {
1611
1611
  set?: MapModelFieldType<Schema, Model, Field>;
1612
1612
  } : never);
1613
- type ScalarFieldMutationPayload<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetModelFields<Schema, Model>> = IsJsonField<Schema, Model, Field> extends true ? ModelFieldIsOptional<Schema, Model, Field> extends true ? JsonValue | JsonNull | DbNull : JsonValue | JsonNull : MapModelFieldType<Schema, Model, Field>;
1613
+ type ScalarFieldMutationPayload<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetModelFields<Schema, Model>> = IsJsonField<Schema, Model, Field> extends true ? ModelFieldIsOptional<Schema, Model, Field> extends true ? JsonValue | JsonNull | DbNull : JsonValue | JsonNull : IsTypedJsonField<Schema, Model, Field> extends true ? ModelFieldIsOptional<Schema, Model, Field> extends true ? MapModelFieldType<Schema, Model, Field> | JsonNull | DbNull : MapModelFieldType<Schema, Model, Field> : MapModelFieldType<Schema, Model, Field>;
1614
1614
  type IsJsonField<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetModelFields<Schema, Model>> = GetModelFieldType<Schema, Model, Field> extends 'Json' ? true : false;
1615
+ type IsTypedJsonField<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetModelFields<Schema, Model>> = GetModelFieldType<Schema, Model, Field> extends GetTypeDefs<Schema> ? true : false;
1615
1616
  type CreateFKPayload<Schema extends SchemaDef, Model extends GetModels<Schema>> = OptionalWrap<Schema, Model, {
1616
1617
  [Key in ForeignKeyFields<Schema, Model>]: MapModelFieldType<Schema, Model, Key>;
1617
1618
  }>;
@@ -3013,6 +3014,7 @@ declare class ZodSchemaFactory<Schema extends SchemaDef, Options extends ClientO
3013
3014
  private makeTypeDefSchema;
3014
3015
  makeWhereSchema(model: string, unique: boolean, withoutRelationFields?: boolean, withAggregations?: boolean, options?: CreateSchemaOptions): ZodType;
3015
3016
  private makeTypedJsonFilterSchema;
3017
+ private makeNullableTypedJsonMutationSchema;
3016
3018
  private isTypeDefType;
3017
3019
  private makeEnumFilterSchema;
3018
3020
  private makeArrayFilterSchema;
package/dist/index.js CHANGED
@@ -1050,6 +1050,9 @@ var BaseCrudDialect = class {
1050
1050
  return this.buildEnumFilter(fieldRef, fieldDef, payload);
1051
1051
  }
1052
1052
  if (isTypeDef(this.schema, fieldDef.type)) {
1053
+ if (payload instanceof DbNullClass || payload instanceof JsonNullClass || payload instanceof AnyNullClass) {
1054
+ return this.buildJsonValueFilterClause(fieldRef, payload);
1055
+ }
1053
1056
  return this.buildJsonFilter(fieldRef, payload, fieldDef);
1054
1057
  }
1055
1058
  return match2(fieldDef.type).with("String", () => this.buildStringFilter(fieldRef, payload)).with(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", () => {
@@ -1527,12 +1530,13 @@ var BaseCrudDialect = class {
1527
1530
  const fieldDef = requireField(this.schema, model, field);
1528
1531
  const fieldModel = fieldDef.type;
1529
1532
  let fieldCountQuery;
1533
+ const subQueryAlias = tmpAlias(`${parentAlias}$_${field}$count`);
1530
1534
  const m2m = getManyToManyRelation(this.schema, model, field);
1531
1535
  if (m2m) {
1532
- 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}`));
1536
+ 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}`));
1533
1537
  } else {
1534
- fieldCountQuery = this.buildModelSelect(fieldModel, fieldModel, value, false).select(eb.fn.countAll().as(`_count$${field}`));
1535
- const joinPairs = buildJoinPairs(this.schema, model, parentAlias, field, fieldModel);
1538
+ fieldCountQuery = this.buildModelSelect(fieldModel, subQueryAlias, value, false).select(eb.fn.countAll().as(`_count$${field}`));
1539
+ const joinPairs = buildJoinPairs(this.schema, model, parentAlias, field, subQueryAlias);
1536
1540
  for (const [left, right] of joinPairs) {
1537
1541
  fieldCountQuery = fieldCountQuery.whereRef(left, "=", right);
1538
1542
  }
@@ -2285,10 +2289,20 @@ var PostgresCrudDialect = class _PostgresCrudDialect extends LateralJoinDialectB
2285
2289
  return query;
2286
2290
  }
2287
2291
  buildJsonObject(value) {
2288
- return this.eb.fn("jsonb_build_object", Object.entries(value).flatMap(([key, value2]) => [
2289
- sql3.lit(key),
2290
- value2
2291
- ]));
2292
+ const entries = Object.entries(value);
2293
+ const MAX_PAIRS = 50;
2294
+ const buildChunk = /* @__PURE__ */ __name((chunk) => this.eb.fn("jsonb_build_object", chunk.flatMap(([k, v]) => [
2295
+ sql3.lit(k),
2296
+ v
2297
+ ])), "buildChunk");
2298
+ if (entries.length <= MAX_PAIRS) {
2299
+ return buildChunk(entries);
2300
+ }
2301
+ const chunks = [];
2302
+ for (let i = 0; i < entries.length; i += MAX_PAIRS) {
2303
+ chunks.push(buildChunk(entries.slice(i, i + MAX_PAIRS)));
2304
+ }
2305
+ return chunks.reduce((acc, chunk) => sql3`${acc} || ${chunk}`);
2292
2306
  }
2293
2307
  castInt(expression) {
2294
2308
  return this.eb.cast(expression, "integer");
@@ -5381,9 +5395,28 @@ var ZodSchemaFactory = class {
5381
5395
  candidates.push(this.makeJsonFilterSchema(contextModel, field, optional));
5382
5396
  if (optional) {
5383
5397
  candidates.push(z.null());
5398
+ candidates.push(z.instanceof(DbNullClass));
5399
+ candidates.push(z.instanceof(JsonNullClass));
5400
+ candidates.push(z.instanceof(AnyNullClass));
5384
5401
  }
5385
5402
  return z.union(candidates);
5386
5403
  }
5404
+ // For optional typed JSON fields, allow DbNull, JsonNull, and null.
5405
+ // z.union doesn't work here because `z.any()` (returned by `makeScalarSchema`)
5406
+ // always wins, so we create a wrapper superRefine instead.
5407
+ // The caller must pass the already-built fieldSchema so that array/list
5408
+ // mutation shapes (set, push, etc.) are preserved.
5409
+ makeNullableTypedJsonMutationSchema(fieldSchema) {
5410
+ return z.any().superRefine((value, ctx) => {
5411
+ if (value instanceof DbNullClass || value instanceof JsonNullClass || value === null || value === void 0) {
5412
+ return;
5413
+ }
5414
+ const parseResult = fieldSchema.safeParse(value);
5415
+ if (!parseResult.success) {
5416
+ parseResult.error.issues.forEach((issue) => ctx.addIssue(issue));
5417
+ }
5418
+ }).optional();
5419
+ }
5387
5420
  isTypeDefType(type) {
5388
5421
  return this.schema.typeDefs && type in this.schema.typeDefs;
5389
5422
  }
@@ -5875,6 +5908,8 @@ var ZodSchemaFactory = class {
5875
5908
  fieldSchema,
5876
5909
  z.instanceof(DbNullClass)
5877
5910
  ]);
5911
+ } else if (this.isTypeDefType(fieldDef.type)) {
5912
+ fieldSchema = this.makeNullableTypedJsonMutationSchema(fieldSchema);
5878
5913
  } else {
5879
5914
  fieldSchema = fieldSchema.nullable();
5880
5915
  }
@@ -6111,6 +6146,8 @@ var ZodSchemaFactory = class {
6111
6146
  fieldSchema,
6112
6147
  z.instanceof(DbNullClass)
6113
6148
  ]);
6149
+ } else if (this.isTypeDefType(fieldDef.type)) {
6150
+ fieldSchema = this.makeNullableTypedJsonMutationSchema(fieldSchema);
6114
6151
  } else {
6115
6152
  fieldSchema = fieldSchema.nullable();
6116
6153
  }