@zenstackhq/runtime 3.0.0-alpha.23 → 3.0.0-alpha.25

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
@@ -305,11 +305,14 @@ function getIdValues(schema, model, data) {
305
305
  }), {});
306
306
  }
307
307
  __name(getIdValues, "getIdValues");
308
- function buildFieldRef(schema, model, field, options, eb, modelAlias) {
308
+ function buildFieldRef(schema, model, field, options, eb, modelAlias, inlineComputedField = true) {
309
309
  const fieldDef = requireField(schema, model, field);
310
310
  if (!fieldDef.computed) {
311
311
  return eb.ref(modelAlias ? `${modelAlias}.${field}` : field);
312
312
  } else {
313
+ if (!inlineComputedField) {
314
+ return eb.ref(modelAlias ? `${modelAlias}.${field}` : field);
315
+ }
313
316
  let computer;
314
317
  if ("computedFields" in options) {
315
318
  const computedFields = options.computedFields;
@@ -493,7 +496,6 @@ var import_kysely2 = require("kysely");
493
496
  var import_ts_pattern3 = require("ts-pattern");
494
497
 
495
498
  // src/client/constants.ts
496
- var CONTEXT_COMMENT_PREFIX = "-- $$context:";
497
499
  var NUMERIC_FIELD_TYPES = [
498
500
  "Int",
499
501
  "Float",
@@ -1080,8 +1082,8 @@ var BaseCrudDialect = class {
1080
1082
  not(eb, ...args) {
1081
1083
  return eb.not(this.and(eb, ...args));
1082
1084
  }
1083
- fieldRef(model, field, eb, modelAlias) {
1084
- return buildFieldRef(this.schema, model, field, this.options, eb, modelAlias);
1085
+ fieldRef(model, field, eb, modelAlias, inlineComputedField = true) {
1086
+ return buildFieldRef(this.schema, model, field, this.options, eb, modelAlias, inlineComputedField);
1085
1087
  }
1086
1088
  };
1087
1089
 
@@ -1176,7 +1178,7 @@ var PostgresCrudDialect = class extends BaseCrudDialect {
1176
1178
  if (payload === true || !payload.select) {
1177
1179
  objArgs.push(...Object.entries(relationModelDef.fields).filter(([, value]) => !value.relation).filter(([name]) => !(typeof payload === "object" && payload.omit?.[name] === true)).map(([field]) => [
1178
1180
  import_kysely2.sql.lit(field),
1179
- this.fieldRef(relationModel, field, eb)
1181
+ this.fieldRef(relationModel, field, eb, void 0, false)
1180
1182
  ]).flatMap((v) => v));
1181
1183
  } else if (payload.select) {
1182
1184
  objArgs.push(...Object.entries(payload.select).filter(([, value]) => value).map(([field, value]) => {
@@ -1188,7 +1190,7 @@ var PostgresCrudDialect = class extends BaseCrudDialect {
1188
1190
  ];
1189
1191
  } else {
1190
1192
  const fieldDef = requireField(this.schema, relationModel, field);
1191
- const fieldValue = fieldDef.relation ? eb.ref(`${parentAlias}$${relationField}$${field}.$j`) : this.fieldRef(relationModel, field, eb);
1193
+ const fieldValue = fieldDef.relation ? eb.ref(`${parentAlias}$${relationField}$${field}.$j`) : this.fieldRef(relationModel, field, eb, void 0, false);
1192
1194
  return [
1193
1195
  import_kysely2.sql.lit(field),
1194
1196
  fieldValue
@@ -1339,7 +1341,7 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
1339
1341
  if (payload === true || !payload.select) {
1340
1342
  objArgs.push(...Object.entries(relationModelDef.fields).filter(([, value]) => !value.relation).filter(([name]) => !(typeof payload === "object" && payload.omit?.[name] === true)).map(([field]) => [
1341
1343
  import_kysely3.sql.lit(field),
1342
- this.fieldRef(relationModel, field, eb)
1344
+ this.fieldRef(relationModel, field, eb, void 0, false)
1343
1345
  ]).flatMap((v) => v));
1344
1346
  } else if (payload.select) {
1345
1347
  objArgs.push(...Object.entries(payload.select).filter(([, value]) => value).map(([field, value]) => {
@@ -1360,7 +1362,7 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
1360
1362
  } else {
1361
1363
  return [
1362
1364
  import_kysely3.sql.lit(field),
1363
- this.fieldRef(relationModel, field, eb)
1365
+ this.fieldRef(relationModel, field, eb, void 0, false)
1364
1366
  ];
1365
1367
  }
1366
1368
  }
@@ -3460,8 +3462,8 @@ ${compiled.parameters.map((p) => (0, import_node_util.inspect)(p)).join("\n")}`;
3460
3462
  isNumericField(fieldDef) {
3461
3463
  return NUMERIC_FIELD_TYPES.includes(fieldDef.type) && !fieldDef.array;
3462
3464
  }
3463
- makeContextComment(context) {
3464
- return import_kysely8.sql.raw(`${CONTEXT_COMMENT_PREFIX}${JSON.stringify(context)}`);
3465
+ makeContextComment(_context) {
3466
+ return import_kysely8.sql``;
3465
3467
  }
3466
3468
  async updateMany(kysely, model, where, data, limit, returnData, filterModel) {
3467
3469
  if (typeof data !== "object") {
@@ -6015,7 +6017,7 @@ ${compiled.parameters.map((p) => (0, import_node_util2.inspect)(p)).join("\n")}`
6015
6017
  return newExecutor;
6016
6018
  }
6017
6019
  get hasMutationHooks() {
6018
- return this.client.$options.plugins?.some((plugin) => plugin.beforeEntityMutation || plugin.afterEntityMutation);
6020
+ return this.client.$options.plugins?.some((plugin) => !!plugin.onEntityMutation);
6019
6021
  }
6020
6022
  getMutationModel(queryNode) {
6021
6023
  return (0, import_ts_pattern16.match)(queryNode).when(import_kysely13.InsertQueryNode.is, (node) => node.into.table.identifier.name).when(import_kysely13.UpdateQueryNode.is, (node) => node.table.table.identifier.name).when(import_kysely13.DeleteQueryNode.is, (node) => {
@@ -6045,10 +6047,14 @@ ${compiled.parameters.map((p) => (0, import_node_util2.inspect)(p)).join("\n")}`
6045
6047
  where: node.where
6046
6048
  })).exhaustive();
6047
6049
  for (const plugin of plugins) {
6048
- if (!plugin.mutationInterceptionFilter) {
6050
+ const onEntityMutation = plugin.onEntityMutation;
6051
+ if (!onEntityMutation) {
6052
+ continue;
6053
+ }
6054
+ if (!onEntityMutation.mutationInterceptionFilter) {
6049
6055
  result.intercept = true;
6050
6056
  } else {
6051
- const filterResult = await plugin.mutationInterceptionFilter({
6057
+ const filterResult = await onEntityMutation.mutationInterceptionFilter({
6052
6058
  model: mutationModel,
6053
6059
  action,
6054
6060
  queryNode
@@ -6080,8 +6086,9 @@ ${compiled.parameters.map((p) => (0, import_node_util2.inspect)(p)).join("\n")}`
6080
6086
  if (this.options.plugins) {
6081
6087
  const mutationModel = this.getMutationModel(queryNode);
6082
6088
  for (const plugin of this.options.plugins) {
6083
- if (plugin.beforeEntityMutation) {
6084
- await plugin.beforeEntityMutation({
6089
+ const onEntityMutation = plugin.onEntityMutation;
6090
+ if (onEntityMutation?.beforeEntityMutation) {
6091
+ await onEntityMutation.beforeEntityMutation({
6085
6092
  model: mutationModel,
6086
6093
  action: mutationInterceptionInfo.action,
6087
6094
  queryNode,
@@ -6097,8 +6104,9 @@ ${compiled.parameters.map((p) => (0, import_node_util2.inspect)(p)).join("\n")}`
6097
6104
  }
6098
6105
  const hooks = [];
6099
6106
  for (const plugin of this.options.plugins ?? []) {
6100
- if (plugin.afterEntityMutation) {
6101
- hooks.push(plugin.afterEntityMutation.bind(plugin));
6107
+ const onEntityMutation = plugin.onEntityMutation;
6108
+ if (onEntityMutation?.afterEntityMutation) {
6109
+ hooks.push(onEntityMutation.afterEntityMutation.bind(plugin));
6102
6110
  }
6103
6111
  }
6104
6112
  if (hooks.length === 0) {
@@ -6896,7 +6904,7 @@ function createModelCrudHandler(client, model, inputValidator, resultProcessor)
6896
6904
  return createZenStackPromise(async (txClient) => {
6897
6905
  let proceed = /* @__PURE__ */ __name(async (_args) => {
6898
6906
  const _handler = txClient ? handler.withClient(txClient) : handler;
6899
- const r = await _handler.handle(operation, _args ?? args);
6907
+ const r = await _handler.handle(operation, _args);
6900
6908
  if (!r && throwIfNoResult) {
6901
6909
  throw new NotFoundError(model);
6902
6910
  }
@@ -6912,27 +6920,18 @@ function createModelCrudHandler(client, model, inputValidator, resultProcessor)
6912
6920
  ...client.$options.plugins ?? []
6913
6921
  ];
6914
6922
  for (const plugin of plugins) {
6915
- if (plugin.onQuery && typeof plugin.onQuery === "object") {
6916
- for (const [_model, modelHooks] of Object.entries(plugin.onQuery)) {
6917
- if (_model === (0, import_common_helpers13.lowerCaseFirst)(model) || _model === "$allModels") {
6918
- if (modelHooks && typeof modelHooks === "object") {
6919
- for (const [op, opHooks] of Object.entries(modelHooks)) {
6920
- if (op === operation || op === "$allOperations") {
6921
- if (typeof opHooks === "function") {
6922
- const _proceed = proceed;
6923
- proceed = /* @__PURE__ */ __name(() => opHooks({
6924
- client,
6925
- model,
6926
- operation,
6927
- args,
6928
- query: _proceed
6929
- }), "proceed");
6930
- }
6931
- }
6932
- }
6933
- }
6934
- }
6935
- }
6923
+ const onQuery = plugin.onQuery;
6924
+ if (onQuery) {
6925
+ const _proceed = proceed;
6926
+ proceed = /* @__PURE__ */ __name((_args) => onQuery({
6927
+ client,
6928
+ model,
6929
+ operation,
6930
+ // reflect the latest override if provided
6931
+ args: _args,
6932
+ // ensure inner overrides are propagated to the previous proceed
6933
+ proceed: /* @__PURE__ */ __name((nextArgs) => _proceed(nextArgs), "proceed")
6934
+ }), "proceed");
6936
6935
  }
6937
6936
  }
6938
6937
  return proceed(args);