@zenstackhq/orm 3.9.4-beta.1 → 3.9.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
@@ -1431,7 +1431,7 @@ var BaseCrudDialect = class {
1431
1431
  }
1432
1432
  }
1433
1433
  canJoinWithoutNestedSelect(modelDef, payload) {
1434
- if (modelDef.computedFields) return false;
1434
+ if (Object.values(modelDef.fields).some((f) => f.computed)) return false;
1435
1435
  if (modelDef.baseModel || modelDef.isDelegate) return false;
1436
1436
  if (typeof payload === "object" && (payload.orderBy || payload.skip !== void 0 || payload.take !== void 0 || payload.cursor || payload.distinct)) return false;
1437
1437
  return true;
@@ -5857,6 +5857,31 @@ var UpdateOperationHandler = class extends BaseOperationHandler {
5857
5857
  }
5858
5858
  };
5859
5859
  //#endregion
5860
+ //#region src/client/executor/connection-mutex.ts
5861
+ /**
5862
+ * This mutex is used to ensure that only one operation at a time can
5863
+ * acquire a connection from the driver. This is necessary when the
5864
+ * driver only has a single connection, like SQLite and PGlite.
5865
+ *
5866
+ * @see {@link https://github.com/kysely-org/kysely/blob/478ec67b2de2568f5590a015d3e120644e81bd87/src/driver/connection-mutex.ts|Kysely Source}
5867
+ */
5868
+ var ConnectionMutex = class {
5869
+ #promise;
5870
+ #resolve;
5871
+ async obtainLock() {
5872
+ while (this.#promise) await this.#promise;
5873
+ this.#promise = new Promise((resolve) => {
5874
+ this.#resolve = resolve;
5875
+ });
5876
+ }
5877
+ releaseLock() {
5878
+ const resolve = this.#resolve;
5879
+ this.#promise = void 0;
5880
+ this.#resolve = void 0;
5881
+ resolve?.();
5882
+ }
5883
+ };
5884
+ //#endregion
5860
5885
  //#region src/client/executor/zenstack-driver.ts
5861
5886
  /**
5862
5887
  * Copied from kysely's RuntimeDriver
@@ -5864,15 +5889,17 @@ var UpdateOperationHandler = class extends BaseOperationHandler {
5864
5889
  var ZenStackDriver = class {
5865
5890
  #driver;
5866
5891
  #log;
5892
+ #connectionMutex;
5867
5893
  #initPromise;
5868
5894
  #initDone;
5869
5895
  #destroyPromise;
5870
5896
  #connections = /* @__PURE__ */ new WeakSet();
5871
5897
  #txConnections = /* @__PURE__ */ new WeakMap();
