@zenstackhq/orm 3.4.4 → 3.4.6

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
@@ -977,21 +977,22 @@ var BaseCrudDialect = class {
977
977
  if (!subPayload) {
978
978
  continue;
979
979
  }
980
- const countSelect = /* @__PURE__ */ __name((negate) => {
980
+ const existsSelect = /* @__PURE__ */ __name((negate) => {
981
981
  const filter = this.buildFilter(relationModel, relationFilterSelectAlias, subPayload);
982
- return this.eb.selectFrom(this.buildSelectModel(relationModel, relationFilterSelectAlias).select(() => this.eb.fn.count(this.eb.lit(1)).as("$count")).where(buildPkFkWhereRefs(this.eb)).where(() => negate ? this.eb.not(filter) : filter).as("$sub")).select("$count");
983
- }, "countSelect");
982
+ const innerQuery = this.buildSelectModel(relationModel, relationFilterSelectAlias).select(this.eb.lit(1).as("_")).where(buildPkFkWhereRefs(this.eb)).where(() => negate ? this.eb.not(filter) : filter);
983
+ return this.buildExistsExpression(innerQuery);
984
+ }, "existsSelect");
984
985
  switch (key) {
985
986
  case "some": {
986
- result = this.and(result, this.eb(countSelect(false), ">", 0));
987
+ result = this.and(result, existsSelect(false));
987
988
  break;
988
989
  }
989
990
  case "every": {
990
- result = this.and(result, this.eb(countSelect(true), "=", 0));
991
+ result = this.and(result, this.eb.not(existsSelect(true)));
991
992
  break;
992
993
  }
993
994
  case "none": {
994
- result = this.and(result, this.eb(countSelect(false), "=", 0));
995
+ result = this.and(result, this.eb.not(existsSelect(false)));
995
996
  break;
996
997
  }
997
998
  }
@@ -1216,16 +1217,22 @@ var BaseCrudDialect = class {
1216
1217
  }
1217
1218
  }
1218
1219
  buildJsonEqualityFilter(lhs, rhs) {
1219
- return this.buildLiteralFilter(lhs, "Json", rhs);
1220
+ return this.buildValueFilter(lhs, "Json", rhs);
1220
1221
  }
