@zenstackhq/orm 3.4.4 → 3.4.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
@@ -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
  }
@@ -1608,6 +1609,15 @@ var BaseCrudDialect = class {
1608
1609
  }
1609
1610
  return true;
1610
1611
  }
1612
+ // #endregion
1613
+ /**
1614
+ * Builds an EXISTS expression from an inner SELECT query.
1615
+ * Can be overridden by dialects that need special handling (e.g., MySQL wraps
1616
+ * in a derived table to avoid "can't specify target table for update in FROM clause").
1617
+ */
1618
+ buildExistsExpression(innerQuery) {
1619
+ return this.eb.exists(innerQuery);
1620
+ }
1611
1621
  };
1612
1622
 
1613
1623
  // src/client/crud/dialects/lateral-join-dialect-base.ts
@@ -1830,6 +1840,9 @@ var MySqlCrudDialect = class extends LateralJoinDialectBase {
1830
1840
  }
1831
1841
  // #endregion
1832
1842
  // #region other overrides
1843
+ buildExistsExpression(innerQuery) {
1844
+ return this.eb.exists(this.eb.selectFrom(innerQuery.as("$exists_sub")).select(this.eb.lit(1).as("_")));
1845
+ }
1833
1846
  buildArrayAgg(arg) {
1834
1847
  return this.eb.fn.coalesce(import_kysely3.sql`JSON_ARRAYAGG(${arg})`, import_kysely3.sql`JSON_ARRAY()`);
1835
1848
  }
@@ -7485,20 +7498,45 @@ var TempAliasTransformer = class extends import_kysely8.OperationNodeTransformer
7485
7498
  __name(this, "TempAliasTransformer");
7486
7499
  }
7487
7500
  aliasMap = /* @__PURE__ */ new Map();
7501
+ textEncoder = new TextEncoder();
7502
+ mode;
7503
+ maxIdentifierLength;
7504
+ constructor(options = {}) {
7505
+ super();
7506
+ this.mode = options.mode ?? "alwaysCompact";
7507
+ const maxIdentifierLength = options.maxIdentifierLength ?? 63;
7508
+ if (!Number.isFinite(maxIdentifierLength) || !Number.isInteger(maxIdentifierLength) || maxIdentifierLength <= 0) {
7509
+ throw new RangeError("maxIdentifierLength must be a positive integer");
7510
+ }
7511
+ this.maxIdentifierLength = maxIdentifierLength;
7512
+ }
7488
7513
  run(node) {
7489
7514
  this.aliasMap.clear();
7490
7515
  return this.transformNode(node);
7491
7516
  }
7492
7517
  transformIdentifier(node, queryId) {
7493
- if (node.name.startsWith(TEMP_ALIAS_PREFIX)) {
7518
+ if (!node.name.startsWith(TEMP_ALIAS_PREFIX)) {
7519
+ return super.transformIdentifier(node, queryId);
7520
+ }
7521
+ let shouldCompact = false;
7522
+ if (this.mode === "alwaysCompact") {
7523
+ shouldCompact = true;
7524
+ } else {
7525
+ const aliasByteLength = this.textEncoder.encode(node.name).length;
7526
+ if (aliasByteLength > this.maxIdentifierLength) {
7527
+ shouldCompact = true;
7528
+ }
7529
+ }
7530
+ if (shouldCompact) {
7494
7531
  let mapped = this.aliasMap.get(node.name);
7495
7532
  if (!mapped) {
7496
7533
  mapped = `$$t${this.aliasMap.size + 1}`;
7497
7534
  this.aliasMap.set(node.name, mapped);
7498
7535
  }
7499
7536
  return import_kysely8.IdentifierNode.create(mapped);
7537
+ } else {
7538
+ return super.transformIdentifier(node, queryId);
7500
7539
  }
7501
- return super.transformIdentifier(node, queryId);
7502
7540
  }
7503
7541
  };
7504
7542
 
@@ -7888,10 +7926,9 @@ In such cases, ZenStack cannot reliably determine the IDs of the mutated entitie
7888
7926
  return this.nameMapper?.transformNode(query) ?? query;
7889
7927
  }
7890
7928
  processTempAlias(query) {
7891
- if (this.options.useCompactAliasNames === false) {
7892
- return query;
7893
- }
7894
- return new TempAliasTransformer().run(query);
7929
+ return new TempAliasTransformer({
7930
+ mode: this.options.useCompactAliasNames === false ? "compactLongNames" : "alwaysCompact"
7931
+ }).run(query);
7895
7932
  }
7896
7933
  createClientForConnection(connection, inTx) {
7897
7934
  const innerExecutor = this.withConnectionProvider(new import_kysely9.SingleConnectionProvider(connection));