@zenstackhq/plugin-policy 3.10.0 → 3.10.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.mjs CHANGED
@@ -191,6 +191,13 @@ function createRejectedByPolicyError(model, reason, message) {
191
191
  function createUnsupportedError(message) {
192
192
  return new ORMError(ORMErrorReason.NOT_SUPPORTED, message);
193
193
  }
194
+ /**
195
+ * Throws a config error if the given field is a parameterized computed field. Such fields need
196
+ * query-time args, which policy expressions have no way to provide.
197
+ */
198
+ function rejectParameterizedComputedField(fieldDef, model) {
199
+ if (fieldDef.computed && fieldDef.params) throw new ORMError(ORMErrorReason.CONFIG_ERROR, `Parameterized computed field "${fieldDef.name}" of model "${model}" cannot be used in a policy expression`);
200
+ }
194
201
  //#endregion
195
202
  //#region \0@oxc-project+runtime@0.124.0/helpers/decorateMetadata.js
196
203
  function __decorateMetadata(k, v) {
@@ -673,8 +680,7 @@ var ExpressionTransformer = class {
673
680
  } else {
674
681
  invariant(i === members.length - 1, "plain field access must be the last segment");
675
682
  invariant(!currNode, "plain field access must be the last segment");
676
- if (fieldDef.originModel && fieldDef.originModel !== fromModel) currNode = this.buildDelegateBaseFieldSelect(fromModel, fromAlias, member, fieldDef.originModel);
677
- else currNode = ReferenceNode.create(ColumnNode.create(member), TableNode.create(fromAlias));
683
+ currNode = this.buildFieldRef(fromModel, member, fromAlias);
678
684
  }
679
685
  }
680
686
  return {
@@ -734,10 +740,18 @@ var ExpressionTransformer = class {
734
740
  return eb.selectFrom(`${m2m.otherModel} as ${relationAlias}`).innerJoin(`${m2m.joinTable} as ${joinTableAlias}`, (join) => join.onRef(`${relationAlias}.${m2m.otherPKName}`, "=", `${joinTableAlias}.${m2m.otherFkName}`).onRef(`${joinTableAlias}.${m2m.parentFkName}`, "=", `${context.alias ?? context.modelOrType}.${m2m.parentPKName}`)).toOperationNode();
735
741
  }
736
742
  createColumnRef(column, context) {
737
- const tableName = context.alias ?? context.modelOrType;
738
- const fieldDef = QueryUtils.requireField(this.schema, context.modelOrType, column);
739
- if (!fieldDef.originModel || fieldDef.originModel === context.modelOrType) return ReferenceNode.create(ColumnNode.create(column), TableNode.create(tableName));
740
- return this.buildDelegateBaseFieldSelect(context.modelOrType, tableName, column, fieldDef.originModel);
743
+ return this.buildFieldRef(context.modelOrType, column, context.alias ?? context.modelOrType);
744
+ }
745
+ /**
746
+ * Builds a reference to a scalar field of `model` whose table is aliased as `modelAlias`.
747
+ * Handles regular columns, `@computed` fields (inlined via the dialect), and fields inherited
748
+ * from a delegate base model (looked up from the base table with a correlated subquery).
749
+ */
750
+ buildFieldRef(model, field, modelAlias) {
751
+ const fieldDef = QueryUtils.requireField(this.schema, model, field);
752
+ rejectParameterizedComputedField(fieldDef, model);
753
+ if (!fieldDef.originModel || fieldDef.originModel === model) return fieldDef.computed ? this.dialect.fieldRef(model, field, modelAlias).toOperationNode() : ReferenceNode.create(ColumnNode.create(field), TableNode.create(modelAlias));
754
+ return this.buildDelegateBaseFieldSelect(model, modelAlias, field, fieldDef.originModel);
741
755
  }
742
756
  getEvaluationBindingScope(scope) {
743
757
  if (!scope) return;
@@ -750,7 +764,7 @@ var ExpressionTransformer = class {
750
764
  return {
751
765
  kind: "SelectQueryNode",
752
766
  from: FromNode.create([TableNode.create(baseModel)]),
753
- selections: [SelectionNode.create(ReferenceNode.create(ColumnNode.create(field), TableNode.create(baseModel)))],
767
+ selections: [SelectionNode.create(this.buildFieldRef(baseModel, field, baseModel))],
754
768
  where: WhereNode.create(conjunction(this.dialect, idFields.map((idField) => BinaryOperationNode.create(ReferenceNode.create(ColumnNode.create(idField), TableNode.create(baseModel)), OperatorNode.create("="), ReferenceNode.create(ColumnNode.create(idField), TableNode.create(modelAlias))))))
755
769
  };
756
770
  }
@@ -1158,6 +1172,10 @@ var PolicyHandler = class extends OperationNodeTransformer {
1158
1172
  const fieldDef = QueryUtils.getField(this.client.$schema, model, f);
1159
1173
  const owningTable = fieldDef?.originModel ?? model;
1160
1174
  if (fieldDef?.originModel) baseModelsToJoin.add(fieldDef.originModel);
1175
+ if (fieldDef?.computed) {
1176
+ rejectParameterizedComputedField(fieldDef, model);
1177
+ return SelectionNode.create(AliasNode.create(this.dialect.fieldRef(owningTable, f, owningTable).toOperationNode(), IdentifierNode.create(f)));
1178
+ }
1161
1179
  return SelectionNode.create(ReferenceNode.create(ColumnNode.create(f), TableNode.create(owningTable)));
1162
1180
  });
1163
1181
  const idFields = QueryUtils.requireIdFields(this.client.$schema, model);