@zenstackhq/plugin-policy 3.9.6 → 3.9.7
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 +65 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +65 -15
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -7
package/dist/index.cjs
CHANGED
|
@@ -259,15 +259,19 @@ var ExpressionTransformer = class {
|
|
|
259
259
|
const fieldDef = _zenstackhq_orm.QueryUtils.requireField(this.schema, context.modelOrType, expr.field);
|
|
260
260
|
if (!fieldDef.relation) return this.createColumnRef(expr.field, context);
|
|
261
261
|
else {
|
|
262
|
-
const { memberFilter, memberSelect, memberAlias, ...restContext } = context;
|
|
262
|
+
const { memberFilter, memberSelect, memberExists, memberAlias, ...restContext } = context;
|
|
263
263
|
const relation = this.transformRelationAccess(expr.field, fieldDef.type, restContext, memberAlias);
|
|
264
|
-
return {
|
|
264
|
+
return this.finalizeMemberSubquery({
|
|
265
265
|
...relation,
|
|
266
266
|
where: this.mergeWhere(relation.where, memberFilter),
|
|
267
267
|
selections: memberSelect ? [memberSelect] : relation.selections
|
|
268
|
-
};
|
|
268
|
+
}, memberExists);
|
|
269
269
|
}
|
|
270
270
|
}
|
|
271
|
+
finalizeMemberSubquery(node, memberExists) {
|
|
272
|
+
if (!memberExists) return node;
|
|
273
|
+
return kysely.UnaryOperationNode.create(kysely.OperatorNode.create(memberExists), node);
|
|
274
|
+
}
|
|
271
275
|
mergeWhere(where, memberFilter) {
|
|
272
276
|
if (!where) return kysely.WhereNode.create(memberFilter ?? trueNode(this.dialect));
|
|
273
277
|
if (!memberFilter) return where;
|
|
@@ -317,14 +321,14 @@ var ExpressionTransformer = class {
|
|
|
317
321
|
const leftRelDef = this.getFieldDefFromFieldRef(expr.left, context);
|
|
318
322
|
(0, _zenstackhq_common_helpers.invariant)(leftRelDef, "failed to get relation field definition");
|
|
319
323
|
const idFields = _zenstackhq_orm.QueryUtils.requireIdFields(this.schema, leftRelDef.type);
|
|
320
|
-
normalizedLeft = this.
|
|
324
|
+
normalizedLeft = this.appendIdOrForeignKey(normalizedLeft, idFields[0], context);
|
|
321
325
|
}
|
|
322
326
|
let normalizedRight = expr.right;
|
|
323
327
|
if (this.isRelationField(expr.right, context)) {
|
|
324
328
|
const rightRelDef = this.getFieldDefFromFieldRef(expr.right, context);
|
|
325
329
|
(0, _zenstackhq_common_helpers.invariant)(rightRelDef, "failed to get relation field definition");
|
|
326
330
|
const idFields = _zenstackhq_orm.QueryUtils.requireIdFields(this.schema, rightRelDef.type);
|
|
327
|
-
normalizedRight = this.
|
|
331
|
+
normalizedRight = this.appendIdOrForeignKey(normalizedRight, idFields[0], context);
|
|
328
332
|
}
|
|
329
333
|
return {
|
|
330
334
|
normalizedLeft,
|
|
@@ -377,11 +381,11 @@ var ExpressionTransformer = class {
|
|
|
377
381
|
bindingScope
|
|
378
382
|
});
|
|
379
383
|
if (expr.op === "!") predicateFilter = logicalNot(this.dialect, predicateFilter);
|
|
380
|
-
const
|
|
381
|
-
const predicateResult = (0, ts_pattern.match)(expr.op).with("?", () => kysely.BinaryOperationNode.create(count, kysely.OperatorNode.create(">"), kysely.ValueNode.createImmediate(0))).with("!", () => kysely.BinaryOperationNode.create(count, kysely.OperatorNode.create("="), kysely.ValueNode.createImmediate(0))).with("^", () => kysely.BinaryOperationNode.create(count, kysely.OperatorNode.create("="), kysely.ValueNode.createImmediate(0))).exhaustive();
|
|
384
|
+
const memberExists = (0, ts_pattern.match)(expr.op).with("?", () => "exists").with("!", () => "not exists").with("^", () => "not exists").exhaustive();
|
|
382
385
|
return this.transform(expr.left, {
|
|
383
386
|
...context,
|
|
384
|
-
memberSelect: kysely.SelectionNode.create(kysely.AliasNode.create(
|
|
387
|
+
memberSelect: kysely.SelectionNode.create(kysely.AliasNode.create(kysely.ValueNode.createImmediate(1), kysely.IdentifierNode.create("_"))),
|
|
388
|
+
memberExists,
|
|
385
389
|
memberFilter: predicateFilter,
|
|
386
390
|
memberAlias
|
|
387
391
|
});
|
|
@@ -467,7 +471,7 @@ var ExpressionTransformer = class {
|
|
|
467
471
|
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`);
|
|
468
472
|
const idFields = Object.values(authModel.fields).filter((f) => f.id).map((f) => f.name);
|
|
469
473
|
(0, _zenstackhq_common_helpers.invariant)(idFields.length > 0, "auth type model must have at least one id field");
|
|
470
|
-
const conditions = idFields.map((fieldName) => _zenstackhq_orm_schema.ExpressionUtils.binary(_zenstackhq_orm_schema.ExpressionUtils.member(authExpr, [fieldName]), "==", this.
|
|
474
|
+
const conditions = idFields.map((fieldName) => _zenstackhq_orm_schema.ExpressionUtils.binary(_zenstackhq_orm_schema.ExpressionUtils.member(authExpr, [fieldName]), "==", this.appendIdOrForeignKey(other, fieldName, context)));
|
|
471
475
|
let result = this.buildAnd(conditions);
|
|
472
476
|
if (expr.op === "!=") result = this.buildLogicalNot(result);
|
|
473
477
|
return this.transform(result, context);
|
|
@@ -477,6 +481,40 @@ var ExpressionTransformer = class {
|
|
|
477
481
|
if (_zenstackhq_orm_schema.ExpressionUtils.isMember(other)) return _zenstackhq_orm_schema.ExpressionUtils.member(other.receiver, [...other.members, fieldName]);
|
|
478
482
|
else return _zenstackhq_orm_schema.ExpressionUtils.member(other, [fieldName]);
|
|
479
483
|
}
|
|
484
|
+
/**
|
|
485
|
+
* Appends `idField` to a relation expression, i.e. `relation` -> `relation.id`. When the relation
|
|
486
|
+
* is SQL-backed, to-one, and owns the foreign key referencing `idField`, the terminal hop is
|
|
487
|
+
* replaced with the foreign key column instead (`relation.id` -> `relationId`), which avoids a
|
|
488
|
+
* correlated subquery into the related table that would only read back the same value.
|
|
489
|
+
*/
|
|
490
|
+
appendIdOrForeignKey(relationExpr, idField, context) {
|
|
491
|
+
if (this.isSqlBackedRef(relationExpr, context)) {
|
|
492
|
+
const resolved = this.resolveFieldRef(relationExpr, context);
|
|
493
|
+
const relation = resolved?.fieldDef.relation;
|
|
494
|
+
if (relation && !resolved.fieldDef.array && relation.fields && relation.references) {
|
|
495
|
+
const idx = relation.references.indexOf(idField);
|
|
496
|
+
const fk = idx >= 0 ? relation.fields[idx] : void 0;
|
|
497
|
+
if (fk) return this.replaceLastMember(relationExpr, fk);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
return this.makeOrAppendMember(relationExpr, idField);
|
|
501
|
+
}
|
|
502
|
+
replaceLastMember(expr, fieldName) {
|
|
503
|
+
if (_zenstackhq_orm_schema.ExpressionUtils.isMember(expr)) return _zenstackhq_orm_schema.ExpressionUtils.member(expr.receiver, [...expr.members.slice(0, -1), fieldName]);
|
|
504
|
+
else {
|
|
505
|
+
(0, _zenstackhq_common_helpers.invariant)(_zenstackhq_orm_schema.ExpressionUtils.isField(expr), "expected field or member expression");
|
|
506
|
+
return _zenstackhq_orm_schema.ExpressionUtils.field(fieldName);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
isSqlBackedRef(expr, context) {
|
|
510
|
+
if (_zenstackhq_orm_schema.ExpressionUtils.isField(expr)) return !context.contextValue;
|
|
511
|
+
if (_zenstackhq_orm_schema.ExpressionUtils.isMember(expr)) {
|
|
512
|
+
if (_zenstackhq_orm_schema.ExpressionUtils.isThis(expr.receiver)) return true;
|
|
513
|
+
if (_zenstackhq_orm_schema.ExpressionUtils.isField(expr.receiver)) return !context.contextValue;
|
|
514
|
+
if (_zenstackhq_orm_schema.ExpressionUtils.isBinding(expr.receiver)) return context.bindingScope?.[expr.receiver.name]?.value === void 0;
|
|
515
|
+
}
|
|
516
|
+
return false;
|
|
517
|
+
}
|
|
480
518
|
transformValue(value, type) {
|
|
481
519
|
if (value === true) return trueNode(this.dialect);
|
|
482
520
|
else if (value === false) return falseNode(this.dialect);
|
|
@@ -535,7 +573,7 @@ var ExpressionTransformer = class {
|
|
|
535
573
|
let receiver;
|
|
536
574
|
let receiverAlias;
|
|
537
575
|
let startType;
|
|
538
|
-
const { memberFilter, memberSelect, memberAlias, ...restContext } = context;
|
|
576
|
+
const { memberFilter, memberSelect, memberExists, memberAlias, ...restContext } = context;
|
|
539
577
|
if (_zenstackhq_orm_schema.ExpressionUtils.isThis(expr.receiver)) if (expr.members.length === 1) return this._field(_zenstackhq_orm_schema.ExpressionUtils.field(expr.members[0]), {
|
|
540
578
|
...context,
|
|
541
579
|
alias: context.thisAlias,
|
|
@@ -616,15 +654,16 @@ var ExpressionTransformer = class {
|
|
|
616
654
|
...relation,
|
|
617
655
|
selections: [kysely.SelectionNode.create(kysely.AliasNode.create(currNode, kysely.IdentifierNode.create(members[i + 1])))]
|
|
618
656
|
};
|
|
619
|
-
else currNode = {
|
|
657
|
+
else currNode = this.finalizeMemberSubquery({
|
|
620
658
|
...relation,
|
|
621
659
|
where: this.mergeWhere(relation.where, memberFilter),
|
|
622
660
|
selections: memberSelect ? [memberSelect] : relation.selections
|
|
623
|
-
};
|
|
661
|
+
}, memberExists);
|
|
624
662
|
} else {
|
|
625
663
|
(0, _zenstackhq_common_helpers.invariant)(i === members.length - 1, "plain field access must be the last segment");
|
|
626
664
|
(0, _zenstackhq_common_helpers.invariant)(!currNode, "plain field access must be the last segment");
|
|
627
|
-
currNode =
|
|
665
|
+
if (fieldDef.originModel && fieldDef.originModel !== fromModel) currNode = this.buildDelegateBaseFieldSelect(fromModel, fromAlias, member, fieldDef.originModel);
|
|
666
|
+
else currNode = kysely.ReferenceNode.create(kysely.ColumnNode.create(member), kysely.TableNode.create(fromAlias));
|
|
628
667
|
}
|
|
629
668
|
}
|
|
630
669
|
return {
|
|
@@ -728,7 +767,17 @@ var ExpressionTransformer = class {
|
|
|
728
767
|
return !!this.getFieldDefFromFieldRef(expr, context)?.relation;
|
|
729
768
|
}
|
|
730
769
|
getFieldDefFromFieldRef(expr, context) {
|
|
770
|
+
return this.resolveFieldRef(expr, context)?.fieldDef;
|
|
771
|
+
}
|
|
772
|
+
resolveFieldRef(expr, context) {
|
|
731
773
|
const model = _zenstackhq_orm_schema.ExpressionUtils.isMember(expr) && _zenstackhq_orm_schema.ExpressionUtils.isThis(expr.receiver) ? context.thisType : context.modelOrType;
|
|
774
|
+
const resolveField = (currModel, field) => {
|
|
775
|
+
const fieldDef = _zenstackhq_orm.QueryUtils.getField(this.schema, currModel, field);
|
|
776
|
+
return fieldDef ? {
|
|
777
|
+
model: currModel,
|
|
778
|
+
fieldDef
|
|
779
|
+
} : void 0;
|
|
780
|
+
};
|
|
732
781
|
const walkRelationChain = (startModel, members) => {
|
|
733
782
|
let currModel = startModel;
|
|
734
783
|
for (let i = 0; i < members.length - 1; i++) {
|
|
@@ -736,13 +785,14 @@ var ExpressionTransformer = class {
|
|
|
736
785
|
if (!hopDef?.relation) return void 0;
|
|
737
786
|
currModel = hopDef.type;
|
|
738
787
|
}
|
|
739
|
-
return
|
|
788
|
+
return resolveField(currModel, members[members.length - 1]);
|
|
740
789
|
};
|
|
741
|
-
if (_zenstackhq_orm_schema.ExpressionUtils.isField(expr)) return
|
|
790
|
+
if (_zenstackhq_orm_schema.ExpressionUtils.isField(expr)) return resolveField(model, expr.field);
|
|
742
791
|
else if (_zenstackhq_orm_schema.ExpressionUtils.isMember(expr)) {
|
|
743
792
|
if (_zenstackhq_orm_schema.ExpressionUtils.isThis(expr.receiver)) return walkRelationChain(model, expr.members);
|
|
744
793
|
else if (_zenstackhq_orm_schema.ExpressionUtils.isBinding(expr.receiver)) return walkRelationChain(this.requireBindingScope(expr.receiver, context).type, expr.members);
|
|
745
794
|
else if (_zenstackhq_orm_schema.ExpressionUtils.isField(expr.receiver)) return walkRelationChain(model, [expr.receiver.field, ...expr.members]);
|
|
795
|
+
else if (this.isAuthCall(expr.receiver)) return walkRelationChain(this.authType, expr.members);
|
|
746
796
|
}
|
|
747
797
|
}
|
|
748
798
|
};
|