@zenstackhq/plugin-policy 3.9.7-beta.1 → 3.9.8
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 +51 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +51 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -7
package/dist/index.mjs
CHANGED
|
@@ -320,14 +320,14 @@ var ExpressionTransformer = class {
|
|
|
320
320
|
const leftRelDef = this.getFieldDefFromFieldRef(expr.left, context);
|
|
321
321
|
invariant(leftRelDef, "failed to get relation field definition");
|
|
322
322
|
const idFields = QueryUtils.requireIdFields(this.schema, leftRelDef.type);
|
|
323
|
-
normalizedLeft = this.
|
|
323
|
+
normalizedLeft = this.appendIdOrForeignKey(normalizedLeft, idFields[0], context);
|
|
324
324
|
}
|
|
325
325
|
let normalizedRight = expr.right;
|
|
326
326
|
if (this.isRelationField(expr.right, context)) {
|
|
327
327
|
const rightRelDef = this.getFieldDefFromFieldRef(expr.right, context);
|
|
328
328
|
invariant(rightRelDef, "failed to get relation field definition");
|
|
329
329
|
const idFields = QueryUtils.requireIdFields(this.schema, rightRelDef.type);
|
|
330
|
-
normalizedRight = this.
|
|
330
|
+
normalizedRight = this.appendIdOrForeignKey(normalizedRight, idFields[0], context);
|
|
331
331
|
}
|
|
332
332
|
return {
|
|
333
333
|
normalizedLeft,
|
|
@@ -470,7 +470,7 @@ var ExpressionTransformer = class {
|
|
|
470
470
|
if (!authModel) throw createUnsupportedError(`Unsupported use of \`auth()\` in policy of model "${context.modelOrType}", comparing with \`auth()\` is only possible when auth type is a model`);
|
|
471
471
|
const idFields = Object.values(authModel.fields).filter((f) => f.id).map((f) => f.name);
|
|
472
472
|
invariant(idFields.length > 0, "auth type model must have at least one id field");
|
|
473
|
-
const conditions = idFields.map((fieldName) => ExpressionUtils.binary(ExpressionUtils.member(authExpr, [fieldName]), "==", this.
|
|
473
|
+
const conditions = idFields.map((fieldName) => ExpressionUtils.binary(ExpressionUtils.member(authExpr, [fieldName]), "==", this.appendIdOrForeignKey(other, fieldName, context)));
|
|
474
474
|
let result = this.buildAnd(conditions);
|
|
475
475
|
if (expr.op === "!=") result = this.buildLogicalNot(result);
|
|
476
476
|
return this.transform(result, context);
|
|
@@ -480,6 +480,40 @@ var ExpressionTransformer = class {
|
|
|
480
480
|
if (ExpressionUtils.isMember(other)) return ExpressionUtils.member(other.receiver, [...other.members, fieldName]);
|
|
481
481
|
else return ExpressionUtils.member(other, [fieldName]);
|
|
482
482
|
}
|
|
483
|
+
/**
|
|
484
|
+
* Appends `idField` to a relation expression, i.e. `relation` -> `relation.id`. When the relation
|
|
485
|
+
* is SQL-backed, to-one, and owns the foreign key referencing `idField`, the terminal hop is
|
|
486
|
+
* replaced with the foreign key column instead (`relation.id` -> `relationId`), which avoids a
|
|
487
|
+
* correlated subquery into the related table that would only read back the same value.
|
|
488
|
+
*/
|
|
489
|
+
appendIdOrForeignKey(relationExpr, idField, context) {
|
|
490
|
+
if (this.isSqlBackedRef(relationExpr, context)) {
|
|
491
|
+
const resolved = this.resolveFieldRef(relationExpr, context);
|
|
492
|
+
const relation = resolved?.fieldDef.relation;
|
|
493
|
+
if (relation && !resolved.fieldDef.array && relation.fields && relation.references) {
|
|
494
|
+
const idx = relation.references.indexOf(idField);
|
|
495
|
+
const fk = idx >= 0 ? relation.fields[idx] : void 0;
|
|
496
|
+
if (fk) return this.replaceLastMember(relationExpr, fk);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
return this.makeOrAppendMember(relationExpr, idField);
|
|
500
|
+
}
|
|
501
|
+
replaceLastMember(expr, fieldName) {
|
|
502
|
+
if (ExpressionUtils.isMember(expr)) return ExpressionUtils.member(expr.receiver, [...expr.members.slice(0, -1), fieldName]);
|
|
503
|
+
else {
|
|
504
|
+
invariant(ExpressionUtils.isField(expr), "expected field or member expression");
|
|
505
|
+
return ExpressionUtils.field(fieldName);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
isSqlBackedRef(expr, context) {
|
|
509
|
+
if (ExpressionUtils.isField(expr)) return !context.contextValue;
|
|
510
|
+
if (ExpressionUtils.isMember(expr)) {
|
|
511
|
+
if (ExpressionUtils.isThis(expr.receiver)) return true;
|
|
512
|
+
if (ExpressionUtils.isField(expr.receiver)) return !context.contextValue;
|
|
513
|
+
if (ExpressionUtils.isBinding(expr.receiver)) return context.bindingScope?.[expr.receiver.name]?.value === void 0;
|
|
514
|
+
}
|
|
515
|
+
return false;
|
|
516
|
+
}
|
|
483
517
|
transformValue(value, type) {
|
|
484
518
|
if (value === true) return trueNode(this.dialect);
|
|
485
519
|
else if (value === false) return falseNode(this.dialect);
|
|
@@ -627,7 +661,8 @@ var ExpressionTransformer = class {
|
|
|
627
661
|
} else {
|
|
628
662
|
invariant(i === members.length - 1, "plain field access must be the last segment");
|
|
629
663
|
invariant(!currNode, "plain field access must be the last segment");
|
|
630
|
-
currNode =
|
|
664
|
+
if (fieldDef.originModel && fieldDef.originModel !== fromModel) currNode = this.buildDelegateBaseFieldSelect(fromModel, fromAlias, member, fieldDef.originModel);
|
|
665
|
+
else currNode = ReferenceNode.create(ColumnNode.create(member), TableNode.create(fromAlias));
|
|
631
666
|
}
|
|
632
667
|
}
|
|
633
668
|
return {
|
|
@@ -731,7 +766,17 @@ var ExpressionTransformer = class {
|
|
|
731
766
|
return !!this.getFieldDefFromFieldRef(expr, context)?.relation;
|
|
732
767
|
}
|
|
733
768
|
getFieldDefFromFieldRef(expr, context) {
|
|
769
|
+
return this.resolveFieldRef(expr, context)?.fieldDef;
|
|
770
|
+
}
|
|
771
|
+
resolveFieldRef(expr, context) {
|
|
734
772
|
const model = ExpressionUtils.isMember(expr) && ExpressionUtils.isThis(expr.receiver) ? context.thisType : context.modelOrType;
|
|
773
|
+
const resolveField = (currModel, field) => {
|
|
774
|
+
const fieldDef = QueryUtils.getField(this.schema, currModel, field);
|
|
775
|
+
return fieldDef ? {
|
|
776
|
+
model: currModel,
|
|
777
|
+
fieldDef
|
|
778
|
+
} : void 0;
|
|
779
|
+
};
|
|
735
780
|
const walkRelationChain = (startModel, members) => {
|
|
736
781
|
let currModel = startModel;
|
|
737
782
|
for (let i = 0; i < members.length - 1; i++) {
|
|
@@ -739,9 +784,9 @@ var ExpressionTransformer = class {
|
|
|
739
784
|
if (!hopDef?.relation) return void 0;
|
|
740
785
|
currModel = hopDef.type;
|
|
741
786
|
}
|
|
742
|
-
return
|
|
787
|
+
return resolveField(currModel, members[members.length - 1]);
|
|
743
788
|
};
|
|
744
|
-
if (ExpressionUtils.isField(expr)) return
|
|
789
|
+
if (ExpressionUtils.isField(expr)) return resolveField(model, expr.field);
|
|
745
790
|
else if (ExpressionUtils.isMember(expr)) {
|
|
746
791
|
if (ExpressionUtils.isThis(expr.receiver)) return walkRelationChain(model, expr.members);
|
|
747
792
|
else if (ExpressionUtils.isBinding(expr.receiver)) return walkRelationChain(this.requireBindingScope(expr.receiver, context).type, expr.members);
|