@zenstackhq/plugin-policy 3.8.1 → 3.8.2

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
@@ -289,6 +289,10 @@ var ExpressionTransformer = class {
289
289
  else {
290
290
  const leftFieldDef = this.getFieldDefFromFieldRef(normalizedLeft, context);
291
291
  const comparand = leftFieldDef && _zenstackhq_orm.QueryUtils.isEnum(this.schema, leftFieldDef.type) ? this.dialect.castText(new kysely.ExpressionWrapper(left)).toOperationNode() : left;
292
+ if (kysely.SelectQueryNode.is(right)) {
293
+ const rightFieldDef = this.getFieldDefFromFieldRef(normalizedRight, context);
294
+ return this.dialect.buildArrayContains(new kysely.ExpressionWrapper(right), new kysely.ExpressionWrapper(comparand), rightFieldDef?.type).toOperationNode();
295
+ }
292
296
  return kysely.BinaryOperationNode.create(comparand, kysely.OperatorNode.create("="), kysely.FunctionNode.create("any", [right]));
293
297
  }
294
298
  if (this.isNullNode(right)) return this.transformNullCheck(left, expr.op);
@@ -307,13 +311,8 @@ var ExpressionTransformer = class {
307
311
  else return kysely.ValueNode.createImmediate(null);
308
312
  }
309
313
  normalizeBinaryOperationOperands(expr, context) {
310
- if (context.contextValue) return {
311
- normalizedLeft: expr.left,
312
- normalizedRight: expr.right
313
- };
314
314
  let normalizedLeft = expr.left;
315
315
  if (this.isRelationField(expr.left, context)) {
316
- (0, _zenstackhq_common_helpers.invariant)(_zenstackhq_orm_schema.ExpressionUtils.isNull(expr.right), "only null comparison is supported for relation field");
317
316
  const leftRelDef = this.getFieldDefFromFieldRef(expr.left, context);
318
317
  (0, _zenstackhq_common_helpers.invariant)(leftRelDef, "failed to get relation field definition");
319
318
  const idFields = _zenstackhq_orm.QueryUtils.requireIdFields(this.schema, leftRelDef.type);
@@ -321,7 +320,6 @@ var ExpressionTransformer = class {
321
320
  }
322
321
  let normalizedRight = expr.right;
323
322
  if (this.isRelationField(expr.right, context)) {
324
- (0, _zenstackhq_common_helpers.invariant)(_zenstackhq_orm_schema.ExpressionUtils.isNull(expr.left), "only null comparison is supported for relation field");
325
323
  const rightRelDef = this.getFieldDefFromFieldRef(expr.right, context);
326
324
  (0, _zenstackhq_common_helpers.invariant)(rightRelDef, "failed to get relation field definition");
327
325
  const idFields = _zenstackhq_orm.QueryUtils.requireIdFields(this.schema, rightRelDef.type);
@@ -334,7 +332,7 @@ var ExpressionTransformer = class {
334
332
  }
335
333
  transformCollectionPredicate(expr, context) {
336
334
  this.ensureCollectionPredicateOperator(expr.op);
337
- if (this.isAuthMember(expr.left) || context.contextValue) {
335
+ if (this.isAuthMember(expr.left) || context.contextValue && !this.isThisRootedMember(expr.left)) {
338
336
  (0, _zenstackhq_common_helpers.invariant)(_zenstackhq_orm_schema.ExpressionUtils.isMember(expr.left) || _zenstackhq_orm_schema.ExpressionUtils.isField(expr.left), "expected member or field expression");
339
337
  const receiver = new ExpressionEvaluator().evaluate(expr.left, {
340
338
  thisValue: context.contextValue,
@@ -373,6 +371,7 @@ var ExpressionTransformer = class {
373
371
  ...context,
374
372
  modelOrType: newContextModel,
375
373
  alias: void 0,
374
+ contextValue: void 0,
376
375
  bindingScope
377
376
  });
378
377
  if (expr.op === "!") predicateFilter = logicalNot(this.dialect, predicateFilter);
@@ -524,7 +523,12 @@ var ExpressionTransformer = class {
524
523
  });
525
524
  else {
526
525
  const firstMemberFieldDef = _zenstackhq_orm.QueryUtils.requireField(this.schema, context.thisType, expr.members[0]);
527
- receiver = this.transformRelationAccess(expr.members[0], firstMemberFieldDef.type, restContext);
526
+ receiver = this.transformRelationAccess(expr.members[0], firstMemberFieldDef.type, {
527
+ ...restContext,
528
+ alias: context.thisAlias,
529
+ modelOrType: context.thisType,
530
+ contextValue: void 0
531
+ });
528
532
  members = expr.members.slice(1);
529
533
  startType = firstMemberFieldDef.type;
530
534
  }
@@ -663,6 +667,9 @@ var ExpressionTransformer = class {
663
667
  isAuthMember(expr) {
664
668
  return _zenstackhq_orm_schema.ExpressionUtils.isMember(expr) && this.isAuthCall(expr.receiver);
665
669
  }
670
+ isThisRootedMember(expr) {
671
+ return _zenstackhq_orm_schema.ExpressionUtils.isMember(expr) && _zenstackhq_orm_schema.ExpressionUtils.isThis(expr.receiver);
672
+ }
666
673
  isNullNode(node) {
667
674
  return kysely.ValueNode.is(node) && node.value === null;
668
675
  }
@@ -679,19 +686,21 @@ var ExpressionTransformer = class {
679
686
  }
680
687
  getFieldDefFromFieldRef(expr, context) {
681
688
  const model = _zenstackhq_orm_schema.ExpressionUtils.isMember(expr) && _zenstackhq_orm_schema.ExpressionUtils.isThis(expr.receiver) ? context.thisType : context.modelOrType;
682
- if (_zenstackhq_orm_schema.ExpressionUtils.isField(expr)) return _zenstackhq_orm.QueryUtils.getField(this.schema, model, expr.field);
683
- else if (_zenstackhq_orm_schema.ExpressionUtils.isMember(expr) && expr.members.length === 1 && _zenstackhq_orm_schema.ExpressionUtils.isThis(expr.receiver)) return _zenstackhq_orm.QueryUtils.getField(this.schema, model, expr.members[0]);
684
- else if (_zenstackhq_orm_schema.ExpressionUtils.isMember(expr) && _zenstackhq_orm_schema.ExpressionUtils.isField(expr.receiver)) {
685
- const receiverDef = _zenstackhq_orm.QueryUtils.getField(this.schema, model, expr.receiver.field);
686
- if (!receiverDef?.relation) return void 0;
687
- let currModel = receiverDef.type;
688
- for (let i = 0; i < expr.members.length - 1; i++) {
689
- const hopDef = _zenstackhq_orm.QueryUtils.getField(this.schema, currModel, expr.members[i]);
689
+ const walkRelationChain = (startModel, members) => {
690
+ let currModel = startModel;
691
+ for (let i = 0; i < members.length - 1; i++) {
692
+ const hopDef = _zenstackhq_orm.QueryUtils.getField(this.schema, currModel, members[i]);
690
693
  if (!hopDef?.relation) return void 0;
691
694
  currModel = hopDef.type;
692
695
  }
693
- return _zenstackhq_orm.QueryUtils.getField(this.schema, currModel, expr.members[expr.members.length - 1]);
694
- } else return;
696
+ return _zenstackhq_orm.QueryUtils.getField(this.schema, currModel, members[members.length - 1]);
697
+ };
698
+ if (_zenstackhq_orm_schema.ExpressionUtils.isField(expr)) return _zenstackhq_orm.QueryUtils.getField(this.schema, model, expr.field);
699
+ else if (_zenstackhq_orm_schema.ExpressionUtils.isMember(expr)) {
700
+ if (_zenstackhq_orm_schema.ExpressionUtils.isThis(expr.receiver)) return walkRelationChain(model, expr.members);
701
+ else if (_zenstackhq_orm_schema.ExpressionUtils.isBinding(expr.receiver)) return walkRelationChain(this.requireBindingScope(expr.receiver, context).type, expr.members);
702
+ else if (_zenstackhq_orm_schema.ExpressionUtils.isField(expr.receiver)) return walkRelationChain(model, [expr.receiver.field, ...expr.members]);
703
+ }
695
704
  }
696
705
  };
697
706
  __decorate([