@zenstackhq/runtime 3.0.0-alpha.22 → 3.0.0-alpha.24

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
@@ -5908,7 +5908,7 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely13
5908
5908
  await this.callBeforeMutationHooks(queryNode, mutationInterceptionInfo);
5909
5909
  }
5910
5910
  const oldQueryNode = queryNode;
5911
- if ((import_kysely13.InsertQueryNode.is(queryNode) || import_kysely13.UpdateQueryNode.is(queryNode)) && mutationInterceptionInfo?.loadAfterMutationEntity) {
5911
+ if ((import_kysely13.InsertQueryNode.is(queryNode) || import_kysely13.UpdateQueryNode.is(queryNode)) && mutationInterceptionInfo?.loadAfterMutationEntities) {
5912
5912
  queryNode = {
5913
5913
  ...queryNode,
5914
5914
  returning: import_kysely13.ReturningNode.create([
@@ -6015,7 +6015,7 @@ ${compiled.parameters.map((p) => (0, import_node_util2.inspect)(p)).join("\n")}`
6015
6015
  return newExecutor;
6016
6016
  }
6017
6017
  get hasMutationHooks() {
6018
- return this.client.$options.plugins?.some((plugin) => plugin.beforeEntityMutation || plugin.afterEntityMutation);
6018
+ return this.client.$options.plugins?.some((plugin) => !!plugin.onEntityMutation);
6019
6019
  }
6020
6020
  getMutationModel(queryNode) {
6021
6021
  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,21 +6045,25 @@ ${compiled.parameters.map((p) => (0, import_node_util2.inspect)(p)).join("\n")}`
6045
6045
  where: node.where
6046
6046
  })).exhaustive();
6047
6047
  for (const plugin of plugins) {
6048
- if (!plugin.mutationInterceptionFilter) {
6048
+ const onEntityMutation = plugin.onEntityMutation;
6049
+ if (!onEntityMutation) {
6050
+ continue;
6051
+ }
6052
+ if (!onEntityMutation.mutationInterceptionFilter) {
6049
6053
  result.intercept = true;
6050
6054
  } else {
6051
- const filterResult = await plugin.mutationInterceptionFilter({
6055
+ const filterResult = await onEntityMutation.mutationInterceptionFilter({
6052
6056
  model: mutationModel,
6053
6057
  action,
6054
6058
  queryNode
6055
6059
  });
6056
6060
  result.intercept ||= filterResult.intercept;
6057
- result.loadBeforeMutationEntity ||= filterResult.loadBeforeMutationEntity;
6058
- result.loadAfterMutationEntity ||= filterResult.loadAfterMutationEntity;
6061
+ result.loadBeforeMutationEntities ||= filterResult.loadBeforeMutationEntities;
6062
+ result.loadAfterMutationEntities ||= filterResult.loadAfterMutationEntities;
6059
6063
  }
6060
6064
  }
6061
6065
  let beforeMutationEntities;
6062
- if (result.loadBeforeMutationEntity && (import_kysely13.UpdateQueryNode.is(queryNode) || import_kysely13.DeleteQueryNode.is(queryNode))) {
6066
+ if (result.loadBeforeMutationEntities && (import_kysely13.UpdateQueryNode.is(queryNode) || import_kysely13.DeleteQueryNode.is(queryNode))) {
6063
6067
  beforeMutationEntities = await this.loadEntities(mutationModel, where);
6064
6068
  }
6065
6069
  return {
@@ -6080,8 +6084,9 @@ ${compiled.parameters.map((p) => (0, import_node_util2.inspect)(p)).join("\n")}`
6080
6084
  if (this.options.plugins) {
6081
6085
  const mutationModel = this.getMutationModel(queryNode);
6082
6086
  for (const plugin of this.options.plugins) {
6083
- if (plugin.beforeEntityMutation) {
6084
- await plugin.beforeEntityMutation({
6087
+ const onEntityMutation = plugin.onEntityMutation;
6088
+ if (onEntityMutation?.beforeEntityMutation) {
6089
+ await onEntityMutation.beforeEntityMutation({
6085
6090
  model: mutationModel,
6086
6091
  action: mutationInterceptionInfo.action,
6087
6092
  queryNode,
@@ -6097,8 +6102,9 @@ ${compiled.parameters.map((p) => (0, import_node_util2.inspect)(p)).join("\n")}`
6097
6102
  }
6098
6103
  const hooks = [];
6099
6104
  for (const plugin of this.options.plugins ?? []) {
6100
- if (plugin.afterEntityMutation) {
6101
- hooks.push(plugin.afterEntityMutation.bind(plugin));
6105
+ const onEntityMutation = plugin.onEntityMutation;
6106
+ if (onEntityMutation?.afterEntityMutation) {
6107
+ hooks.push(onEntityMutation.afterEntityMutation.bind(plugin));
6102
6108
  }
6103
6109
  }
6104
6110
  if (hooks.length === 0) {
@@ -6108,7 +6114,7 @@ ${compiled.parameters.map((p) => (0, import_node_util2.inspect)(p)).join("\n")}`
6108
6114
  const inTransaction = this.driver.isTransactionConnection(connection);
6109
6115
  for (const hook of hooks) {
6110
6116
  let afterMutationEntities = void 0;
6111
- if (mutationInterceptionInfo.loadAfterMutationEntity) {
6117
+ if (mutationInterceptionInfo.loadAfterMutationEntities) {
6112
6118
  if (import_kysely13.InsertQueryNode.is(queryNode) || import_kysely13.UpdateQueryNode.is(queryNode)) {
6113
6119
  afterMutationEntities = queryResult.rows;
6114
6120
  }
@@ -6896,7 +6902,7 @@ function createModelCrudHandler(client, model, inputValidator, resultProcessor)
6896
6902
  return createZenStackPromise(async (txClient) => {
6897
6903
  let proceed = /* @__PURE__ */ __name(async (_args) => {
6898
6904
  const _handler = txClient ? handler.withClient(txClient) : handler;
6899
- const r = await _handler.handle(operation, _args ?? args);
6905
+ const r = await _handler.handle(operation, _args);
6900
6906
  if (!r && throwIfNoResult) {
6901
6907
  throw new NotFoundError(model);
6902
6908
  }
@@ -6912,27 +6918,18 @@ function createModelCrudHandler(client, model, inputValidator, resultProcessor)
6912
6918
  ...client.$options.plugins ?? []
6913
6919
  ];
6914
6920
  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
- }
6921
+ const onQuery = plugin.onQuery;
6922
+ if (onQuery) {
6923
+ const _proceed = proceed;
6924
+ proceed = /* @__PURE__ */ __name((_args) => onQuery({
6925
+ client,
6926
+ model,
6927
+ operation,
6928
+ // reflect the latest override if provided
6929
+ args: _args,
6930
+ // ensure inner overrides are propagated to the previous proceed
6931
+ proceed: /* @__PURE__ */ __name((nextArgs) => _proceed(nextArgs), "proceed")
6932
+ }), "proceed");
6936
6933
  }
6937
6934
  }
6938
6935
  return proceed(args);