1221
- buildLiteralFilter(lhs, type, rhs) {
1222
- return this.eb(lhs, "=", rhs !== null && rhs !== void 0 ? this.transformInput(rhs, type, false) : rhs);
1222
+ buildValueFilter(lhs, type, rhs) {
1223
+ if (rhs === void 0) {
1224
+ return this.true();
1225
+ }
1226
+ if (rhs === null) {
1227
+ return this.eb(lhs, "is", null);
1228
+ }
1229
+ return this.eb(lhs, "=", this.transformInput(rhs, type, false));
1223
1230
  }
1224
1231
  buildStandardFilter(type, payload, lhs, getRhs, recurse, throwIfInvalid = false, onlyForKeys = void 0, excludeKeys = []) {
1225
1232
  if (payload === null || !(0, import_common_helpers2.isPlainObject)(payload)) {
1226
1233
  return {
1227
1234
  conditions: [
1228
- this.buildLiteralFilter(lhs, type, payload)
1235
+ this.buildValueFilter(lhs, type, payload)
1229
1236
  ],
1230
1237
  consumedKeys: []
1231
1238
  };
@@ -1608,6 +1615,15 @@ var BaseCrudDialect = class {
1608
1615
  }
1609
1616
  return true;
1610
1617
  }
1618
+ // #endregion
1619
+ /**
1620
+ * Builds an EXISTS expression from an inner SELECT query.
1621
+ * Can be overridden by dialects that need special handling (e.g., MySQL wraps
1622
+ * in a derived table to avoid "can't specify target table for update in FROM clause").
1623
+ */
1624
+ buildExistsExpression(innerQuery) {
1625
+ return this.eb.exists(innerQuery);
1626
+ }
1611
1627
  };
1612
1628
 
1613
1629
  // src/client/crud/dialects/lateral-join-dialect-base.ts
@@ -1830,6 +1846,9 @@ var MySqlCrudDialect = class extends LateralJoinDialectBase {
1830
1846
  }
1831
1847
  // #endregion
1832
1848
  // #region other overrides
1849
+ buildExistsExpression(innerQuery) {
1850
+ return this.eb.exists(this.eb.selectFrom(innerQuery.as("$exists_sub")).select(this.eb.lit(1).as("_")));
1851
+ }
1833
1852
  buildArrayAgg(arg) {
1834
1853
  return this.eb.fn.coalesce(import_kysely3.sql`JSON_ARRAYAGG(${arg})`, import_kysely3.sql`JSON_ARRAY()`);
1835
1854
  }
@@ -1967,7 +1986,10 @@ var PostgresCrudDialect = class _PostgresCrudDialect extends LateralJoinDialectB
1967
1986
  overrideTypeParsers() {
1968
1987
  if (this.options.fixPostgresTimezone !== false && !_PostgresCrudDialect.typeParserOverrideApplied) {
1969
1988
  _PostgresCrudDialect.typeParserOverrideApplied = true;
1970
- import("pg").then((pg) => {
1989
+ import(
1990
+ /* webpackIgnore: true */
1991
+ "pg"
1992
+ ).then((pg) => {
1971
1993
  pg.types.setTypeParser(pg.types.builtins.TIMESTAMP, (value) => {
1972
1994
  if (typeof value !== "string") {
1973
1995
  return value;
@@ -3170,7 +3192,7 @@ var BaseOperationHandler = class {
3170
3192
  const length = firstArgVal;
3171
3193
  const generated = typeof length === "number" ? (0, import_nanoid.nanoid)(length) : (0, import_nanoid.nanoid)();
3172
3194
  return this.formatGeneratedValue(generated, defaultValue.args?.[1]);
3173
- }).with("ulid", () => this.formatGeneratedValue((0, import_ulid.ulid)(), defaultValue.args?.[0])).otherwise(() => void 0);
3195
+ }).with("ulid", () => this.formatGeneratedValue((0, import_ulid.ulid)(), defaultValue.args?.[0])).with("now", () => /* @__PURE__ */ new Date()).otherwise(() => void 0);
3174
3196
  } else if (schema_exports.ExpressionUtils.isMember(defaultValue) && schema_exports.ExpressionUtils.isCall(defaultValue.receiver) && defaultValue.receiver.function === "auth") {
3175
3197
  let val = this.client.$auth;
3176
3198
  for (const member of defaultValue.members) {
@@ -7485,20 +7507,45 @@ var TempAliasTransformer = class extends import_kysely8.OperationNodeTransformer
7485
7507
  __name(this, "TempAliasTransformer");
7486
7508
  }
7487
7509
  aliasMap = /* @__PURE__ */ new Map();
7510
+ textEncoder = new TextEncoder();
7511
+ mode;
7512
+ maxIdentifierLength;
7513
+ constructor(options = {}) {
7514
+ super();
7515
+ this.mode = options.mode ?? "alwaysCompact";
7516
+ const maxIdentifierLength = options.maxIdentifierLength ?? 63;
7517
+ if (!Number.isFinite(maxIdentifierLength) || !Number.isInteger(maxIdentifierLength) || maxIdentifierLength <= 0) {
7518
+ throw new RangeError("maxIdentifierLength must be a positive integer");
7519
+ }
7520
+ this.maxIdentifierLength = maxIdentifierLength;
7521
+ }
7488
7522
  run(node) {
7489
7523
  this.aliasMap.clear();
7490
7524
  return this.transformNode(node);
7491
7525
  }
7492
7526
  transformIdentifier(node, queryId) {
7493
- if (node.name.startsWith(TEMP_ALIAS_PREFIX)) {
7527
+ if (!node.name.startsWith(TEMP_ALIAS_PREFIX)) {
7528
+ return super.transformIdentifier(node, queryId);
7529
+ }
7530
+ let shouldCompact = false;
7531
+ if (this.mode === "alwaysCompact") {
7532
+ shouldCompact = true;
7533
+ } else {
7534
+ const aliasByteLength = this.textEncoder.encode(node.name).length;
7535
+ if (aliasByteLength > this.maxIdentifierLength) {
7536
+ shouldCompact = true;
7537
+ }
7538
+ }
7539
+ if (shouldCompact) {
7494
7540
  let mapped = this.aliasMap.get(node.name);
7495
7541
  if (!mapped) {
7496
7542
  mapped = `$$t${this.aliasMap.size + 1}`;
7497
7543
  this.aliasMap.set(node.name, mapped);
7498
7544
  }
7499
7545
  return import_kysely8.IdentifierNode.create(mapped);
7546
+ } else {
7547
+ return super.transformIdentifier(node, queryId);
7500
7548
  }
7501
- return super.transformIdentifier(node, queryId);
7502
7549
  }
7503
7550
  };
7504
7551
 
@@ -7888,10 +7935,9 @@ In such cases, ZenStack cannot reliably determine the IDs of the mutated entitie
7888
7935
  return this.nameMapper?.transformNode(query) ?? query;
7889
7936
  }
7890
7937
  processTempAlias(query) {
7891
- if (this.options.useCompactAliasNames === false) {
7892
- return query;
7893
- }
7894
- return new TempAliasTransformer().run(query);
7938
+ return new TempAliasTransformer({
7939
+ mode: this.options.useCompactAliasNames === false ? "compactLongNames" : "alwaysCompact"
7940
+ }).run(query);
7895
7941
  }
7896
7942
  createClientForConnection(connection, inTx) {
7897
7943
  const innerExecutor = this.withConnectionProvider(new import_kysely9.SingleConnectionProvider(connection));
@@ -8073,7 +8119,7 @@ var isEmpty = /* @__PURE__ */ __name((eb, args, { dialect }) => {
8073
8119
  }
8074
8120
  return eb(dialect.buildArrayLength(field), "=", import_kysely10.sql.lit(0));
8075
8121
  }, "isEmpty");
8076
- var now = /* @__PURE__ */ __name(() => import_kysely10.sql.raw("CURRENT_TIMESTAMP"), "now");
8122
+ var now = /* @__PURE__ */ __name((_eb, _args, context) => (0, import_ts_pattern16.match)(context.dialect.provider).with("sqlite", () => import_kysely10.sql.raw("strftime('%Y-%m-%dT%H:%M:%fZ')")).with("mysql", () => import_kysely10.sql.raw("CONCAT(SUBSTRING(DATE_FORMAT(UTC_TIMESTAMP(3), '%Y-%m-%dT%H:%i:%s.%f'), 1, 23), '+00:00')")).with("postgresql", () => import_kysely10.sql.raw("CURRENT_TIMESTAMP")).exhaustive(), "now");
8077
8123
  var currentModel = /* @__PURE__ */ __name((_eb, args, { model }) => {
8078
8124
  let result = model;
8079
8125
  const [casing] = args;