5872
- constructor(driver, log) {
5898
+ constructor(driver, log, adapter) {
5873
5899
  this.#initDone = false;
5874
5900
  this.#driver = driver;
5875
5901
  this.#log = log;
5902
+ if (!adapter.supportsMultipleConnections) this.#connectionMutex = new ConnectionMutex();
5876
5903
  }
5877
5904
  async init() {
5878
5905
  if (this.#destroyPromise) throw new Error("driver has already been destroyed");
@@ -5887,15 +5914,25 @@ var ZenStackDriver = class {
5887
5914
  async acquireConnection() {
5888
5915
  if (this.#destroyPromise) throw new Error("driver has already been destroyed");
5889
5916
  if (!this.#initDone) await this.init();
5890
- const connection = await this.#driver.acquireConnection();
5891
- if (!this.#connections.has(connection)) {
5892
- if (this.#needsLogging()) this.#addLogging(connection);
5893
- this.#connections.add(connection);
5917
+ await this.#connectionMutex?.obtainLock();
5918
+ try {
5919
+ const connection = await this.#driver.acquireConnection();
5920
+ if (!this.#connections.has(connection)) {
5921
+ if (this.#needsLogging()) this.#addLogging(connection);
5922
+ this.#connections.add(connection);
5923
+ }
5924
+ return connection;
5925
+ } catch (error) {
5926
+ this.#connectionMutex?.releaseLock();
5927
+ throw error;
5894
5928
  }
5895
- return connection;
5896
5929
  }
5897
5930
  async releaseConnection(connection) {
5898
- await this.#driver.releaseConnection(connection);
5931
+ try {
5932
+ await this.#driver.releaseConnection(connection);
5933
+ } finally {
5934
+ this.#connectionMutex?.releaseLock();
5935
+ }
5899
5936
  }
5900
5937
  async beginTransaction(connection, settings) {
5901
5938
  const result = await this.#driver.beginTransaction(connection, settings);
@@ -6436,8 +6473,7 @@ var QueryNameMapper = class extends kysely.OperationNodeTransformer {
6436
6473
  }
6437
6474
  processEnumSelection(selection, fieldName) {
6438
6475
  const { alias, node } = stripAlias(selection);
6439
- const qualifier = kysely.ReferenceNode.is(node) && node.table && kysely.TableNode.is(node.table) ? node.table.table.identifier.name : void 0;
6440
- const fieldScope = this.resolveFieldFromScopes(fieldName, qualifier);
6476
+ const fieldScope = this.resolveFieldFromScopes(fieldName, kysely.ReferenceNode.is(node) ? node.table?.table?.identifier.name : void 0);
6441
6477
  if (!fieldScope || !fieldScope.model) return selection;
6442
6478
  const aliasName = alias && kysely.IdentifierNode.is(alias) ? alias.name : fieldName;
6443
6479
  const fieldDef = getField(this.schema, fieldScope.model, fieldName);
@@ -7357,9 +7393,9 @@ var ClientImpl = class ClientImpl {
7357
7393
  this.auth = baseClient.auth;
7358
7394
  this.slowQueries = baseClient.slowQueries;
7359
7395
  } else {
7360
- const driver = new ZenStackDriver(options.dialect.createDriver(), new kysely.Log(this.$options.log ?? []));
7361
- const compiler = options.dialect.createQueryCompiler();
7362
7396
  const adapter = options.dialect.createAdapter();
7397
+ const driver = new ZenStackDriver(options.dialect.createDriver(), new kysely.Log(this.$options.log ?? []), adapter);
7398
+ const compiler = options.dialect.createQueryCompiler();
7363
7399
  const connectionProvider = new kysely.DefaultConnectionProvider(driver);
7364
7400
  this.kyselyProps = {
7365
7401
  config: {
@@ -7395,7 +7431,8 @@ var ClientImpl = class ClientImpl {
7395
7431
  */
7396
7432
  validateComputedFieldsConfig(options) {
7397
7433
  const computedFieldsConfig = "computedFields" in options ? options.computedFields : void 0;
7398
- for (const [modelName, modelDef] of Object.entries(this.$schema.models)) if (modelDef.computedFields) for (const fieldName of Object.keys(modelDef.computedFields)) {
7434
+ for (const [modelName, modelDef] of Object.entries(this.$schema.models)) for (const [fieldName, fieldDef] of Object.entries(modelDef.fields)) {
7435
+ if (!fieldDef.computed || fieldDef.originModel) continue;
7399
7436
  const fieldConfig = (computedFieldsConfig?.[(0, _zenstackhq_common_helpers.lowerCaseFirst)(modelName)] ?? computedFieldsConfig?.[modelName])?.[fieldName];
7400
7437
  if (fieldConfig === null || fieldConfig === void 0) throw createConfigError(`Computed field "${fieldName}" in model "${modelName}" does not have a configuration. Please provide an implementation in the computedFields option.`);
7401
7438
  if (typeof fieldConfig !== "function") throw createConfigError(`Computed field "${fieldName}" in model "${modelName}" has an invalid configuration: expected a function but received ${typeof fieldConfig}.`);