@zenstackhq/orm 3.9.0 → 3.9.1

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
@@ -252,6 +252,7 @@ var query_utils_exports = /* @__PURE__ */ __exportAll({
252
252
  requireIdFields: () => requireIdFields,
253
253
  requireModel: () => requireModel,
254
254
  requireTypeDef: () => requireTypeDef,
255
+ schemaHasMappedNames: () => schemaHasMappedNames,
255
256
  stripAlias: () => stripAlias,
256
257
  tmpAlias: () => tmpAlias
257
258
  });
@@ -281,6 +282,22 @@ function getModel(schema, model) {
281
282
  function getTypeDef(schema, type) {
282
283
  return schema.typeDefs?.[type];
283
284
  }
285
+ /**
286
+ * Whether any model, type def, or field in the schema carries `@@map`/`@map`. Answering it walks
287
+ * every model and field, and it is asked once per query-executor construction, so the (immutable)
288
+ * answer is memoized per schema alongside the other structural lookups. See issue #2773.
289
+ */
290
+ function schemaHasMappedNames(schema) {
291
+ const cache = getSchemaLookupCache(schema);
292
+ if (cache.hasMappedNames === void 0) {
293
+ const hasMapAttr = (decl) => {
294
+ if (decl.attributes?.some((attr) => attr.name === "@@map")) return true;
295
+ return Object.values(decl.fields ?? {}).some((field) => field.attributes?.some((attr) => attr.name === "@map"));
296
+ };
297
+ cache.hasMappedNames = Object.values(schema.models).some(hasMapAttr) || Object.values(schema.typeDefs ?? {}).some(hasMapAttr) || Object.values(schema.enums ?? {}).some(hasMapAttr);
298
+ }
299
+ return cache.hasMappedNames;
300
+ }
284
301
  function requireModel(schema, model) {
285
302
  const modelDef = getModel(schema, model);
286
303
  if (!modelDef) throw createInternalError(`Model "${model}" not found in schema`, model);
@@ -672,9 +689,23 @@ const CRUD_EXT = [...CRUD, "post-update"];
672
689
  //#region src/client/crud/dialects/base-dialect.ts
673
690
  var BaseCrudDialect = class {
674
691
  eb = (0, kysely.expressionBuilder)();
675
- constructor(schema, options) {
676
- this.schema = schema;
677
- this.options = options;
692
+ schema;
693
+ options;
694
+ /**
695
+ * The client executing the query. Unset only when the dialect was constructed from a
696
+ * standalone schema/options pair, in which case it cannot evaluate computed fields.
697
+ */
698
+ client;
699
+ constructor(...args) {
700
+ if (args.length === 1) {
701
+ const [client] = args;
702
+ this.client = client;
703
+ this.schema = client.$schema;
704
+ this.options = client.$options;
705
+ } else {
706
+ [this.schema, this.options] = args;
707
+ this.client = void 0;
708
+ }
678
709
  }
679
710
  /**
680
711
  * Transforms input value before sending to database.
@@ -1392,7 +1423,11 @@ var BaseCrudDialect = class {
1392
1423
  computer = computedFields?.[(0, _zenstackhq_common_helpers.lowerCaseFirst)(computedModel)]?.[field] ?? computedFields?.[computedModel]?.[field];
1393
1424
  }
1394
1425
  if (!computer) throw createConfigError(`Computed field "${field}" implementation not provided for model "${model}"`);
1395
- return computer(this.eb, { modelAlias }, computedArgs);
1426
+ (0, _zenstackhq_common_helpers.invariant)(this.client, `computed field "${field}" of model "${model}" needs a client to be evaluated`);
1427
+ return this.eb.parens(computer(this.eb, {
1428
+ modelAlias,
1429
+ client: this.client
1430
+ }, computedArgs));
1396
1431
  }
1397
1432
  }
1398
1433
  canJoinWithoutNestedSelect(modelDef, payload) {
@@ -1561,9 +1596,6 @@ var LateralJoinDialectBase = class extends BaseCrudDialect {
1561
1596
  //#endregion
1562
1597
  //#region src/client/crud/dialects/mysql.ts
1563
1598
  var MySqlCrudDialect = class extends LateralJoinDialectBase {
1564
- constructor(schema, options) {
1565
- super(schema, options);
1566
- }
1567
1599
  get provider() {
1568
1600
  return "mysql";
1569
1601
  }
@@ -1791,8 +1823,8 @@ var PostgresCrudDialect = class PostgresCrudDialect extends LateralJoinDialectBa
1791
1823
  "@db.Decimal": "decimal",
1792
1824
  "@db.Boolean": "boolean"
1793
1825
  };
1794
- constructor(schema, options) {
1795
- super(schema, options);
1826
+ constructor(...args) {
1827
+ super(...args);
1796
1828
  this.overrideTypeParsers();
1797
1829
  }
1798
1830
  get provider() {
@@ -2341,8 +2373,8 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
2341
2373
  };
2342
2374
  //#endregion
2343
2375
  //#region src/client/crud/dialects/index.ts
2344
- function getCrudDialect(schema, options) {
2345
- return (0, ts_pattern.match)(schema.provider.type).with("sqlite", () => new SqliteCrudDialect(schema, options)).with("postgresql", () => new PostgresCrudDialect(schema, options)).with("mysql", () => new MySqlCrudDialect(schema, options)).exhaustive();
2376
+ function getCrudDialect(...args) {
2377
+ return (0, ts_pattern.match)((args.length === 1 ? args[0].$schema : args[0]).provider.type).with("sqlite", () => new SqliteCrudDialect(...args)).with("postgresql", () => new PostgresCrudDialect(...args)).with("mysql", () => new MySqlCrudDialect(...args)).exhaustive();
2346
2378
  }
2347
2379
  //#endregion
2348
2380
  //#region src/client/zod/cache-decorator.ts
@@ -4348,7 +4380,7 @@ var BaseOperationHandler = class {
4348
4380
  this.client = client;
4349
4381
  this.model = model;
4350
4382
  this.inputValidator = inputValidator;
4351
- this.dialect = getCrudDialect(this.schema, this.client.$options);
4383
+ this.dialect = getCrudDialect(this.client);
4352
4384
  }
4353
4385
  get schema() {
4354
4386
  return this.client.$schema;
@@ -5981,7 +6013,7 @@ var QueryNameMapper = class extends kysely.OperationNodeTransformer {
5981
6013
  constructor(client) {
5982
6014
  super();
5983
6015
  this.client = client;
5984
- this.dialect = getCrudDialect(client.$schema, client.$options);
6016
+ this.dialect = getCrudDialect(client);
5985
6017
  for (const [modelName, modelDef] of Object.entries(client.$schema.models)) {
5986
6018
  const mappedName = this.getMappedName(modelDef);
5987
6019
  if (mappedName) this.modelToTableMap.set(modelName, mappedName);
@@ -6444,22 +6476,22 @@ const DEFAULT_MAX_SLOW_RECORDS = 100;
6444
6476
  var ZenStackQueryExecutor = class ZenStackQueryExecutor extends kysely.DefaultQueryExecutor {
6445
6477
  nameMapper;
6446
6478
  dialect;
6447
- constructor(client, driver, compiler, adapter, connectionProvider, plugins = [], suppressMutationHooks = false) {
6479
+ constructor(client, driver, compiler, adapter, connectionProvider, plugins = [], suppressMutationHooks = false, nameMapper) {
6448
6480
  super(compiler, adapter, connectionProvider, plugins);
6449
6481
  this.client = client;
6450
6482
  this.driver = driver;
6451
6483
  this.compiler = compiler;
6452
6484
  this.connectionProvider = connectionProvider;
6453
6485
  this.suppressMutationHooks = suppressMutationHooks;
6454
- if (client.$schema.provider.type === "postgresql" || this.schemaHasMappedNames(client.$schema)) this.nameMapper = new QueryNameMapper(client);
6455
- this.dialect = getCrudDialect(client.$schema, client.$options);
6486
+ this.nameMapper = nameMapper ?? (client.$schema.provider.type === "postgresql" || schemaHasMappedNames(client.$schema) ? new QueryNameMapper(client.$contract) : void 0);
6487
+ this.dialect = getCrudDialect(client.$contract);
6456
6488
  }
6457
- schemaHasMappedNames(schema) {
6458
- const hasMapAttr = (decl) => {
6459
- if (decl.attributes?.some((attr) => attr.name === "@@map")) return true;
6460
- return Object.values(decl.fields).some((field) => field.attributes?.some((attr) => attr.name === "@map"));
6461
- };
6462
- return Object.values(schema.models).some(hasMapAttr) || Object.values(schema.typeDefs ?? []).some(hasMapAttr);
6489
+ /**
6490
+ * The name mapper built for this executor's schema, if the schema needs one. Exposed so that
6491
+ * derived clients built from the same schema and options can reuse it instead of rebuilding it.
6492
+ */
6493
+ getNameMapper() {
6494
+ return this.nameMapper;
6463
6495
  }
6464
6496
  get kysely() {
6465
6497
  return this.client.$qb;
@@ -6506,7 +6538,7 @@ var ZenStackQueryExecutor = class ZenStackQueryExecutor extends kysely.DefaultQu
6506
6538
  proceed = async (query) => {
6507
6539
  const _p = (q) => _proceed(q);
6508
6540
  return await hook({
6509
- client: this.client,
6541
+ client: this.client.$contract,
6510
6542
  schema: this.client.$schema,
6511
6543
  query,
6512
6544
  proceed: _p
@@ -6744,7 +6776,7 @@ In such cases, ZenStack cannot reliably determine the IDs of the mutated entitie
6744
6776
  innerExecutor.suppressMutationHooks = true;
6745
6777
  const innerClient = this.client.withExecutor(innerExecutor);
6746
6778
  if (inTx) innerClient.forceTransaction();
6747
- return innerClient;
6779
+ return innerClient.$contract;
6748
6780
  }
6749
6781
  andNodes(condition1, condition2) {
6750
6782
  if (condition1 && condition2) return kysely.WhereNode.create(kysely.AndNode.create(condition1, condition2));
@@ -6796,19 +6828,19 @@ In such cases, ZenStack cannot reliably determine the IDs of the mutated entitie
6796
6828
  return finalResult;
6797
6829
  }
6798
6830
  withPlugin(plugin) {
6799
- return new ZenStackQueryExecutor(this.client, this.driver, this.compiler, this.adapter, this.connectionProvider, [...this.plugins, plugin], this.suppressMutationHooks);
6831
+ return new ZenStackQueryExecutor(this.client, this.driver, this.compiler, this.adapter, this.connectionProvider, [...this.plugins, plugin], this.suppressMutationHooks, this.nameMapper);
6800
6832
  }
6801
6833
  withPlugins(plugins) {
6802
- return new ZenStackQueryExecutor(this.client, this.driver, this.compiler, this.adapter, this.connectionProvider, [...this.plugins, ...plugins], this.suppressMutationHooks);
6834
+ return new ZenStackQueryExecutor(this.client, this.driver, this.compiler, this.adapter, this.connectionProvider, [...this.plugins, ...plugins], this.suppressMutationHooks, this.nameMapper);
6803
6835
  }
6804
6836
  withPluginAtFront(plugin) {
6805
- return new ZenStackQueryExecutor(this.client, this.driver, this.compiler, this.adapter, this.connectionProvider, [plugin, ...this.plugins], this.suppressMutationHooks);
6837
+ return new ZenStackQueryExecutor(this.client, this.driver, this.compiler, this.adapter, this.connectionProvider, [plugin, ...this.plugins], this.suppressMutationHooks, this.nameMapper);
6806
6838
  }
6807
6839
  withoutPlugins() {
6808
- return new ZenStackQueryExecutor(this.client, this.driver, this.compiler, this.adapter, this.connectionProvider, [], this.suppressMutationHooks);
6840
+ return new ZenStackQueryExecutor(this.client, this.driver, this.compiler, this.adapter, this.connectionProvider, [], this.suppressMutationHooks, this.nameMapper);
6809
6841
  }
6810
6842
  withConnectionProvider(connectionProvider) {
6811
- const newExecutor = new ZenStackQueryExecutor(this.client, this.driver, this.compiler, this.adapter, connectionProvider, this.plugins, this.suppressMutationHooks);
6843
+ const newExecutor = new ZenStackQueryExecutor(this.client, this.driver, this.compiler, this.adapter, connectionProvider, this.plugins, this.suppressMutationHooks, this.nameMapper);
6812
6844
  newExecutor.client = this.client.withExecutor(newExecutor);
6813
6845
  return newExecutor;
6814
6846
  }
@@ -7264,6 +7296,12 @@ var ResultProcessor = class {
7264
7296
  //#endregion
7265
7297
  //#region src/client/client-impl.ts
7266
7298
  /**
7299
+ * Returns the name mapper held by a ZenStack executor, or undefined for a plain kysely one.
7300
+ */
7301
+ function getExecutorNameMapper(executor) {
7302
+ return executor instanceof ZenStackQueryExecutor ? executor.getNameMapper() : void 0;
7303
+ }
7304
+ /**
7267
7305
  * ZenStack ORM client.
7268
7306
  */
7269
7307
  const ZenStackClient = function(schema, options) {
@@ -7291,7 +7329,7 @@ var ClientImpl = class ClientImpl {
7291
7329
  if (baseClient) {
7292
7330
  this.kyselyProps = {
7293
7331
  ...baseClient.kyselyProps,
7294
- executor: executor ?? new ZenStackQueryExecutor(this, baseClient.kyselyProps.driver, baseClient.kyselyProps.dialect.createQueryCompiler(), baseClient.kyselyProps.dialect.createAdapter(), new kysely.DefaultConnectionProvider(baseClient.kyselyProps.driver))
7332
+ executor: executor ?? new ZenStackQueryExecutor(this, baseClient.kyselyProps.driver, baseClient.kyselyProps.dialect.createQueryCompiler(), baseClient.kyselyProps.dialect.createAdapter(), new kysely.DefaultConnectionProvider(baseClient.kyselyProps.driver), [], false, baseClient.$schema === schema && baseClient.$options === options ? getExecutorNameMapper(baseClient.kyselyProps.executor) : void 0)
7295
7333
  };
7296
7334
  this.kyselyRaw = baseClient.kyselyRaw;
7297
7335
  this.auth = baseClient.auth;
@@ -7368,14 +7406,14 @@ var ClientImpl = class ClientImpl {
7368
7406
  if (!this.kysely.isTransaction) this.kysely = new kysely.Transaction(this.kyselyProps);
7369
7407
  }
7370
7408
  async interactiveTransaction(callback, options) {
7371
- if (this.kysely.isTransaction) return callback(this);
7409
+ if (this.kysely.isTransaction) return callback(this.$contract);
7372
7410
  else {
7373
7411
  let txBuilder = this.kysely.transaction();
7374
7412
  if (options?.isolationLevel) txBuilder = txBuilder.setIsolationLevel(options.isolationLevel);
7375
7413
  return txBuilder.execute((tx) => {
7376
7414
  const txClient = new ClientImpl(this.schema, this.$options, this);
7377
7415
  txClient.kysely = tx;
7378
- return callback(txClient);
7416
+ return callback(txClient.$contract);
7379
7417
  });
7380
7418
  }
7381
7419
  }
@@ -7392,7 +7430,7 @@ var ClientImpl = class ClientImpl {
7392
7430
  const result = [];
7393
7431
  for (const promise of arg) {
7394
7432
  const cb = this.getPromiseCallback(promise);
7395
- result.push(await cb(txClient));
7433
+ result.push(await cb(txClient.$contract));
7396
7434
  }
7397
7435
  return result;
7398
7436
  };
@@ -7493,6 +7531,16 @@ var ClientImpl = class ClientImpl {
7493
7531
  get $auth() {
7494
7532
  return this.auth;
7495
7533
  }
7534
+ /**
7535
+ * This client viewed through its public typed contract. `ClientImpl` is intentionally
7536
+ * untyped internally — the model accessors are added by the runtime proxy — so this
7537
+ * getter is the single sanctioned bridge to `ClientContract`. The proxy invokes it
7538
+ * with the proxy as `this` (`Reflect.get` with receiver), so the returned reference
7539
+ * keeps the model accessors.
7540
+ */
7541
+ get $contract() {
7542
+ return this;
7543
+ }
7496
7544
  $setOptions(options) {
7497
7545
  const newClient = new ClientImpl(this.schema, options, this);
7498
7546
  newClient.inputValidator = new InputValidator(newClient, { enabled: newClient.$options.validateInput !== false });