@zenstackhq/orm 3.5.2 → 3.5.3

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
@@ -908,6 +908,10 @@ declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
908
908
  * Builds a VALUES table and select all fields from it.
909
909
  */
910
910
  abstract buildValuesTableSelect(fields: FieldDef[], rows: unknown[][]): SelectQueryBuilder<any, any, any>;
911
+ /**
912
+ * Builds a binary comparison expression between two operands.
913
+ */
914
+ buildComparison(left: Expression<unknown>, _leftFieldDef: FieldDef | undefined, op: string, right: Expression<unknown>, _rightFieldDef: FieldDef | undefined): Expression<SqlBool>;
911
915
  /**
912
916
  * Builds a JSON path selection expression.
913
917
  */
package/dist/index.d.ts CHANGED
@@ -908,6 +908,10 @@ declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
908
908
  * Builds a VALUES table and select all fields from it.
909
909
  */
910
910
  abstract buildValuesTableSelect(fields: FieldDef[], rows: unknown[][]): SelectQueryBuilder<any, any, any>;
911
+ /**
912
+ * Builds a binary comparison expression between two operands.
913
+ */
914
+ buildComparison(left: Expression<unknown>, _leftFieldDef: FieldDef | undefined, op: string, right: Expression<unknown>, _rightFieldDef: FieldDef | undefined): Expression<SqlBool>;
911
915
  /**
912
916
  * Builds a JSON path selection expression.
913
917
  */
package/dist/index.js CHANGED
@@ -1597,6 +1597,12 @@ var BaseCrudDialect = class {
1597
1597
  buildExistsExpression(innerQuery) {
1598
1598
  return this.eb.exists(innerQuery);
1599
1599
  }
1600
+ /**
1601
+ * Builds a binary comparison expression between two operands.
1602
+ */
1603
+ buildComparison(left, _leftFieldDef, op, right, _rightFieldDef) {
1604
+ return this.eb(left, op, right);
1605
+ }
1600
1606
  };
1601
1607
 
1602
1608
  // src/client/crud/dialects/lateral-join-dialect-base.ts
@@ -2031,6 +2037,33 @@ var PostgresCrudDialect = class _PostgresCrudDialect extends LateralJoinDialectB
2031
2037
  Bytes: "bytea",
2032
2038
  Json: "jsonb"
2033
2039
  };
2040
+ // Maps @db.* attribute names to PostgreSQL SQL types for use in VALUES table casts
2041
+ static dbAttributeToSqlTypeMap = {
2042
+ "@db.Uuid": "uuid",
2043
+ "@db.Citext": "citext",
2044
+ "@db.Inet": "inet",
2045
+ "@db.Bit": "bit",
2046
+ "@db.VarBit": "varbit",
2047
+ "@db.Xml": "xml",
2048
+ "@db.Json": "json",
2049
+ "@db.JsonB": "jsonb",
2050
+ "@db.ByteA": "bytea",
2051
+ "@db.Text": "text",
2052
+ "@db.Char": "bpchar",
2053
+ "@db.VarChar": "varchar",
2054
+ "@db.Date": "date",
2055
+ "@db.Time": "time",
2056
+ "@db.Timetz": "timetz",
2057
+ "@db.Timestamp": "timestamp",
2058
+ "@db.Timestamptz": "timestamptz",
2059
+ "@db.SmallInt": "smallint",
2060
+ "@db.Integer": "integer",
2061
+ "@db.BigInt": "bigint",
2062
+ "@db.Real": "real",
2063
+ "@db.DoublePrecision": "double precision",
2064
+ "@db.Decimal": "decimal",
2065
+ "@db.Boolean": "boolean"
2066
+ };
2034
2067
  constructor(schema, options) {
2035
2068
  super(schema, options);
2036
2069
  this.overrideTypeParsers();
@@ -2295,13 +2328,55 @@ var PostgresCrudDialect = class _PostgresCrudDialect extends LateralJoinDialectB
2295
2328
  receiver
2296
2329
  ]).as("$items")).select(this.eb.lit(1).as("_")).where(buildFilter(this.eb.ref("$items.value"))));
2297
2330
  }
2298
- getSqlType(zmodelType) {
2331
+ getSqlType(zmodelType, attributes) {
2332
+ if (attributes) {
2333
+ for (const attr of attributes) {
2334
+ const mapped = _PostgresCrudDialect.dbAttributeToSqlTypeMap[attr.name];
2335
+ if (mapped) {
2336
+ return mapped;
2337
+ }
2338
+ }
2339
+ }
2299
2340
  if (isEnum(this.schema, zmodelType)) {
2300
2341
  return "text";
2301
2342
  } else {
2302
2343
  return this.zmodelToSqlTypeMap[zmodelType] ?? "text";
2303
2344
  }
2304
2345
  }
2346
+ // Resolves the effective SQL type for a field: the native type from any @db.* attribute,
2347
+ // or the base ZModel SQL type if no attribute is present, or undefined if the field is unknown.
2348
+ resolveFieldSqlType(fieldDef) {
2349
+ if (!fieldDef) {
2350
+ return {
2351
+ sqlType: void 0,
2352
+ hasDbOverride: false
2353
+ };
2354
+ }
2355
+ const dbAttr = fieldDef.attributes?.find((a) => a.name.startsWith("@db."));
2356
+ if (dbAttr) {
2357
+ return {
2358
+ sqlType: _PostgresCrudDialect.dbAttributeToSqlTypeMap[dbAttr.name],
2359
+ hasDbOverride: true
2360
+ };
2361
+ }
2362
+ return {
2363
+ sqlType: this.getSqlType(fieldDef.type),
2364
+ hasDbOverride: false
2365
+ };
2366
+ }
2367
+ buildComparison(left, leftFieldDef, op, right, rightFieldDef) {
2368
+ const leftResolved = this.resolveFieldSqlType(leftFieldDef);
2369
+ const rightResolved = this.resolveFieldSqlType(rightFieldDef);
2370
+ if (leftResolved.sqlType !== rightResolved.sqlType && (leftResolved.hasDbOverride || rightResolved.hasDbOverride)) {
2371
+ if (leftResolved.hasDbOverride) {
2372
+ left = this.eb.cast(left, sql3.raw(this.getSqlType(leftFieldDef.type)));
2373
+ }
2374
+ if (rightResolved.hasDbOverride) {
2375
+ right = this.eb.cast(right, sql3.raw(this.getSqlType(rightFieldDef.type)));
2376
+ }
2377
+ }
2378
+ return super.buildComparison(left, leftFieldDef, op, right, rightFieldDef);
2379
+ }
2305
2380
  getStringCasingBehavior() {
2306
2381
  return {
2307
2382
  supportsILike: true,
@@ -2323,7 +2398,7 @@ var PostgresCrudDialect = class _PostgresCrudDialect extends LateralJoinDialectB
2323
2398
  }
2324
2399
  const eb = expressionBuilder3();
2325
2400
  return eb.selectFrom(sql3`(VALUES ${sql3.join(rows.map((row) => sql3`(${sql3.join(row.map((v) => sql3.val(v)))})`), sql3.raw(", "))})`.as("$values")).select(fields.map((f, i) => {
2326
- const mappedType = this.getSqlType(f.type);
2401
+ const mappedType = this.getSqlType(f.type, f.attributes);
2327
2402
  const castType = f.array ? sql3`${sql3.raw(mappedType)}[]` : sql3.raw(mappedType);
2328
2403
  return this.eb.cast(sql3.ref(`$values.column${i + 1}`), castType).as(f.name);
2329
2404
  }));