@zenstackhq/plugin-policy 3.5.6 → 3.6.0
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 +1374 -1948
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +28 -21
- package/dist/index.d.mts +34 -0
- package/dist/index.mjs +1421 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +12 -11
- package/dist/index.d.ts +0 -27
- package/dist/index.js +0 -1971
- package/dist/index.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,1996 +1,1422 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let _zenstackhq_orm = require("@zenstackhq/orm");
|
|
3
|
+
let _zenstackhq_common_helpers = require("@zenstackhq/common-helpers");
|
|
4
|
+
let kysely = require("kysely");
|
|
5
|
+
let _zenstackhq_orm_schema = require("@zenstackhq/orm/schema");
|
|
6
|
+
let ts_pattern = require("ts-pattern");
|
|
7
|
+
//#region src/column-collector.ts
|
|
8
|
+
/**
|
|
9
|
+
* Collects all column names from a query.
|
|
10
|
+
*/
|
|
11
|
+
var ColumnCollector = class extends _zenstackhq_orm.KyselyUtils.DefaultOperationNodeVisitor {
|
|
12
|
+
columns = [];
|
|
13
|
+
collect(node) {
|
|
14
|
+
this.columns = [];
|
|
15
|
+
this.visitNode(node);
|
|
16
|
+
return this.columns;
|
|
17
|
+
}
|
|
18
|
+
visitColumn(node) {
|
|
19
|
+
if (!this.columns.includes(node.column.name)) this.columns.push(node.column.name);
|
|
20
|
+
}
|
|
10
21
|
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
-
|
|
21
|
-
// src/index.ts
|
|
22
|
-
var src_exports = {};
|
|
23
|
-
__export(src_exports, {
|
|
24
|
-
PolicyPlugin: () => PolicyPlugin
|
|
25
|
-
});
|
|
26
|
-
module.exports = __toCommonJS(src_exports);
|
|
27
|
-
|
|
28
|
-
// src/functions.ts
|
|
29
|
-
var import_common_helpers4 = require("@zenstackhq/common-helpers");
|
|
30
|
-
var import_orm5 = require("@zenstackhq/orm");
|
|
31
|
-
var import_kysely4 = require("kysely");
|
|
32
|
-
|
|
33
|
-
// src/policy-handler.ts
|
|
34
|
-
var import_common_helpers3 = require("@zenstackhq/common-helpers");
|
|
35
|
-
var import_orm4 = require("@zenstackhq/orm");
|
|
36
|
-
var import_schema4 = require("@zenstackhq/orm/schema");
|
|
37
|
-
var import_kysely3 = require("kysely");
|
|
38
|
-
var import_ts_pattern3 = require("ts-pattern");
|
|
39
|
-
|
|
40
|
-
// src/column-collector.ts
|
|
41
|
-
var import_orm = require("@zenstackhq/orm");
|
|
42
|
-
var ColumnCollector = class extends import_orm.KyselyUtils.DefaultOperationNodeVisitor {
|
|
43
|
-
static {
|
|
44
|
-
__name(this, "ColumnCollector");
|
|
45
|
-
}
|
|
46
|
-
columns = [];
|
|
47
|
-
collect(node) {
|
|
48
|
-
this.columns = [];
|
|
49
|
-
this.visitNode(node);
|
|
50
|
-
return this.columns;
|
|
51
|
-
}
|
|
52
|
-
visitColumn(node) {
|
|
53
|
-
if (!this.columns.includes(node.column.name)) {
|
|
54
|
-
this.columns.push(node.column.name);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
// src/expression-transformer.ts
|
|
60
|
-
var import_common_helpers2 = require("@zenstackhq/common-helpers");
|
|
61
|
-
var import_orm3 = require("@zenstackhq/orm");
|
|
62
|
-
var import_schema3 = require("@zenstackhq/orm/schema");
|
|
63
|
-
var import_kysely2 = require("kysely");
|
|
64
|
-
var import_ts_pattern2 = require("ts-pattern");
|
|
65
|
-
|
|
66
|
-
// src/expression-evaluator.ts
|
|
67
|
-
var import_common_helpers = require("@zenstackhq/common-helpers");
|
|
68
|
-
var import_schema = require("@zenstackhq/orm/schema");
|
|
69
|
-
var import_ts_pattern = require("ts-pattern");
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/expression-evaluator.ts
|
|
24
|
+
/**
|
|
25
|
+
* Utility for evaluating a ZModel expression into a JavaScript value.
|
|
26
|
+
*/
|
|
70
27
|
var ExpressionEvaluator = class {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
[expr2.binding]: item
|
|
143
|
-
} : context.bindingScope
|
|
144
|
-
}))).with("!", () => left.every((item) => this.evaluate(expr2.right, {
|
|
145
|
-
...context,
|
|
146
|
-
thisValue: item,
|
|
147
|
-
bindingScope: expr2.binding ? {
|
|
148
|
-
...context.bindingScope ?? {},
|
|
149
|
-
[expr2.binding]: item
|
|
150
|
-
} : context.bindingScope
|
|
151
|
-
}))).with("^", () => !left.some((item) => this.evaluate(expr2.right, {
|
|
152
|
-
...context,
|
|
153
|
-
thisValue: item,
|
|
154
|
-
bindingScope: expr2.binding ? {
|
|
155
|
-
...context.bindingScope ?? {},
|
|
156
|
-
[expr2.binding]: item
|
|
157
|
-
} : context.bindingScope
|
|
158
|
-
}))).exhaustive();
|
|
159
|
-
}
|
|
160
|
-
evaluateBinding(expr2, context) {
|
|
161
|
-
if (!context.bindingScope || !(expr2.name in context.bindingScope)) {
|
|
162
|
-
throw new Error(`Unresolved binding: ${expr2.name}`);
|
|
163
|
-
}
|
|
164
|
-
return context.bindingScope[expr2.name];
|
|
165
|
-
}
|
|
28
|
+
evaluate(expression, context) {
|
|
29
|
+
return (0, ts_pattern.match)(expression).when(_zenstackhq_orm_schema.ExpressionUtils.isArray, (expr) => this.evaluateArray(expr, context)).when(_zenstackhq_orm_schema.ExpressionUtils.isBinary, (expr) => this.evaluateBinary(expr, context)).when(_zenstackhq_orm_schema.ExpressionUtils.isField, (expr) => this.evaluateField(expr, context)).when(_zenstackhq_orm_schema.ExpressionUtils.isLiteral, (expr) => this.evaluateLiteral(expr)).when(_zenstackhq_orm_schema.ExpressionUtils.isMember, (expr) => this.evaluateMember(expr, context)).when(_zenstackhq_orm_schema.ExpressionUtils.isUnary, (expr) => this.evaluateUnary(expr, context)).when(_zenstackhq_orm_schema.ExpressionUtils.isCall, (expr) => this.evaluateCall(expr, context)).when(_zenstackhq_orm_schema.ExpressionUtils.isBinding, (expr) => this.evaluateBinding(expr, context)).when(_zenstackhq_orm_schema.ExpressionUtils.isThis, () => context.thisValue).when(_zenstackhq_orm_schema.ExpressionUtils.isNull, () => null).exhaustive() ?? null;
|
|
30
|
+
}
|
|
31
|
+
evaluateCall(expr, context) {
|
|
32
|
+
if (expr.function === "auth") return context.auth;
|
|
33
|
+
else if (expr.function === "currentModel") return context.thisType;
|
|
34
|
+
else if (expr.function === "currentOperation") return context.operation;
|
|
35
|
+
else throw new Error(`Unsupported call expression function: ${expr.function}`);
|
|
36
|
+
}
|
|
37
|
+
evaluateUnary(expr, context) {
|
|
38
|
+
return (0, ts_pattern.match)(expr.op).with("!", () => !this.evaluate(expr.operand, context)).exhaustive();
|
|
39
|
+
}
|
|
40
|
+
evaluateMember(expr, context) {
|
|
41
|
+
let val = this.evaluate(expr.receiver, context);
|
|
42
|
+
for (const member of expr.members) val = val?.[member];
|
|
43
|
+
return val;
|
|
44
|
+
}
|
|
45
|
+
evaluateLiteral(expr) {
|
|
46
|
+
return expr.value;
|
|
47
|
+
}
|
|
48
|
+
evaluateField(expr, context) {
|
|
49
|
+
if (context.bindingScope && expr.field in context.bindingScope) return context.bindingScope[expr.field];
|
|
50
|
+
return context.thisValue?.[expr.field];
|
|
51
|
+
}
|
|
52
|
+
evaluateArray(expr, context) {
|
|
53
|
+
return expr.items.map((item) => this.evaluate(item, context));
|
|
54
|
+
}
|
|
55
|
+
evaluateBinary(expr, context) {
|
|
56
|
+
if (expr.op === "?" || expr.op === "!" || expr.op === "^") return this.evaluateCollectionPredicate(expr, context);
|
|
57
|
+
const left = this.evaluate(expr.left, context);
|
|
58
|
+
const right = this.evaluate(expr.right, context);
|
|
59
|
+
if (!["==", "!="].includes(expr.op) && (left === null || right === null)) return null;
|
|
60
|
+
return (0, ts_pattern.match)(expr.op).with("==", () => left === right).with("!=", () => left !== right).with(">", () => left > right).with(">=", () => left >= right).with("<", () => left < right).with("<=", () => left <= right).with("&&", () => left && right).with("||", () => left || right).with("in", () => {
|
|
61
|
+
const _right = right ?? [];
|
|
62
|
+
(0, _zenstackhq_common_helpers.invariant)(Array.isArray(_right), "expected array for \"in\" operator");
|
|
63
|
+
return _right.includes(left);
|
|
64
|
+
}).exhaustive();
|
|
65
|
+
}
|
|
66
|
+
evaluateCollectionPredicate(expr, context) {
|
|
67
|
+
const op = expr.op;
|
|
68
|
+
(0, _zenstackhq_common_helpers.invariant)(op === "?" || op === "!" || op === "^", "expected \"?\" or \"!\" or \"^\" operator");
|
|
69
|
+
const left = this.evaluate(expr.left, context);
|
|
70
|
+
if (left === null || left === void 0) return null;
|
|
71
|
+
(0, _zenstackhq_common_helpers.invariant)(Array.isArray(left), "expected array");
|
|
72
|
+
return (0, ts_pattern.match)(op).with("?", () => left.some((item) => this.evaluate(expr.right, {
|
|
73
|
+
...context,
|
|
74
|
+
thisValue: item,
|
|
75
|
+
bindingScope: expr.binding ? {
|
|
76
|
+
...context.bindingScope ?? {},
|
|
77
|
+
[expr.binding]: item
|
|
78
|
+
} : context.bindingScope
|
|
79
|
+
}))).with("!", () => left.every((item) => this.evaluate(expr.right, {
|
|
80
|
+
...context,
|
|
81
|
+
thisValue: item,
|
|
82
|
+
bindingScope: expr.binding ? {
|
|
83
|
+
...context.bindingScope ?? {},
|
|
84
|
+
[expr.binding]: item
|
|
85
|
+
} : context.bindingScope
|
|
86
|
+
}))).with("^", () => !left.some((item) => this.evaluate(expr.right, {
|
|
87
|
+
...context,
|
|
88
|
+
thisValue: item,
|
|
89
|
+
bindingScope: expr.binding ? {
|
|
90
|
+
...context.bindingScope ?? {},
|
|
91
|
+
[expr.binding]: item
|
|
92
|
+
} : context.bindingScope
|
|
93
|
+
}))).exhaustive();
|
|
94
|
+
}
|
|
95
|
+
evaluateBinding(expr, context) {
|
|
96
|
+
if (!context.bindingScope || !(expr.name in context.bindingScope)) throw new Error(`Unresolved binding: ${expr.name}`);
|
|
97
|
+
return context.bindingScope[expr.name];
|
|
98
|
+
}
|
|
166
99
|
};
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
100
|
+
//#endregion
|
|
101
|
+
//#region src/types.ts
|
|
102
|
+
/**
|
|
103
|
+
* Operators allowed for collection predicate expressions.
|
|
104
|
+
*/
|
|
105
|
+
const CollectionPredicateOperator = [
|
|
106
|
+
"?",
|
|
107
|
+
"!",
|
|
108
|
+
"^"
|
|
173
109
|
];
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
110
|
+
//#endregion
|
|
111
|
+
//#region src/utils.ts
|
|
112
|
+
/**
|
|
113
|
+
* Creates a `true` value node.
|
|
114
|
+
*/
|
|
179
115
|
function trueNode(dialect) {
|
|
180
|
-
|
|
116
|
+
return kysely.ValueNode.createImmediate(dialect.transformInput(true, "Boolean", false));
|
|
181
117
|
}
|
|
182
|
-
|
|
118
|
+
/**
|
|
119
|
+
* Creates a `false` value node.
|
|
120
|
+
*/
|
|
183
121
|
function falseNode(dialect) {
|
|
184
|
-
|
|
122
|
+
return kysely.ValueNode.createImmediate(dialect.transformInput(false, "Boolean", false));
|
|
185
123
|
}
|
|
186
|
-
|
|
124
|
+
/**
|
|
125
|
+
* Checks if a node is a truthy value node.
|
|
126
|
+
*/
|
|
187
127
|
function isTrueNode(node) {
|
|
188
|
-
|
|
128
|
+
return kysely.ValueNode.is(node) && (node.value === true || node.value === 1);
|
|
189
129
|
}
|
|
190
|
-
|
|
130
|
+
/**
|
|
131
|
+
* Checks if a node is a falsy value node.
|
|
132
|
+
*/
|
|
191
133
|
function isFalseNode(node) {
|
|
192
|
-
|
|
134
|
+
return kysely.ValueNode.is(node) && (node.value === false || node.value === 0);
|
|
193
135
|
}
|
|
194
|
-
|
|
136
|
+
/**
|
|
137
|
+
* Builds a logical conjunction of a list of nodes.
|
|
138
|
+
*/
|
|
195
139
|
function conjunction(dialect, nodes) {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
if (nodes.some(isFalseNode)) {
|
|
203
|
-
return falseNode(dialect);
|
|
204
|
-
}
|
|
205
|
-
const items = nodes.filter((n) => !isTrueNode(n));
|
|
206
|
-
if (items.length === 0) {
|
|
207
|
-
return trueNode(dialect);
|
|
208
|
-
}
|
|
209
|
-
return items.reduce((acc, node) => import_kysely.AndNode.create(wrapParensIf(acc, import_kysely.OrNode.is), wrapParensIf(node, import_kysely.OrNode.is)));
|
|
140
|
+
if (nodes.length === 0) return trueNode(dialect);
|
|
141
|
+
if (nodes.length === 1) return nodes[0];
|
|
142
|
+
if (nodes.some(isFalseNode)) return falseNode(dialect);
|
|
143
|
+
const items = nodes.filter((n) => !isTrueNode(n));
|
|
144
|
+
if (items.length === 0) return trueNode(dialect);
|
|
145
|
+
return items.reduce((acc, node) => kysely.AndNode.create(wrapParensIf(acc, kysely.OrNode.is), wrapParensIf(node, kysely.OrNode.is)));
|
|
210
146
|
}
|
|
211
|
-
__name(conjunction, "conjunction");
|
|
212
147
|
function disjunction(dialect, nodes) {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
if (nodes.some(isTrueNode)) {
|
|
220
|
-
return trueNode(dialect);
|
|
221
|
-
}
|
|
222
|
-
const items = nodes.filter((n) => !isFalseNode(n));
|
|
223
|
-
if (items.length === 0) {
|
|
224
|
-
return falseNode(dialect);
|
|
225
|
-
}
|
|
226
|
-
return items.reduce((acc, node) => import_kysely.OrNode.create(wrapParensIf(acc, import_kysely.AndNode.is), wrapParensIf(node, import_kysely.AndNode.is)));
|
|
148
|
+
if (nodes.length === 0) return falseNode(dialect);
|
|
149
|
+
if (nodes.length === 1) return nodes[0];
|
|
150
|
+
if (nodes.some(isTrueNode)) return trueNode(dialect);
|
|
151
|
+
const items = nodes.filter((n) => !isFalseNode(n));
|
|
152
|
+
if (items.length === 0) return falseNode(dialect);
|
|
153
|
+
return items.reduce((acc, node) => kysely.OrNode.create(wrapParensIf(acc, kysely.AndNode.is), wrapParensIf(node, kysely.AndNode.is)));
|
|
227
154
|
}
|
|
228
|
-
|
|
155
|
+
/**
|
|
156
|
+
* Negates a logical expression.
|
|
157
|
+
*/
|
|
229
158
|
function logicalNot(dialect, node) {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
if (isFalseNode(node)) {
|
|
234
|
-
return trueNode(dialect);
|
|
235
|
-
}
|
|
236
|
-
return import_kysely.UnaryOperationNode.create(import_kysely.OperatorNode.create("not"), wrapParensIf(node, (n) => import_kysely.AndNode.is(n) || import_kysely.OrNode.is(n)));
|
|
159
|
+
if (isTrueNode(node)) return falseNode(dialect);
|
|
160
|
+
if (isFalseNode(node)) return trueNode(dialect);
|
|
161
|
+
return kysely.UnaryOperationNode.create(kysely.OperatorNode.create("not"), wrapParensIf(node, (n) => kysely.AndNode.is(n) || kysely.OrNode.is(n)));
|
|
237
162
|
}
|
|
238
|
-
__name(logicalNot, "logicalNot");
|
|
239
163
|
function wrapParensIf(node, predicate) {
|
|
240
|
-
|
|
164
|
+
return predicate(node) ? kysely.ParensNode.create(node) : node;
|
|
241
165
|
}
|
|
242
|
-
|
|
166
|
+
/**
|
|
167
|
+
* Builds an expression node that checks if a node is false.
|
|
168
|
+
*/
|
|
243
169
|
function buildIsFalse(node, dialect) {
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
return falseNode(dialect);
|
|
248
|
-
}
|
|
249
|
-
return import_kysely.BinaryOperationNode.create(
|
|
250
|
-
// coalesce so null is treated as false
|
|
251
|
-
import_kysely.FunctionNode.create("coalesce", [
|
|
252
|
-
node,
|
|
253
|
-
falseNode(dialect)
|
|
254
|
-
]),
|
|
255
|
-
import_kysely.OperatorNode.create("="),
|
|
256
|
-
falseNode(dialect)
|
|
257
|
-
);
|
|
170
|
+
if (isFalseNode(node)) return trueNode(dialect);
|
|
171
|
+
else if (isTrueNode(node)) return falseNode(dialect);
|
|
172
|
+
return kysely.BinaryOperationNode.create(kysely.FunctionNode.create("coalesce", [node, falseNode(dialect)]), kysely.OperatorNode.create("="), falseNode(dialect));
|
|
258
173
|
}
|
|
259
|
-
|
|
174
|
+
/**
|
|
175
|
+
* Gets the table name from a node.
|
|
176
|
+
*/
|
|
260
177
|
function getTableName(node) {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
return node.table.identifier.name;
|
|
266
|
-
} else if (import_kysely.AliasNode.is(node)) {
|
|
267
|
-
return getTableName(node.node);
|
|
268
|
-
} else if (import_kysely.ReferenceNode.is(node) && node.table) {
|
|
269
|
-
return getTableName(node.table);
|
|
270
|
-
}
|
|
271
|
-
return void 0;
|
|
178
|
+
if (!node) return node;
|
|
179
|
+
if (kysely.TableNode.is(node)) return node.table.identifier.name;
|
|
180
|
+
else if (kysely.AliasNode.is(node)) return getTableName(node.node);
|
|
181
|
+
else if (kysely.ReferenceNode.is(node) && node.table) return getTableName(node.table);
|
|
272
182
|
}
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
return import_schema2.ExpressionUtils.isCall(expr2) && expr2.function === "before";
|
|
183
|
+
function isBeforeInvocation(expr) {
|
|
184
|
+
return _zenstackhq_orm_schema.ExpressionUtils.isCall(expr) && expr.function === "before";
|
|
276
185
|
}
|
|
277
|
-
__name(isBeforeInvocation, "isBeforeInvocation");
|
|
278
186
|
function createRejectedByPolicyError(model, reason, message) {
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
187
|
+
const err = new _zenstackhq_orm.ORMError(_zenstackhq_orm.ORMErrorReason.REJECTED_BY_POLICY, message ?? "operation is rejected by access policies");
|
|
188
|
+
err.rejectedByPolicyReason = reason;
|
|
189
|
+
err.model = model;
|
|
190
|
+
return err;
|
|
283
191
|
}
|
|
284
|
-
__name(createRejectedByPolicyError, "createRejectedByPolicyError");
|
|
285
192
|
function createUnsupportedError(message) {
|
|
286
|
-
|
|
193
|
+
return new _zenstackhq_orm.ORMError(_zenstackhq_orm.ORMErrorReason.NOT_SUPPORTED, message);
|
|
287
194
|
}
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
293
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
294
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
295
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
195
|
+
//#endregion
|
|
196
|
+
//#region \0@oxc-project+runtime@0.124.0/helpers/decorateMetadata.js
|
|
197
|
+
function __decorateMetadata(k, v) {
|
|
198
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
296
199
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
200
|
+
//#endregion
|
|
201
|
+
//#region \0@oxc-project+runtime@0.124.0/helpers/decorate.js
|
|
202
|
+
function __decorate(decorators, target, key, desc) {
|
|
203
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
204
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
205
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
206
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
300
207
|
}
|
|
301
|
-
|
|
302
|
-
|
|
208
|
+
//#endregion
|
|
209
|
+
//#region src/expression-transformer.ts
|
|
210
|
+
const expressionHandlers = /* @__PURE__ */ new Map();
|
|
303
211
|
function expr(kind) {
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
return descriptor;
|
|
309
|
-
};
|
|
212
|
+
return function(_target, _propertyKey, descriptor) {
|
|
213
|
+
if (!expressionHandlers.get(kind)) expressionHandlers.set(kind, descriptor);
|
|
214
|
+
return descriptor;
|
|
215
|
+
};
|
|
310
216
|
}
|
|
311
|
-
|
|
217
|
+
/**
|
|
218
|
+
* Utility for transforming a ZModel expression into a Kysely OperationNode.
|
|
219
|
+
*/
|
|
312
220
|
var ExpressionTransformer = class {
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
currNode = {
|
|
786
|
-
...relation,
|
|
787
|
-
selections: [
|
|
788
|
-
import_kysely2.SelectionNode.create(import_kysely2.AliasNode.create(currNode, import_kysely2.IdentifierNode.create(members[i + 1])))
|
|
789
|
-
]
|
|
790
|
-
};
|
|
791
|
-
} else {
|
|
792
|
-
currNode = {
|
|
793
|
-
...relation,
|
|
794
|
-
where: this.mergeWhere(relation.where, memberFilter),
|
|
795
|
-
selections: memberSelect ? [
|
|
796
|
-
memberSelect
|
|
797
|
-
] : relation.selections
|
|
798
|
-
};
|
|
799
|
-
}
|
|
800
|
-
} else {
|
|
801
|
-
(0, import_common_helpers2.invariant)(i === members.length - 1, "plain field access must be the last segment");
|
|
802
|
-
(0, import_common_helpers2.invariant)(!currNode, "plain field access must be the last segment");
|
|
803
|
-
currNode = import_kysely2.ColumnNode.create(member);
|
|
804
|
-
}
|
|
805
|
-
}
|
|
806
|
-
return {
|
|
807
|
-
...receiver,
|
|
808
|
-
selections: [
|
|
809
|
-
import_kysely2.SelectionNode.create(import_kysely2.AliasNode.create(currNode, import_kysely2.IdentifierNode.create("_")))
|
|
810
|
-
]
|
|
811
|
-
};
|
|
812
|
-
}
|
|
813
|
-
requireBindingScope(expr2, context) {
|
|
814
|
-
const binding = context.bindingScope?.[expr2.name];
|
|
815
|
-
(0, import_common_helpers2.invariant)(binding, `binding not found: ${expr2.name}`);
|
|
816
|
-
return binding;
|
|
817
|
-
}
|
|
818
|
-
valueMemberAccess(receiver, expr2, receiverType) {
|
|
819
|
-
if (!receiver) {
|
|
820
|
-
return import_kysely2.ValueNode.createImmediate(null);
|
|
821
|
-
}
|
|
822
|
-
(0, import_common_helpers2.invariant)(expr2.members.length > 0, "member expression must have at least one member");
|
|
823
|
-
let curr = receiver;
|
|
824
|
-
let currType = receiverType;
|
|
825
|
-
for (let i = 0; i < expr2.members.length; i++) {
|
|
826
|
-
const field = expr2.members[i];
|
|
827
|
-
curr = curr?.[field];
|
|
828
|
-
if (curr === void 0) {
|
|
829
|
-
curr = import_kysely2.ValueNode.createImmediate(null);
|
|
830
|
-
break;
|
|
831
|
-
}
|
|
832
|
-
currType = import_orm3.QueryUtils.requireField(this.schema, currType, field).type;
|
|
833
|
-
if (i === expr2.members.length - 1) {
|
|
834
|
-
curr = this.transformValue(curr, currType);
|
|
835
|
-
}
|
|
836
|
-
}
|
|
837
|
-
return curr;
|
|
838
|
-
}
|
|
839
|
-
transformRelationAccess(field, relationModel, context) {
|
|
840
|
-
const m2m = import_orm3.QueryUtils.getManyToManyRelation(this.schema, context.modelOrType, field);
|
|
841
|
-
if (m2m) {
|
|
842
|
-
return this.transformManyToManyRelationAccess(m2m, context);
|
|
843
|
-
}
|
|
844
|
-
const fromModel = context.modelOrType;
|
|
845
|
-
const relationFieldDef = import_orm3.QueryUtils.requireField(this.schema, fromModel, field);
|
|
846
|
-
const { keyPairs, ownedByModel } = import_orm3.QueryUtils.getRelationForeignKeyFieldPairs(this.schema, fromModel, field);
|
|
847
|
-
let condition;
|
|
848
|
-
if (ownedByModel) {
|
|
849
|
-
condition = conjunction(this.dialect, keyPairs.map(({ fk, pk }) => {
|
|
850
|
-
let fkRef = import_kysely2.ReferenceNode.create(import_kysely2.ColumnNode.create(fk), import_kysely2.TableNode.create(context.alias ?? fromModel));
|
|
851
|
-
if (relationFieldDef.originModel && relationFieldDef.originModel !== fromModel) {
|
|
852
|
-
fkRef = this.buildDelegateBaseFieldSelect(fromModel, context.alias ?? fromModel, fk, relationFieldDef.originModel);
|
|
853
|
-
}
|
|
854
|
-
return import_kysely2.BinaryOperationNode.create(fkRef, import_kysely2.OperatorNode.create("="), import_kysely2.ReferenceNode.create(import_kysely2.ColumnNode.create(pk), import_kysely2.TableNode.create(relationModel)));
|
|
855
|
-
}));
|
|
856
|
-
} else {
|
|
857
|
-
condition = conjunction(this.dialect, keyPairs.map(({ fk, pk }) => import_kysely2.BinaryOperationNode.create(import_kysely2.ReferenceNode.create(import_kysely2.ColumnNode.create(pk), import_kysely2.TableNode.create(context.alias ?? fromModel)), import_kysely2.OperatorNode.create("="), import_kysely2.ReferenceNode.create(import_kysely2.ColumnNode.create(fk), import_kysely2.TableNode.create(relationModel)))));
|
|
858
|
-
}
|
|
859
|
-
return {
|
|
860
|
-
kind: "SelectQueryNode",
|
|
861
|
-
from: import_kysely2.FromNode.create([
|
|
862
|
-
import_kysely2.TableNode.create(relationModel)
|
|
863
|
-
]),
|
|
864
|
-
where: import_kysely2.WhereNode.create(condition)
|
|
865
|
-
};
|
|
866
|
-
}
|
|
867
|
-
transformManyToManyRelationAccess(m2m, context) {
|
|
868
|
-
const eb = (0, import_kysely2.expressionBuilder)();
|
|
869
|
-
const relationQuery = eb.selectFrom(m2m.otherModel).innerJoin(m2m.joinTable, (join) => join.onRef(`${m2m.otherModel}.${m2m.otherPKName}`, "=", `${m2m.joinTable}.${m2m.otherFkName}`).onRef(`${m2m.joinTable}.${m2m.parentFkName}`, "=", `${context.alias ?? context.modelOrType}.${m2m.parentPKName}`));
|
|
870
|
-
return relationQuery.toOperationNode();
|
|
871
|
-
}
|
|
872
|
-
createColumnRef(column, context) {
|
|
873
|
-
const tableName = context.alias ?? context.modelOrType;
|
|
874
|
-
if (context.operation === "create") {
|
|
875
|
-
return import_kysely2.ReferenceNode.create(import_kysely2.ColumnNode.create(column), import_kysely2.TableNode.create(tableName));
|
|
876
|
-
}
|
|
877
|
-
const fieldDef = import_orm3.QueryUtils.requireField(this.schema, context.modelOrType, column);
|
|
878
|
-
if (!fieldDef.originModel || fieldDef.originModel === context.modelOrType) {
|
|
879
|
-
return import_kysely2.ReferenceNode.create(import_kysely2.ColumnNode.create(column), import_kysely2.TableNode.create(tableName));
|
|
880
|
-
}
|
|
881
|
-
return this.buildDelegateBaseFieldSelect(context.modelOrType, tableName, column, fieldDef.originModel);
|
|
882
|
-
}
|
|
883
|
-
// convert transformer's binding scope to equivalent expression evaluator binding scope
|
|
884
|
-
getEvaluationBindingScope(scope) {
|
|
885
|
-
if (!scope) {
|
|
886
|
-
return void 0;
|
|
887
|
-
}
|
|
888
|
-
const result = {};
|
|
889
|
-
for (const [key, value] of Object.entries(scope)) {
|
|
890
|
-
if (value.value !== void 0) {
|
|
891
|
-
result[key] = value.value;
|
|
892
|
-
}
|
|
893
|
-
}
|
|
894
|
-
return Object.keys(result).length > 0 ? result : void 0;
|
|
895
|
-
}
|
|
896
|
-
buildDelegateBaseFieldSelect(model, modelAlias, field, baseModel) {
|
|
897
|
-
const idFields = import_orm3.QueryUtils.requireIdFields(this.client.$schema, model);
|
|
898
|
-
return {
|
|
899
|
-
kind: "SelectQueryNode",
|
|
900
|
-
from: import_kysely2.FromNode.create([
|
|
901
|
-
import_kysely2.TableNode.create(baseModel)
|
|
902
|
-
]),
|
|
903
|
-
selections: [
|
|
904
|
-
import_kysely2.SelectionNode.create(import_kysely2.ReferenceNode.create(import_kysely2.ColumnNode.create(field), import_kysely2.TableNode.create(baseModel)))
|
|
905
|
-
],
|
|
906
|
-
where: import_kysely2.WhereNode.create(conjunction(this.dialect, idFields.map((idField) => import_kysely2.BinaryOperationNode.create(import_kysely2.ReferenceNode.create(import_kysely2.ColumnNode.create(idField), import_kysely2.TableNode.create(baseModel)), import_kysely2.OperatorNode.create("="), import_kysely2.ReferenceNode.create(import_kysely2.ColumnNode.create(idField), import_kysely2.TableNode.create(modelAlias))))))
|
|
907
|
-
};
|
|
908
|
-
}
|
|
909
|
-
isAuthCall(value) {
|
|
910
|
-
return import_schema3.ExpressionUtils.isCall(value) && value.function === "auth";
|
|
911
|
-
}
|
|
912
|
-
isAuthMember(expr2) {
|
|
913
|
-
return import_schema3.ExpressionUtils.isMember(expr2) && this.isAuthCall(expr2.receiver);
|
|
914
|
-
}
|
|
915
|
-
isNullNode(node) {
|
|
916
|
-
return import_kysely2.ValueNode.is(node) && node.value === null;
|
|
917
|
-
}
|
|
918
|
-
buildLogicalNot(result) {
|
|
919
|
-
return import_schema3.ExpressionUtils.unary("!", result);
|
|
920
|
-
}
|
|
921
|
-
buildAnd(conditions) {
|
|
922
|
-
if (conditions.length === 0) {
|
|
923
|
-
return import_schema3.ExpressionUtils.literal(true);
|
|
924
|
-
} else if (conditions.length === 1) {
|
|
925
|
-
return conditions[0];
|
|
926
|
-
} else {
|
|
927
|
-
return conditions.reduce((acc, condition) => import_schema3.ExpressionUtils.binary(acc, "&&", condition));
|
|
928
|
-
}
|
|
929
|
-
}
|
|
930
|
-
isRelationField(expr2, context) {
|
|
931
|
-
const fieldDef = this.getFieldDefFromFieldRef(expr2, context);
|
|
932
|
-
return !!fieldDef?.relation;
|
|
933
|
-
}
|
|
934
|
-
getFieldDefFromFieldRef(expr2, context) {
|
|
935
|
-
const model = import_schema3.ExpressionUtils.isMember(expr2) && import_schema3.ExpressionUtils.isThis(expr2.receiver) ? context.thisType : context.modelOrType;
|
|
936
|
-
if (import_schema3.ExpressionUtils.isField(expr2)) {
|
|
937
|
-
return import_orm3.QueryUtils.getField(this.schema, model, expr2.field);
|
|
938
|
-
} else if (import_schema3.ExpressionUtils.isMember(expr2) && expr2.members.length === 1 && import_schema3.ExpressionUtils.isThis(expr2.receiver)) {
|
|
939
|
-
return import_orm3.QueryUtils.getField(this.schema, model, expr2.members[0]);
|
|
940
|
-
} else if (import_schema3.ExpressionUtils.isMember(expr2) && import_schema3.ExpressionUtils.isField(expr2.receiver)) {
|
|
941
|
-
const receiverDef = import_orm3.QueryUtils.getField(this.schema, model, expr2.receiver.field);
|
|
942
|
-
if (!receiverDef?.relation) return void 0;
|
|
943
|
-
let currModel = receiverDef.type;
|
|
944
|
-
for (let i = 0; i < expr2.members.length - 1; i++) {
|
|
945
|
-
const hopDef = import_orm3.QueryUtils.getField(this.schema, currModel, expr2.members[i]);
|
|
946
|
-
if (!hopDef?.relation) return void 0;
|
|
947
|
-
currModel = hopDef.type;
|
|
948
|
-
}
|
|
949
|
-
return import_orm3.QueryUtils.getField(this.schema, currModel, expr2.members[expr2.members.length - 1]);
|
|
950
|
-
} else {
|
|
951
|
-
return void 0;
|
|
952
|
-
}
|
|
953
|
-
}
|
|
221
|
+
dialect;
|
|
222
|
+
eb = (0, kysely.expressionBuilder)();
|
|
223
|
+
constructor(client) {
|
|
224
|
+
this.client = client;
|
|
225
|
+
this.dialect = (0, _zenstackhq_orm.getCrudDialect)(this.schema, this.clientOptions);
|
|
226
|
+
}
|
|
227
|
+
get schema() {
|
|
228
|
+
return this.client.$schema;
|
|
229
|
+
}
|
|
230
|
+
get clientOptions() {
|
|
231
|
+
return this.client.$options;
|
|
232
|
+
}
|
|
233
|
+
get auth() {
|
|
234
|
+
return this.client.$auth;
|
|
235
|
+
}
|
|
236
|
+
get authType() {
|
|
237
|
+
if (!this.schema.authType) (0, _zenstackhq_common_helpers.invariant)(false, "Schema does not have an \"authType\" specified");
|
|
238
|
+
return this.schema.authType;
|
|
239
|
+
}
|
|
240
|
+
transform(expression, context) {
|
|
241
|
+
const handler = expressionHandlers.get(expression.kind);
|
|
242
|
+
if (!handler) throw new Error(`Unsupported expression kind: ${expression.kind}`);
|
|
243
|
+
const result = handler.value.call(this, expression, context);
|
|
244
|
+
(0, _zenstackhq_common_helpers.invariant)("kind" in result, `expression handler must return an OperationNode: transforming ${expression.kind}`);
|
|
245
|
+
return result;
|
|
246
|
+
}
|
|
247
|
+
_literal(expr) {
|
|
248
|
+
return this.transformValue(expr.value, typeof expr.value === "string" ? "String" : typeof expr.value === "boolean" ? "Boolean" : "Int");
|
|
249
|
+
}
|
|
250
|
+
_array(expr, context) {
|
|
251
|
+
return this.dialect.buildArrayValue(expr.items.map((item) => new kysely.ExpressionWrapper(this.transform(item, context))), expr.type).toOperationNode();
|
|
252
|
+
}
|
|
253
|
+
_field(expr, context) {
|
|
254
|
+
if (context.contextValue) {
|
|
255
|
+
const fieldDef = _zenstackhq_orm.QueryUtils.requireField(this.schema, context.modelOrType, expr.field);
|
|
256
|
+
return this.transformValue(context.contextValue[expr.field], fieldDef.type);
|
|
257
|
+
}
|
|
258
|
+
const fieldDef = _zenstackhq_orm.QueryUtils.requireField(this.schema, context.modelOrType, expr.field);
|
|
259
|
+
if (!fieldDef.relation) return this.createColumnRef(expr.field, context);
|
|
260
|
+
else {
|
|
261
|
+
const { memberFilter, memberSelect, ...restContext } = context;
|
|
262
|
+
const relation = this.transformRelationAccess(expr.field, fieldDef.type, restContext);
|
|
263
|
+
return {
|
|
264
|
+
...relation,
|
|
265
|
+
where: this.mergeWhere(relation.where, memberFilter),
|
|
266
|
+
selections: memberSelect ? [memberSelect] : relation.selections
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
mergeWhere(where, memberFilter) {
|
|
271
|
+
if (!where) return kysely.WhereNode.create(memberFilter ?? trueNode(this.dialect));
|
|
272
|
+
if (!memberFilter) return where;
|
|
273
|
+
return kysely.WhereNode.create(conjunction(this.dialect, [where.where, memberFilter]));
|
|
274
|
+
}
|
|
275
|
+
_null() {
|
|
276
|
+
return kysely.ValueNode.createImmediate(null);
|
|
277
|
+
}
|
|
278
|
+
_binary(expr, context) {
|
|
279
|
+
if (expr.op === "&&") return conjunction(this.dialect, [this.transform(expr.left, context), this.transform(expr.right, context)]);
|
|
280
|
+
else if (expr.op === "||") return disjunction(this.dialect, [this.transform(expr.left, context), this.transform(expr.right, context)]);
|
|
281
|
+
if (this.isAuthCall(expr.left) || this.isAuthCall(expr.right)) return this.transformAuthBinary(expr, context);
|
|
282
|
+
const op = expr.op;
|
|
283
|
+
if (op === "?" || op === "!" || op === "^") return this.transformCollectionPredicate(expr, context);
|
|
284
|
+
const { normalizedLeft, normalizedRight } = this.normalizeBinaryOperationOperands(expr, context);
|
|
285
|
+
const left = this.transform(normalizedLeft, context);
|
|
286
|
+
const right = this.transform(normalizedRight, context);
|
|
287
|
+
if (op === "in") if (this.isNullNode(left)) return this.transformValue(false, "Boolean");
|
|
288
|
+
else if (kysely.ValueListNode.is(right)) return kysely.BinaryOperationNode.create(left, kysely.OperatorNode.create("in"), right);
|
|
289
|
+
else return kysely.BinaryOperationNode.create(left, kysely.OperatorNode.create("="), kysely.FunctionNode.create("any", [right]));
|
|
290
|
+
if (this.isNullNode(right)) return this.transformNullCheck(left, expr.op);
|
|
291
|
+
else if (this.isNullNode(left)) return this.transformNullCheck(right, expr.op);
|
|
292
|
+
else {
|
|
293
|
+
const leftFieldDef = this.getFieldDefFromFieldRef(normalizedLeft, context);
|
|
294
|
+
const rightFieldDef = this.getFieldDefFromFieldRef(normalizedRight, context);
|
|
295
|
+
const sqlOp = op === "==" ? "=" : op;
|
|
296
|
+
return this.dialect.buildComparison(new kysely.ExpressionWrapper(left), leftFieldDef, sqlOp, new kysely.ExpressionWrapper(right), rightFieldDef).toOperationNode();
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
transformNullCheck(expr, operator) {
|
|
300
|
+
if (operator === "==" || operator === "!=") if (kysely.ValueNode.is(expr)) if (expr.value === null) return operator === "==" ? trueNode(this.dialect) : falseNode(this.dialect);
|
|
301
|
+
else return operator === "==" ? falseNode(this.dialect) : trueNode(this.dialect);
|
|
302
|
+
else return operator === "==" ? kysely.BinaryOperationNode.create(expr, kysely.OperatorNode.create("is"), kysely.ValueNode.createImmediate(null)) : kysely.BinaryOperationNode.create(expr, kysely.OperatorNode.create("is not"), kysely.ValueNode.createImmediate(null));
|
|
303
|
+
else return kysely.ValueNode.createImmediate(null);
|
|
304
|
+
}
|
|
305
|
+
normalizeBinaryOperationOperands(expr, context) {
|
|
306
|
+
if (context.contextValue) return {
|
|
307
|
+
normalizedLeft: expr.left,
|
|
308
|
+
normalizedRight: expr.right
|
|
309
|
+
};
|
|
310
|
+
let normalizedLeft = expr.left;
|
|
311
|
+
if (this.isRelationField(expr.left, context)) {
|
|
312
|
+
(0, _zenstackhq_common_helpers.invariant)(_zenstackhq_orm_schema.ExpressionUtils.isNull(expr.right), "only null comparison is supported for relation field");
|
|
313
|
+
const leftRelDef = this.getFieldDefFromFieldRef(expr.left, context);
|
|
314
|
+
(0, _zenstackhq_common_helpers.invariant)(leftRelDef, "failed to get relation field definition");
|
|
315
|
+
const idFields = _zenstackhq_orm.QueryUtils.requireIdFields(this.schema, leftRelDef.type);
|
|
316
|
+
normalizedLeft = this.makeOrAppendMember(normalizedLeft, idFields[0]);
|
|
317
|
+
}
|
|
318
|
+
let normalizedRight = expr.right;
|
|
319
|
+
if (this.isRelationField(expr.right, context)) {
|
|
320
|
+
(0, _zenstackhq_common_helpers.invariant)(_zenstackhq_orm_schema.ExpressionUtils.isNull(expr.left), "only null comparison is supported for relation field");
|
|
321
|
+
const rightRelDef = this.getFieldDefFromFieldRef(expr.right, context);
|
|
322
|
+
(0, _zenstackhq_common_helpers.invariant)(rightRelDef, "failed to get relation field definition");
|
|
323
|
+
const idFields = _zenstackhq_orm.QueryUtils.requireIdFields(this.schema, rightRelDef.type);
|
|
324
|
+
normalizedRight = this.makeOrAppendMember(normalizedRight, idFields[0]);
|
|
325
|
+
}
|
|
326
|
+
return {
|
|
327
|
+
normalizedLeft,
|
|
328
|
+
normalizedRight
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
transformCollectionPredicate(expr, context) {
|
|
332
|
+
this.ensureCollectionPredicateOperator(expr.op);
|
|
333
|
+
if (this.isAuthMember(expr.left) || context.contextValue) {
|
|
334
|
+
(0, _zenstackhq_common_helpers.invariant)(_zenstackhq_orm_schema.ExpressionUtils.isMember(expr.left) || _zenstackhq_orm_schema.ExpressionUtils.isField(expr.left), "expected member or field expression");
|
|
335
|
+
const receiver = new ExpressionEvaluator().evaluate(expr.left, {
|
|
336
|
+
thisValue: context.contextValue,
|
|
337
|
+
auth: this.auth,
|
|
338
|
+
bindingScope: this.getEvaluationBindingScope(context.bindingScope),
|
|
339
|
+
operation: context.operation,
|
|
340
|
+
thisType: context.thisType
|
|
341
|
+
});
|
|
342
|
+
const baseType = this.isAuthMember(expr.left) ? this.authType : context.modelOrType;
|
|
343
|
+
const memberType = this.getMemberType(baseType, expr.left);
|
|
344
|
+
return this.transformValueCollectionPredicate(receiver, expr, {
|
|
345
|
+
...context,
|
|
346
|
+
modelOrType: memberType
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
(0, _zenstackhq_common_helpers.invariant)(_zenstackhq_orm_schema.ExpressionUtils.isField(expr.left) || _zenstackhq_orm_schema.ExpressionUtils.isMember(expr.left), "left operand must be field or member access");
|
|
350
|
+
let newContextModel;
|
|
351
|
+
const fieldDef = this.getFieldDefFromFieldRef(expr.left, context);
|
|
352
|
+
if (fieldDef) {
|
|
353
|
+
(0, _zenstackhq_common_helpers.invariant)(fieldDef.relation, `field is not a relation: ${JSON.stringify(expr.left)}`);
|
|
354
|
+
newContextModel = fieldDef.type;
|
|
355
|
+
} else {
|
|
356
|
+
(0, _zenstackhq_common_helpers.invariant)(_zenstackhq_orm_schema.ExpressionUtils.isMember(expr.left) && (_zenstackhq_orm_schema.ExpressionUtils.isField(expr.left.receiver) || _zenstackhq_orm_schema.ExpressionUtils.isBinding(expr.left.receiver)), "left operand must be member access with field receiver");
|
|
357
|
+
if (_zenstackhq_orm_schema.ExpressionUtils.isField(expr.left.receiver)) newContextModel = _zenstackhq_orm.QueryUtils.requireField(this.schema, context.modelOrType, expr.left.receiver.field).type;
|
|
358
|
+
else newContextModel = this.requireBindingScope(expr.left.receiver, context).type;
|
|
359
|
+
for (const member of expr.left.members) newContextModel = _zenstackhq_orm.QueryUtils.requireField(this.schema, newContextModel, member).type;
|
|
360
|
+
}
|
|
361
|
+
const bindingScope = expr.binding ? {
|
|
362
|
+
...context.bindingScope ?? {},
|
|
363
|
+
[expr.binding]: {
|
|
364
|
+
type: newContextModel,
|
|
365
|
+
alias: newContextModel
|
|
366
|
+
}
|
|
367
|
+
} : context.bindingScope;
|
|
368
|
+
let predicateFilter = this.transform(expr.right, {
|
|
369
|
+
...context,
|
|
370
|
+
modelOrType: newContextModel,
|
|
371
|
+
alias: void 0,
|
|
372
|
+
bindingScope
|
|
373
|
+
});
|
|
374
|
+
if (expr.op === "!") predicateFilter = logicalNot(this.dialect, predicateFilter);
|
|
375
|
+
const count = kysely.FunctionNode.create("count", [kysely.ValueNode.createImmediate(1)]);
|
|
376
|
+
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();
|
|
377
|
+
return this.transform(expr.left, {
|
|
378
|
+
...context,
|
|
379
|
+
memberSelect: kysely.SelectionNode.create(kysely.AliasNode.create(predicateResult, kysely.IdentifierNode.create("_"))),
|
|
380
|
+
memberFilter: predicateFilter
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
ensureCollectionPredicateOperator(op) {
|
|
384
|
+
(0, _zenstackhq_common_helpers.invariant)(CollectionPredicateOperator.includes(op), "expected \"?\" or \"!\" or \"^\" operator");
|
|
385
|
+
}
|
|
386
|
+
transformValueCollectionPredicate(receiver, expr, context) {
|
|
387
|
+
if (!receiver) return kysely.ValueNode.createImmediate(null);
|
|
388
|
+
this.ensureCollectionPredicateOperator(expr.op);
|
|
389
|
+
if (!new _zenstackhq_orm.SchemaUtils.MatchingExpressionVisitor((e) => _zenstackhq_orm_schema.ExpressionUtils.isThis(e)).find(expr.right)) {
|
|
390
|
+
const value = new ExpressionEvaluator().evaluate(expr, {
|
|
391
|
+
auth: this.auth,
|
|
392
|
+
thisValue: context.contextValue,
|
|
393
|
+
bindingScope: this.getEvaluationBindingScope(context.bindingScope),
|
|
394
|
+
operation: context.operation,
|
|
395
|
+
thisType: context.thisType
|
|
396
|
+
});
|
|
397
|
+
return this.transformValue(value, "Boolean");
|
|
398
|
+
} else {
|
|
399
|
+
(0, _zenstackhq_common_helpers.invariant)(Array.isArray(receiver), "array value is expected");
|
|
400
|
+
const components = receiver.map((item) => {
|
|
401
|
+
const bindingScope = expr.binding ? {
|
|
402
|
+
...context.bindingScope ?? {},
|
|
403
|
+
[expr.binding]: {
|
|
404
|
+
type: context.modelOrType,
|
|
405
|
+
alias: context.thisAlias ?? context.modelOrType,
|
|
406
|
+
value: item
|
|
407
|
+
}
|
|
408
|
+
} : context.bindingScope;
|
|
409
|
+
return this.transform(expr.right, {
|
|
410
|
+
operation: context.operation,
|
|
411
|
+
thisType: context.thisType,
|
|
412
|
+
thisAlias: context.thisAlias,
|
|
413
|
+
modelOrType: context.modelOrType,
|
|
414
|
+
contextValue: item,
|
|
415
|
+
bindingScope
|
|
416
|
+
});
|
|
417
|
+
});
|
|
418
|
+
return (0, ts_pattern.match)(expr.op).with("?", () => disjunction(this.dialect, components)).with("!", () => conjunction(this.dialect, components)).with("^", () => logicalNot(this.dialect, disjunction(this.dialect, components))).exhaustive();
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
getMemberType(receiverType, expr) {
|
|
422
|
+
if (_zenstackhq_orm_schema.ExpressionUtils.isField(expr)) return _zenstackhq_orm.QueryUtils.requireField(this.schema, receiverType, expr.field).type;
|
|
423
|
+
else {
|
|
424
|
+
let currType = receiverType;
|
|
425
|
+
for (const member of expr.members) currType = _zenstackhq_orm.QueryUtils.requireField(this.schema, currType, member).type;
|
|
426
|
+
return currType;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
transformAuthBinary(expr, context) {
|
|
430
|
+
if (expr.op !== "==" && expr.op !== "!=") throw createUnsupportedError(`Unsupported operator for \`auth()\` in policy of model "${context.modelOrType}": ${expr.op}`);
|
|
431
|
+
let authExpr;
|
|
432
|
+
let other;
|
|
433
|
+
if (this.isAuthCall(expr.left)) {
|
|
434
|
+
authExpr = expr.left;
|
|
435
|
+
other = expr.right;
|
|
436
|
+
} else {
|
|
437
|
+
authExpr = expr.right;
|
|
438
|
+
other = expr.left;
|
|
439
|
+
}
|
|
440
|
+
if (_zenstackhq_orm_schema.ExpressionUtils.isNull(other)) return this.transformValue(expr.op === "==" ? !this.auth : !!this.auth, "Boolean");
|
|
441
|
+
else {
|
|
442
|
+
const authModel = _zenstackhq_orm.QueryUtils.getModel(this.schema, this.authType);
|
|
443
|
+
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`);
|
|
444
|
+
const idFields = Object.values(authModel.fields).filter((f) => f.id).map((f) => f.name);
|
|
445
|
+
(0, _zenstackhq_common_helpers.invariant)(idFields.length > 0, "auth type model must have at least one id field");
|
|
446
|
+
const conditions = idFields.map((fieldName) => _zenstackhq_orm_schema.ExpressionUtils.binary(_zenstackhq_orm_schema.ExpressionUtils.member(authExpr, [fieldName]), "==", this.makeOrAppendMember(other, fieldName)));
|
|
447
|
+
let result = this.buildAnd(conditions);
|
|
448
|
+
if (expr.op === "!=") result = this.buildLogicalNot(result);
|
|
449
|
+
return this.transform(result, context);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
makeOrAppendMember(other, fieldName) {
|
|
453
|
+
if (_zenstackhq_orm_schema.ExpressionUtils.isMember(other)) return _zenstackhq_orm_schema.ExpressionUtils.member(other.receiver, [...other.members, fieldName]);
|
|
454
|
+
else return _zenstackhq_orm_schema.ExpressionUtils.member(other, [fieldName]);
|
|
455
|
+
}
|
|
456
|
+
transformValue(value, type) {
|
|
457
|
+
if (value === true) return trueNode(this.dialect);
|
|
458
|
+
else if (value === false) return falseNode(this.dialect);
|
|
459
|
+
else if (Array.isArray(value)) return this.dialect.buildArrayValue(value.map((v) => new kysely.ExpressionWrapper(this.transformValue(v, type))), type).toOperationNode();
|
|
460
|
+
else {
|
|
461
|
+
const transformed = this.dialect.transformInput(value, type, false) ?? null;
|
|
462
|
+
if (typeof transformed !== "string") return kysely.ValueNode.createImmediate(transformed);
|
|
463
|
+
else return kysely.ValueNode.create(transformed);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
_unary(expr, context) {
|
|
467
|
+
(0, _zenstackhq_common_helpers.invariant)(expr.op === "!", "only \"!\" operator is supported");
|
|
468
|
+
return logicalNot(this.dialect, this.transform(expr.operand, context));
|
|
469
|
+
}
|
|
470
|
+
_call(expr, context) {
|
|
471
|
+
return this.transformCall(expr, context).toOperationNode();
|
|
472
|
+
}
|
|
473
|
+
transformCall(expr, context) {
|
|
474
|
+
const func = this.getFunctionImpl(expr.function);
|
|
475
|
+
if (!func) throw createUnsupportedError(`Function not implemented: ${expr.function}`);
|
|
476
|
+
return func(this.eb, (expr.args ?? []).map((arg) => this.transformCallArg(arg, context)), {
|
|
477
|
+
client: this.client,
|
|
478
|
+
dialect: this.dialect,
|
|
479
|
+
model: context.modelOrType,
|
|
480
|
+
modelAlias: context.alias ?? context.modelOrType,
|
|
481
|
+
operation: context.operation
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
getFunctionImpl(functionName) {
|
|
485
|
+
let func = this.clientOptions.functions?.[functionName];
|
|
486
|
+
if (!func) {
|
|
487
|
+
for (const plugin of this.clientOptions.plugins ?? []) if (plugin.functions?.[functionName]) {
|
|
488
|
+
func = plugin.functions[functionName];
|
|
489
|
+
break;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
return func;
|
|
493
|
+
}
|
|
494
|
+
transformCallArg(arg, context) {
|
|
495
|
+
if (_zenstackhq_orm_schema.ExpressionUtils.isField(arg)) return this.eb.ref(arg.field);
|
|
496
|
+
else return new kysely.ExpressionWrapper(this.transform(arg, context));
|
|
497
|
+
}
|
|
498
|
+
_member(expr, context) {
|
|
499
|
+
if (_zenstackhq_orm_schema.ExpressionUtils.isBinding(expr.receiver)) {
|
|
500
|
+
const scope = this.requireBindingScope(expr.receiver, context);
|
|
501
|
+
if (scope.value !== void 0) return this.valueMemberAccess(scope.value, expr, scope.type);
|
|
502
|
+
}
|
|
503
|
+
if (this.isAuthCall(expr.receiver)) return this.valueMemberAccess(this.auth, expr, this.authType);
|
|
504
|
+
if (isBeforeInvocation(expr.receiver)) {
|
|
505
|
+
(0, _zenstackhq_common_helpers.invariant)(context.operation === "post-update", "before() can only be used in post-update policy");
|
|
506
|
+
(0, _zenstackhq_common_helpers.invariant)(expr.members.length === 1, "before() can only be followed by a scalar field access");
|
|
507
|
+
return kysely.ReferenceNode.create(kysely.ColumnNode.create(expr.members[0]), kysely.TableNode.create("$before"));
|
|
508
|
+
}
|
|
509
|
+
(0, _zenstackhq_common_helpers.invariant)(_zenstackhq_orm_schema.ExpressionUtils.isField(expr.receiver) || _zenstackhq_orm_schema.ExpressionUtils.isThis(expr.receiver) || _zenstackhq_orm_schema.ExpressionUtils.isBinding(expr.receiver), "expect receiver to be field expression, collection predicate binding, or \"this\"");
|
|
510
|
+
let members = expr.members;
|
|
511
|
+
let receiver;
|
|
512
|
+
let startType;
|
|
513
|
+
const { memberFilter, memberSelect, ...restContext } = context;
|
|
514
|
+
if (_zenstackhq_orm_schema.ExpressionUtils.isThis(expr.receiver)) if (expr.members.length === 1) return this._field(_zenstackhq_orm_schema.ExpressionUtils.field(expr.members[0]), {
|
|
515
|
+
...context,
|
|
516
|
+
alias: context.thisAlias,
|
|
517
|
+
modelOrType: context.thisType,
|
|
518
|
+
thisType: context.thisType,
|
|
519
|
+
contextValue: void 0
|
|
520
|
+
});
|
|
521
|
+
else {
|
|
522
|
+
const firstMemberFieldDef = _zenstackhq_orm.QueryUtils.requireField(this.schema, context.thisType, expr.members[0]);
|
|
523
|
+
receiver = this.transformRelationAccess(expr.members[0], firstMemberFieldDef.type, restContext);
|
|
524
|
+
members = expr.members.slice(1);
|
|
525
|
+
startType = firstMemberFieldDef.type;
|
|
526
|
+
}
|
|
527
|
+
else if (_zenstackhq_orm_schema.ExpressionUtils.isBinding(expr.receiver)) if (expr.members.length === 1) {
|
|
528
|
+
const bindingScope = this.requireBindingScope(expr.receiver, context);
|
|
529
|
+
return this._field(_zenstackhq_orm_schema.ExpressionUtils.field(expr.members[0]), {
|
|
530
|
+
...context,
|
|
531
|
+
modelOrType: bindingScope.type,
|
|
532
|
+
alias: bindingScope.alias,
|
|
533
|
+
thisType: context.thisType,
|
|
534
|
+
contextValue: void 0
|
|
535
|
+
});
|
|
536
|
+
} else {
|
|
537
|
+
const bindingScope = this.requireBindingScope(expr.receiver, context);
|
|
538
|
+
const firstMemberFieldDef = _zenstackhq_orm.QueryUtils.requireField(this.schema, bindingScope.type, expr.members[0]);
|
|
539
|
+
receiver = this.transformRelationAccess(expr.members[0], firstMemberFieldDef.type, {
|
|
540
|
+
...restContext,
|
|
541
|
+
modelOrType: bindingScope.type,
|
|
542
|
+
alias: bindingScope.alias
|
|
543
|
+
});
|
|
544
|
+
members = expr.members.slice(1);
|
|
545
|
+
startType = firstMemberFieldDef.type;
|
|
546
|
+
}
|
|
547
|
+
else receiver = this.transform(expr.receiver, restContext);
|
|
548
|
+
(0, _zenstackhq_common_helpers.invariant)(kysely.SelectQueryNode.is(receiver), "expected receiver to be select query");
|
|
549
|
+
if (startType === void 0) if (_zenstackhq_orm_schema.ExpressionUtils.isField(expr.receiver)) startType = _zenstackhq_orm.QueryUtils.requireField(this.schema, context.modelOrType, expr.receiver.field).type;
|
|
550
|
+
else startType = context.thisType;
|
|
551
|
+
const memberFields = [];
|
|
552
|
+
let currType = startType;
|
|
553
|
+
for (const member of members) {
|
|
554
|
+
const fieldDef = _zenstackhq_orm.QueryUtils.requireField(this.schema, currType, member);
|
|
555
|
+
memberFields.push({
|
|
556
|
+
fieldDef,
|
|
557
|
+
fromModel: currType
|
|
558
|
+
});
|
|
559
|
+
currType = fieldDef.type;
|
|
560
|
+
}
|
|
561
|
+
let currNode = void 0;
|
|
562
|
+
for (let i = members.length - 1; i >= 0; i--) {
|
|
563
|
+
const member = members[i];
|
|
564
|
+
const { fieldDef, fromModel } = memberFields[i];
|
|
565
|
+
if (fieldDef.relation) {
|
|
566
|
+
const relation = this.transformRelationAccess(member, fieldDef.type, {
|
|
567
|
+
...restContext,
|
|
568
|
+
modelOrType: fromModel,
|
|
569
|
+
alias: void 0
|
|
570
|
+
});
|
|
571
|
+
if (currNode) currNode = {
|
|
572
|
+
...relation,
|
|
573
|
+
selections: [kysely.SelectionNode.create(kysely.AliasNode.create(currNode, kysely.IdentifierNode.create(members[i + 1])))]
|
|
574
|
+
};
|
|
575
|
+
else currNode = {
|
|
576
|
+
...relation,
|
|
577
|
+
where: this.mergeWhere(relation.where, memberFilter),
|
|
578
|
+
selections: memberSelect ? [memberSelect] : relation.selections
|
|
579
|
+
};
|
|
580
|
+
} else {
|
|
581
|
+
(0, _zenstackhq_common_helpers.invariant)(i === members.length - 1, "plain field access must be the last segment");
|
|
582
|
+
(0, _zenstackhq_common_helpers.invariant)(!currNode, "plain field access must be the last segment");
|
|
583
|
+
currNode = kysely.ColumnNode.create(member);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
return {
|
|
587
|
+
...receiver,
|
|
588
|
+
selections: [kysely.SelectionNode.create(kysely.AliasNode.create(currNode, kysely.IdentifierNode.create("_")))]
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
requireBindingScope(expr, context) {
|
|
592
|
+
const binding = context.bindingScope?.[expr.name];
|
|
593
|
+
(0, _zenstackhq_common_helpers.invariant)(binding, `binding not found: ${expr.name}`);
|
|
594
|
+
return binding;
|
|
595
|
+
}
|
|
596
|
+
valueMemberAccess(receiver, expr, receiverType) {
|
|
597
|
+
if (!receiver) return kysely.ValueNode.createImmediate(null);
|
|
598
|
+
(0, _zenstackhq_common_helpers.invariant)(expr.members.length > 0, "member expression must have at least one member");
|
|
599
|
+
let curr = receiver;
|
|
600
|
+
let currType = receiverType;
|
|
601
|
+
for (let i = 0; i < expr.members.length; i++) {
|
|
602
|
+
const field = expr.members[i];
|
|
603
|
+
curr = curr?.[field];
|
|
604
|
+
if (curr === void 0) {
|
|
605
|
+
curr = kysely.ValueNode.createImmediate(null);
|
|
606
|
+
break;
|
|
607
|
+
}
|
|
608
|
+
currType = _zenstackhq_orm.QueryUtils.requireField(this.schema, currType, field).type;
|
|
609
|
+
if (i === expr.members.length - 1) curr = this.transformValue(curr, currType);
|
|
610
|
+
}
|
|
611
|
+
return curr;
|
|
612
|
+
}
|
|
613
|
+
transformRelationAccess(field, relationModel, context) {
|
|
614
|
+
const m2m = _zenstackhq_orm.QueryUtils.getManyToManyRelation(this.schema, context.modelOrType, field);
|
|
615
|
+
if (m2m) return this.transformManyToManyRelationAccess(m2m, context);
|
|
616
|
+
const fromModel = context.modelOrType;
|
|
617
|
+
const relationFieldDef = _zenstackhq_orm.QueryUtils.requireField(this.schema, fromModel, field);
|
|
618
|
+
const { keyPairs, ownedByModel } = _zenstackhq_orm.QueryUtils.getRelationForeignKeyFieldPairs(this.schema, fromModel, field);
|
|
619
|
+
let condition;
|
|
620
|
+
if (ownedByModel) condition = conjunction(this.dialect, keyPairs.map(({ fk, pk }) => {
|
|
621
|
+
let fkRef = kysely.ReferenceNode.create(kysely.ColumnNode.create(fk), kysely.TableNode.create(context.alias ?? fromModel));
|
|
622
|
+
if (relationFieldDef.originModel && relationFieldDef.originModel !== fromModel) fkRef = this.buildDelegateBaseFieldSelect(fromModel, context.alias ?? fromModel, fk, relationFieldDef.originModel);
|
|
623
|
+
return kysely.BinaryOperationNode.create(fkRef, kysely.OperatorNode.create("="), kysely.ReferenceNode.create(kysely.ColumnNode.create(pk), kysely.TableNode.create(relationModel)));
|
|
624
|
+
}));
|
|
625
|
+
else condition = conjunction(this.dialect, keyPairs.map(({ fk, pk }) => kysely.BinaryOperationNode.create(kysely.ReferenceNode.create(kysely.ColumnNode.create(pk), kysely.TableNode.create(context.alias ?? fromModel)), kysely.OperatorNode.create("="), kysely.ReferenceNode.create(kysely.ColumnNode.create(fk), kysely.TableNode.create(relationModel)))));
|
|
626
|
+
return {
|
|
627
|
+
kind: "SelectQueryNode",
|
|
628
|
+
from: kysely.FromNode.create([kysely.TableNode.create(relationModel)]),
|
|
629
|
+
where: kysely.WhereNode.create(condition)
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
transformManyToManyRelationAccess(m2m, context) {
|
|
633
|
+
return (0, kysely.expressionBuilder)().selectFrom(m2m.otherModel).innerJoin(m2m.joinTable, (join) => join.onRef(`${m2m.otherModel}.${m2m.otherPKName}`, "=", `${m2m.joinTable}.${m2m.otherFkName}`).onRef(`${m2m.joinTable}.${m2m.parentFkName}`, "=", `${context.alias ?? context.modelOrType}.${m2m.parentPKName}`)).toOperationNode();
|
|
634
|
+
}
|
|
635
|
+
createColumnRef(column, context) {
|
|
636
|
+
const tableName = context.alias ?? context.modelOrType;
|
|
637
|
+
if (context.operation === "create") return kysely.ReferenceNode.create(kysely.ColumnNode.create(column), kysely.TableNode.create(tableName));
|
|
638
|
+
const fieldDef = _zenstackhq_orm.QueryUtils.requireField(this.schema, context.modelOrType, column);
|
|
639
|
+
if (!fieldDef.originModel || fieldDef.originModel === context.modelOrType) return kysely.ReferenceNode.create(kysely.ColumnNode.create(column), kysely.TableNode.create(tableName));
|
|
640
|
+
return this.buildDelegateBaseFieldSelect(context.modelOrType, tableName, column, fieldDef.originModel);
|
|
641
|
+
}
|
|
642
|
+
getEvaluationBindingScope(scope) {
|
|
643
|
+
if (!scope) return;
|
|
644
|
+
const result = {};
|
|
645
|
+
for (const [key, value] of Object.entries(scope)) if (value.value !== void 0) result[key] = value.value;
|
|
646
|
+
return Object.keys(result).length > 0 ? result : void 0;
|
|
647
|
+
}
|
|
648
|
+
buildDelegateBaseFieldSelect(model, modelAlias, field, baseModel) {
|
|
649
|
+
const idFields = _zenstackhq_orm.QueryUtils.requireIdFields(this.client.$schema, model);
|
|
650
|
+
return {
|
|
651
|
+
kind: "SelectQueryNode",
|
|
652
|
+
from: kysely.FromNode.create([kysely.TableNode.create(baseModel)]),
|
|
653
|
+
selections: [kysely.SelectionNode.create(kysely.ReferenceNode.create(kysely.ColumnNode.create(field), kysely.TableNode.create(baseModel)))],
|
|
654
|
+
where: kysely.WhereNode.create(conjunction(this.dialect, idFields.map((idField) => kysely.BinaryOperationNode.create(kysely.ReferenceNode.create(kysely.ColumnNode.create(idField), kysely.TableNode.create(baseModel)), kysely.OperatorNode.create("="), kysely.ReferenceNode.create(kysely.ColumnNode.create(idField), kysely.TableNode.create(modelAlias))))))
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
isAuthCall(value) {
|
|
658
|
+
return _zenstackhq_orm_schema.ExpressionUtils.isCall(value) && value.function === "auth";
|
|
659
|
+
}
|
|
660
|
+
isAuthMember(expr) {
|
|
661
|
+
return _zenstackhq_orm_schema.ExpressionUtils.isMember(expr) && this.isAuthCall(expr.receiver);
|
|
662
|
+
}
|
|
663
|
+
isNullNode(node) {
|
|
664
|
+
return kysely.ValueNode.is(node) && node.value === null;
|
|
665
|
+
}
|
|
666
|
+
buildLogicalNot(result) {
|
|
667
|
+
return _zenstackhq_orm_schema.ExpressionUtils.unary("!", result);
|
|
668
|
+
}
|
|
669
|
+
buildAnd(conditions) {
|
|
670
|
+
if (conditions.length === 0) return _zenstackhq_orm_schema.ExpressionUtils.literal(true);
|
|
671
|
+
else if (conditions.length === 1) return conditions[0];
|
|
672
|
+
else return conditions.reduce((acc, condition) => _zenstackhq_orm_schema.ExpressionUtils.binary(acc, "&&", condition));
|
|
673
|
+
}
|
|
674
|
+
isRelationField(expr, context) {
|
|
675
|
+
return !!this.getFieldDefFromFieldRef(expr, context)?.relation;
|
|
676
|
+
}
|
|
677
|
+
getFieldDefFromFieldRef(expr, context) {
|
|
678
|
+
const model = _zenstackhq_orm_schema.ExpressionUtils.isMember(expr) && _zenstackhq_orm_schema.ExpressionUtils.isThis(expr.receiver) ? context.thisType : context.modelOrType;
|
|
679
|
+
if (_zenstackhq_orm_schema.ExpressionUtils.isField(expr)) return _zenstackhq_orm.QueryUtils.getField(this.schema, model, expr.field);
|
|
680
|
+
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]);
|
|
681
|
+
else if (_zenstackhq_orm_schema.ExpressionUtils.isMember(expr) && _zenstackhq_orm_schema.ExpressionUtils.isField(expr.receiver)) {
|
|
682
|
+
const receiverDef = _zenstackhq_orm.QueryUtils.getField(this.schema, model, expr.receiver.field);
|
|
683
|
+
if (!receiverDef?.relation) return void 0;
|
|
684
|
+
let currModel = receiverDef.type;
|
|
685
|
+
for (let i = 0; i < expr.members.length - 1; i++) {
|
|
686
|
+
const hopDef = _zenstackhq_orm.QueryUtils.getField(this.schema, currModel, expr.members[i]);
|
|
687
|
+
if (!hopDef?.relation) return void 0;
|
|
688
|
+
currModel = hopDef.type;
|
|
689
|
+
}
|
|
690
|
+
return _zenstackhq_orm.QueryUtils.getField(this.schema, currModel, expr.members[expr.members.length - 1]);
|
|
691
|
+
} else return;
|
|
692
|
+
}
|
|
954
693
|
};
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
]),
|
|
961
|
-
_ts_metadata("design:returntype", void 0)
|
|
694
|
+
__decorate([
|
|
695
|
+
expr("literal"),
|
|
696
|
+
__decorateMetadata("design:type", Function),
|
|
697
|
+
__decorateMetadata("design:paramtypes", [Object]),
|
|
698
|
+
__decorateMetadata("design:returntype", void 0)
|
|
962
699
|
], ExpressionTransformer.prototype, "_literal", null);
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
typeof ExpressionTransformerContext === "undefined" ? Object : ExpressionTransformerContext
|
|
969
|
-
]),
|
|
970
|
-
_ts_metadata("design:returntype", void 0)
|
|
700
|
+
__decorate([
|
|
701
|
+
expr("array"),
|
|
702
|
+
__decorateMetadata("design:type", Function),
|
|
703
|
+
__decorateMetadata("design:paramtypes", [Object, Object]),
|
|
704
|
+
__decorateMetadata("design:returntype", void 0)
|
|
971
705
|
], ExpressionTransformer.prototype, "_array", null);
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
typeof ExpressionTransformerContext === "undefined" ? Object : ExpressionTransformerContext
|
|
978
|
-
]),
|
|
979
|
-
_ts_metadata("design:returntype", void 0)
|
|
706
|
+
__decorate([
|
|
707
|
+
expr("field"),
|
|
708
|
+
__decorateMetadata("design:type", Function),
|
|
709
|
+
__decorateMetadata("design:paramtypes", [Object, Object]),
|
|
710
|
+
__decorateMetadata("design:returntype", void 0)
|
|
980
711
|
], ExpressionTransformer.prototype, "_field", null);
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
712
|
+
__decorate([
|
|
713
|
+
expr("null"),
|
|
714
|
+
__decorateMetadata("design:type", Function),
|
|
715
|
+
__decorateMetadata("design:paramtypes", []),
|
|
716
|
+
__decorateMetadata("design:returntype", void 0)
|
|
986
717
|
], ExpressionTransformer.prototype, "_null", null);
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
typeof ExpressionTransformerContext === "undefined" ? Object : ExpressionTransformerContext
|
|
993
|
-
]),
|
|
994
|
-
_ts_metadata("design:returntype", void 0)
|
|
718
|
+
__decorate([
|
|
719
|
+
expr("binary"),
|
|
720
|
+
__decorateMetadata("design:type", Function),
|
|
721
|
+
__decorateMetadata("design:paramtypes", [Object, Object]),
|
|
722
|
+
__decorateMetadata("design:returntype", void 0)
|
|
995
723
|
], ExpressionTransformer.prototype, "_binary", null);
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
typeof ExpressionTransformerContext === "undefined" ? Object : ExpressionTransformerContext
|
|
1002
|
-
]),
|
|
1003
|
-
_ts_metadata("design:returntype", void 0)
|
|
724
|
+
__decorate([
|
|
725
|
+
expr("unary"),
|
|
726
|
+
__decorateMetadata("design:type", Function),
|
|
727
|
+
__decorateMetadata("design:paramtypes", [Object, Object]),
|
|
728
|
+
__decorateMetadata("design:returntype", void 0)
|
|
1004
729
|
], ExpressionTransformer.prototype, "_unary", null);
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
typeof ExpressionTransformerContext === "undefined" ? Object : ExpressionTransformerContext
|
|
1011
|
-
]),
|
|
1012
|
-
_ts_metadata("design:returntype", void 0)
|
|
730
|
+
__decorate([
|
|
731
|
+
expr("call"),
|
|
732
|
+
__decorateMetadata("design:type", Function),
|
|
733
|
+
__decorateMetadata("design:paramtypes", [Object, Object]),
|
|
734
|
+
__decorateMetadata("design:returntype", void 0)
|
|
1013
735
|
], ExpressionTransformer.prototype, "_call", null);
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
typeof ExpressionTransformerContext === "undefined" ? Object : ExpressionTransformerContext
|
|
1020
|
-
]),
|
|
1021
|
-
_ts_metadata("design:returntype", void 0)
|
|
736
|
+
__decorate([
|
|
737
|
+
expr("member"),
|
|
738
|
+
__decorateMetadata("design:type", Function),
|
|
739
|
+
__decorateMetadata("design:paramtypes", [Object, Object]),
|
|
740
|
+
__decorateMetadata("design:returntype", void 0)
|
|
1022
741
|
], ExpressionTransformer.prototype, "_member", null);
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
var PolicyHandler = class extends
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
tryGetConstantPolicy(model, operation) {
|
|
1624
|
-
const policies = this.getModelPolicies(model, operation);
|
|
1625
|
-
if (!policies.some((p) => p.kind === "allow")) {
|
|
1626
|
-
return false;
|
|
1627
|
-
} else if (
|
|
1628
|
-
// unconditional deny
|
|
1629
|
-
policies.some((p) => p.kind === "deny" && this.isTrueExpr(p.condition))
|
|
1630
|
-
) {
|
|
1631
|
-
return false;
|
|
1632
|
-
} else if (
|
|
1633
|
-
// unconditional allow
|
|
1634
|
-
!policies.some((p) => p.kind === "deny") && policies.some((p) => p.kind === "allow" && this.isTrueExpr(p.condition))
|
|
1635
|
-
) {
|
|
1636
|
-
return true;
|
|
1637
|
-
} else {
|
|
1638
|
-
return void 0;
|
|
1639
|
-
}
|
|
1640
|
-
}
|
|
1641
|
-
isTrueExpr(expr2) {
|
|
1642
|
-
return import_schema4.ExpressionUtils.isLiteral(expr2) && expr2.value === true;
|
|
1643
|
-
}
|
|
1644
|
-
async processReadBack(node, result, proceed) {
|
|
1645
|
-
if (result.rows.length === 0) {
|
|
1646
|
-
return result;
|
|
1647
|
-
}
|
|
1648
|
-
if (!this.isMutationQueryNode(node) || !node.returning) {
|
|
1649
|
-
return result;
|
|
1650
|
-
}
|
|
1651
|
-
const { mutationModel } = this.getMutationModel(node);
|
|
1652
|
-
const idConditions = this.buildIdConditions(mutationModel, result.rows);
|
|
1653
|
-
const policyFilter = this.buildPolicyFilter(mutationModel, void 0, "read");
|
|
1654
|
-
const select = {
|
|
1655
|
-
kind: "SelectQueryNode",
|
|
1656
|
-
from: import_kysely3.FromNode.create([
|
|
1657
|
-
import_kysely3.TableNode.create(mutationModel)
|
|
1658
|
-
]),
|
|
1659
|
-
where: import_kysely3.WhereNode.create(conjunction(this.dialect, [
|
|
1660
|
-
idConditions,
|
|
1661
|
-
policyFilter
|
|
1662
|
-
])),
|
|
1663
|
-
selections: node.returning.selections
|
|
1664
|
-
};
|
|
1665
|
-
const selectResult = await proceed(select);
|
|
1666
|
-
return selectResult;
|
|
1667
|
-
}
|
|
1668
|
-
buildIdConditions(table, rows) {
|
|
1669
|
-
const idFields = import_orm4.QueryUtils.requireIdFields(this.client.$schema, table);
|
|
1670
|
-
return disjunction(this.dialect, rows.map((row) => conjunction(this.dialect, idFields.map((field) => import_kysely3.BinaryOperationNode.create(import_kysely3.ReferenceNode.create(import_kysely3.ColumnNode.create(field), import_kysely3.TableNode.create(table)), import_kysely3.OperatorNode.create("="), import_kysely3.ValueNode.create(row[field]))))));
|
|
1671
|
-
}
|
|
1672
|
-
getMutationModel(node) {
|
|
1673
|
-
const r = (0, import_ts_pattern3.match)(node).when(import_kysely3.InsertQueryNode.is, (node2) => ({
|
|
1674
|
-
mutationModel: getTableName(node2.into),
|
|
1675
|
-
alias: void 0
|
|
1676
|
-
})).when(import_kysely3.UpdateQueryNode.is, (node2) => {
|
|
1677
|
-
if (!node2.table) {
|
|
1678
|
-
(0, import_common_helpers3.invariant)(false, "Update query must have a table");
|
|
1679
|
-
}
|
|
1680
|
-
const r2 = this.extractTableName(node2.table);
|
|
1681
|
-
return r2 ? {
|
|
1682
|
-
mutationModel: r2.model,
|
|
1683
|
-
alias: r2.alias
|
|
1684
|
-
} : void 0;
|
|
1685
|
-
}).when(import_kysely3.DeleteQueryNode.is, (node2) => {
|
|
1686
|
-
if (node2.from.froms.length !== 1) {
|
|
1687
|
-
throw createUnsupportedError("Only one from table is supported for delete");
|
|
1688
|
-
}
|
|
1689
|
-
const r2 = this.extractTableName(node2.from.froms[0]);
|
|
1690
|
-
return r2 ? {
|
|
1691
|
-
mutationModel: r2.model,
|
|
1692
|
-
alias: r2.alias
|
|
1693
|
-
} : void 0;
|
|
1694
|
-
}).exhaustive();
|
|
1695
|
-
if (!r) {
|
|
1696
|
-
(0, import_common_helpers3.invariant)(false, `Unable to get table name for query node: ${node}`);
|
|
1697
|
-
}
|
|
1698
|
-
return r;
|
|
1699
|
-
}
|
|
1700
|
-
isCrudQueryNode(node) {
|
|
1701
|
-
return import_kysely3.SelectQueryNode.is(node) || import_kysely3.InsertQueryNode.is(node) || import_kysely3.UpdateQueryNode.is(node) || import_kysely3.DeleteQueryNode.is(node);
|
|
1702
|
-
}
|
|
1703
|
-
isMutationQueryNode(node) {
|
|
1704
|
-
return import_kysely3.InsertQueryNode.is(node) || import_kysely3.UpdateQueryNode.is(node) || import_kysely3.DeleteQueryNode.is(node);
|
|
1705
|
-
}
|
|
1706
|
-
buildPolicyFilter(model, alias, operation) {
|
|
1707
|
-
const m2mFilter = this.getModelPolicyFilterForManyToManyJoinTable(model, alias, operation);
|
|
1708
|
-
if (m2mFilter) {
|
|
1709
|
-
return m2mFilter;
|
|
1710
|
-
}
|
|
1711
|
-
const policies = this.getModelPolicies(model, operation);
|
|
1712
|
-
const allows = policies.filter((policy) => policy.kind === "allow").map((policy) => this.compilePolicyCondition(model, alias, operation, policy));
|
|
1713
|
-
const denies = policies.filter((policy) => policy.kind === "deny").map((policy) => this.compilePolicyCondition(model, alias, operation, policy));
|
|
1714
|
-
let combinedPolicy;
|
|
1715
|
-
if (allows.length === 0) {
|
|
1716
|
-
if (operation === "post-update") {
|
|
1717
|
-
combinedPolicy = trueNode(this.dialect);
|
|
1718
|
-
} else {
|
|
1719
|
-
combinedPolicy = falseNode(this.dialect);
|
|
1720
|
-
}
|
|
1721
|
-
} else {
|
|
1722
|
-
combinedPolicy = disjunction(this.dialect, allows);
|
|
1723
|
-
}
|
|
1724
|
-
if (denies.length !== 0) {
|
|
1725
|
-
const combinedDenies = conjunction(this.dialect, denies.map((d) => buildIsFalse(d, this.dialect)));
|
|
1726
|
-
combinedPolicy = conjunction(this.dialect, [
|
|
1727
|
-
combinedPolicy,
|
|
1728
|
-
combinedDenies
|
|
1729
|
-
]);
|
|
1730
|
-
}
|
|
1731
|
-
return combinedPolicy;
|
|
1732
|
-
}
|
|
1733
|
-
extractTableName(node) {
|
|
1734
|
-
if (import_kysely3.TableNode.is(node)) {
|
|
1735
|
-
return {
|
|
1736
|
-
model: node.table.identifier.name
|
|
1737
|
-
};
|
|
1738
|
-
}
|
|
1739
|
-
if (import_kysely3.AliasNode.is(node)) {
|
|
1740
|
-
const inner = this.extractTableName(node.node);
|
|
1741
|
-
if (!inner) {
|
|
1742
|
-
return void 0;
|
|
1743
|
-
}
|
|
1744
|
-
return {
|
|
1745
|
-
model: inner.model,
|
|
1746
|
-
alias: import_kysely3.IdentifierNode.is(node.alias) ? node.alias.name : void 0
|
|
1747
|
-
};
|
|
1748
|
-
} else {
|
|
1749
|
-
return void 0;
|
|
1750
|
-
}
|
|
1751
|
-
}
|
|
1752
|
-
createPolicyFilterForFrom(node) {
|
|
1753
|
-
if (!node) {
|
|
1754
|
-
return void 0;
|
|
1755
|
-
}
|
|
1756
|
-
return this.createPolicyFilterForTables(node.froms);
|
|
1757
|
-
}
|
|
1758
|
-
createPolicyFilterForTables(tables) {
|
|
1759
|
-
return tables.reduce((acc, table) => {
|
|
1760
|
-
const extractResult = this.extractTableName(table);
|
|
1761
|
-
if (extractResult) {
|
|
1762
|
-
const { model, alias } = extractResult;
|
|
1763
|
-
const filter = this.buildPolicyFilter(model, alias, "read");
|
|
1764
|
-
return acc ? conjunction(this.dialect, [
|
|
1765
|
-
acc,
|
|
1766
|
-
filter
|
|
1767
|
-
]) : filter;
|
|
1768
|
-
}
|
|
1769
|
-
return acc;
|
|
1770
|
-
}, void 0);
|
|
1771
|
-
}
|
|
1772
|
-
compilePolicyCondition(model, alias, operation, policy) {
|
|
1773
|
-
return new ExpressionTransformer(this.client).transform(policy.condition, {
|
|
1774
|
-
modelOrType: model,
|
|
1775
|
-
thisType: model,
|
|
1776
|
-
thisAlias: alias,
|
|
1777
|
-
alias,
|
|
1778
|
-
operation
|
|
1779
|
-
});
|
|
1780
|
-
}
|
|
1781
|
-
getModelPolicies(model, operation) {
|
|
1782
|
-
const modelDef = import_orm4.QueryUtils.requireModel(this.client.$schema, model);
|
|
1783
|
-
const result = [];
|
|
1784
|
-
const extractOperations = /* @__PURE__ */ __name((expr2) => {
|
|
1785
|
-
(0, import_common_helpers3.invariant)(import_schema4.ExpressionUtils.isLiteral(expr2), "expecting a literal");
|
|
1786
|
-
(0, import_common_helpers3.invariant)(typeof expr2.value === "string", "expecting a string literal");
|
|
1787
|
-
return expr2.value.split(",").filter((v) => !!v).map((v) => v.trim());
|
|
1788
|
-
}, "extractOperations");
|
|
1789
|
-
if (modelDef.attributes) {
|
|
1790
|
-
result.push(...modelDef.attributes.filter((attr) => attr.name === "@@allow" || attr.name === "@@deny").map((attr) => ({
|
|
1791
|
-
kind: attr.name === "@@allow" ? "allow" : "deny",
|
|
1792
|
-
operations: extractOperations(attr.args[0].value),
|
|
1793
|
-
condition: attr.args[1].value
|
|
1794
|
-
})).filter((policy) => operation !== "post-update" && policy.operations.includes("all") || policy.operations.includes(operation)));
|
|
1795
|
-
}
|
|
1796
|
-
return result;
|
|
1797
|
-
}
|
|
1798
|
-
getFieldPolicies(model, field, operation) {
|
|
1799
|
-
const fieldDef = import_orm4.QueryUtils.requireField(this.client.$schema, model, field);
|
|
1800
|
-
const result = [];
|
|
1801
|
-
const extractOperations = /* @__PURE__ */ __name((expr2) => {
|
|
1802
|
-
(0, import_common_helpers3.invariant)(import_schema4.ExpressionUtils.isLiteral(expr2), "expecting a literal");
|
|
1803
|
-
(0, import_common_helpers3.invariant)(typeof expr2.value === "string", "expecting a string literal");
|
|
1804
|
-
return expr2.value.split(",").filter((v) => !!v).map((v) => v.trim());
|
|
1805
|
-
}, "extractOperations");
|
|
1806
|
-
if (fieldDef.attributes) {
|
|
1807
|
-
result.push(...fieldDef.attributes.filter((attr) => attr.name === "@allow" || attr.name === "@deny").map((attr) => ({
|
|
1808
|
-
kind: attr.name === "@allow" ? "allow" : "deny",
|
|
1809
|
-
operations: extractOperations(attr.args[0].value),
|
|
1810
|
-
condition: attr.args[1].value
|
|
1811
|
-
})).filter((policy) => policy.operations.includes("all") || policy.operations.includes(operation)));
|
|
1812
|
-
}
|
|
1813
|
-
return result;
|
|
1814
|
-
}
|
|
1815
|
-
resolveManyToManyJoinTable(tableName) {
|
|
1816
|
-
for (const model of Object.values(this.client.$schema.models)) {
|
|
1817
|
-
for (const field of Object.values(model.fields)) {
|
|
1818
|
-
const m2m = import_orm4.QueryUtils.getManyToManyRelation(this.client.$schema, model.name, field.name);
|
|
1819
|
-
if (m2m?.joinTable === tableName) {
|
|
1820
|
-
const sortedRecord = [
|
|
1821
|
-
{
|
|
1822
|
-
model: model.name,
|
|
1823
|
-
field: field.name
|
|
1824
|
-
},
|
|
1825
|
-
{
|
|
1826
|
-
model: m2m.otherModel,
|
|
1827
|
-
field: m2m.otherField
|
|
1828
|
-
}
|
|
1829
|
-
].sort(this.manyToManySorter);
|
|
1830
|
-
const firstIdFields = import_orm4.QueryUtils.requireIdFields(this.client.$schema, sortedRecord[0].model);
|
|
1831
|
-
const secondIdFields = import_orm4.QueryUtils.requireIdFields(this.client.$schema, sortedRecord[1].model);
|
|
1832
|
-
(0, import_common_helpers3.invariant)(firstIdFields.length === 1 && secondIdFields.length === 1, "only single-field id is supported for implicit many-to-many join table");
|
|
1833
|
-
return {
|
|
1834
|
-
firstModel: sortedRecord[0].model,
|
|
1835
|
-
firstField: sortedRecord[0].field,
|
|
1836
|
-
firstIdField: firstIdFields[0],
|
|
1837
|
-
secondModel: sortedRecord[1].model,
|
|
1838
|
-
secondField: sortedRecord[1].field,
|
|
1839
|
-
secondIdField: secondIdFields[0]
|
|
1840
|
-
};
|
|
1841
|
-
}
|
|
1842
|
-
}
|
|
1843
|
-
}
|
|
1844
|
-
return void 0;
|
|
1845
|
-
}
|
|
1846
|
-
manyToManySorter(a, b) {
|
|
1847
|
-
return a.model !== b.model ? a.model.localeCompare(b.model) : a.field.localeCompare(b.field);
|
|
1848
|
-
}
|
|
1849
|
-
isManyToManyJoinTable(tableName) {
|
|
1850
|
-
return !!this.resolveManyToManyJoinTable(tableName);
|
|
1851
|
-
}
|
|
1852
|
-
getModelPolicyFilterForManyToManyJoinTable(tableName, alias, operation) {
|
|
1853
|
-
const m2m = this.resolveManyToManyJoinTable(tableName);
|
|
1854
|
-
if (!m2m) {
|
|
1855
|
-
return void 0;
|
|
1856
|
-
}
|
|
1857
|
-
const checkForOperation = operation === "read" ? "read" : "update";
|
|
1858
|
-
const joinTable = alias ?? tableName;
|
|
1859
|
-
const aQuery = this.eb.selectFrom(m2m.firstModel).whereRef(`${m2m.firstModel}.${m2m.firstIdField}`, "=", `${joinTable}.A`).select(() => new import_kysely3.ExpressionWrapper(this.buildPolicyFilter(m2m.firstModel, void 0, checkForOperation)).as("$conditionA"));
|
|
1860
|
-
const bQuery = this.eb.selectFrom(m2m.secondModel).whereRef(`${m2m.secondModel}.${m2m.secondIdField}`, "=", `${joinTable}.B`).select(() => new import_kysely3.ExpressionWrapper(this.buildPolicyFilter(m2m.secondModel, void 0, checkForOperation)).as("$conditionB"));
|
|
1861
|
-
return this.eb.and([
|
|
1862
|
-
aQuery,
|
|
1863
|
-
bQuery
|
|
1864
|
-
]).toOperationNode();
|
|
1865
|
-
}
|
|
1866
|
-
tryRejectNonexistentModel(model) {
|
|
1867
|
-
if (!import_orm4.QueryUtils.hasModel(this.client.$schema, model) && !this.isManyToManyJoinTable(model)) {
|
|
1868
|
-
throw createRejectedByPolicyError(model, import_orm4.RejectedByPolicyReason.NO_ACCESS);
|
|
1869
|
-
}
|
|
1870
|
-
}
|
|
1871
|
-
tryRejectNonexistingTables(tables) {
|
|
1872
|
-
for (const table of tables) {
|
|
1873
|
-
const extractResult = this.extractTableName(table);
|
|
1874
|
-
if (extractResult) {
|
|
1875
|
-
this.tryRejectNonexistentModel(extractResult.model);
|
|
1876
|
-
}
|
|
1877
|
-
}
|
|
1878
|
-
}
|
|
1879
|
-
// correction to kysely mutation result may be needed because we might have added
|
|
1880
|
-
// returning clause to the query and caused changes to the result shape
|
|
1881
|
-
postProcessMutationResult(result, node) {
|
|
1882
|
-
if (node.returning) {
|
|
1883
|
-
return result;
|
|
1884
|
-
} else {
|
|
1885
|
-
return {
|
|
1886
|
-
...result,
|
|
1887
|
-
rows: [],
|
|
1888
|
-
numAffectedRows: result.numAffectedRows ?? BigInt(result.rows.length)
|
|
1889
|
-
};
|
|
1890
|
-
}
|
|
1891
|
-
}
|
|
1892
|
-
// strips table references from an OperationNode
|
|
1893
|
-
stripTableReferences(node, modelName) {
|
|
1894
|
-
return new TableReferenceStripper().strip(node, modelName);
|
|
1895
|
-
}
|
|
742
|
+
//#endregion
|
|
743
|
+
//#region src/policy-handler.ts
|
|
744
|
+
var PolicyHandler = class extends kysely.OperationNodeTransformer {
|
|
745
|
+
dialect;
|
|
746
|
+
eb = (0, kysely.expressionBuilder)();
|
|
747
|
+
constructor(client, options = {}) {
|
|
748
|
+
super();
|
|
749
|
+
this.client = client;
|
|
750
|
+
this.options = options;
|
|
751
|
+
this.dialect = (0, _zenstackhq_orm.getCrudDialect)(this.client.$schema, this.client.$options);
|
|
752
|
+
}
|
|
753
|
+
async handle(node, proceed) {
|
|
754
|
+
if (!this.isCrudQueryNode(node)) {
|
|
755
|
+
if (this.options.dangerouslyAllowRawSql && kysely.RawNode.is(node)) return proceed(node);
|
|
756
|
+
throw createRejectedByPolicyError(void 0, _zenstackhq_orm.RejectedByPolicyReason.OTHER, "non-CRUD queries are not allowed");
|
|
757
|
+
}
|
|
758
|
+
if (!this.isMutationQueryNode(node)) return proceed(this.transformNode(node));
|
|
759
|
+
const { mutationModel } = this.getMutationModel(node);
|
|
760
|
+
this.tryRejectNonexistentModel(mutationModel);
|
|
761
|
+
if (kysely.InsertQueryNode.is(node)) await this.preCreateCheck(mutationModel, node, proceed);
|
|
762
|
+
if (kysely.UpdateQueryNode.is(node)) await this.preUpdateCheck(mutationModel, node, proceed);
|
|
763
|
+
const needsPostUpdateCheck = kysely.UpdateQueryNode.is(node) && this.hasPostUpdatePolicies(mutationModel);
|
|
764
|
+
let beforeUpdateInfo;
|
|
765
|
+
if (needsPostUpdateCheck) beforeUpdateInfo = await this.loadBeforeUpdateEntities(mutationModel, node.where, proceed, !this.dialect.supportsReturning);
|
|
766
|
+
const result = await proceed(this.transformNode(node));
|
|
767
|
+
if ((result.numAffectedRows ?? 0) > 0 && needsPostUpdateCheck) await this.postUpdateCheck(mutationModel, beforeUpdateInfo, result, proceed);
|
|
768
|
+
if (!node.returning || this.onlyReturningId(node)) return this.postProcessMutationResult(result, node);
|
|
769
|
+
else {
|
|
770
|
+
const readBackResult = await this.processReadBack(node, result, proceed);
|
|
771
|
+
if (readBackResult.rows.length !== result.rows.length) throw createRejectedByPolicyError(mutationModel, _zenstackhq_orm.RejectedByPolicyReason.CANNOT_READ_BACK, "result is not allowed to be read back");
|
|
772
|
+
return readBackResult;
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
async preCreateCheck(mutationModel, node, proceed) {
|
|
776
|
+
const isManyToManyJoinTable = this.isManyToManyJoinTable(mutationModel);
|
|
777
|
+
let needCheckPreCreate = true;
|
|
778
|
+
if (!isManyToManyJoinTable) {
|
|
779
|
+
const constCondition = this.tryGetConstantPolicy(mutationModel, "create");
|
|
780
|
+
if (constCondition === true) needCheckPreCreate = false;
|
|
781
|
+
else if (constCondition === false) throw createRejectedByPolicyError(mutationModel, _zenstackhq_orm.RejectedByPolicyReason.NO_ACCESS);
|
|
782
|
+
}
|
|
783
|
+
if (needCheckPreCreate) await this.enforcePreCreatePolicy(node, mutationModel, isManyToManyJoinTable, proceed);
|
|
784
|
+
}
|
|
785
|
+
async preUpdateCheck(mutationModel, node, proceed) {
|
|
786
|
+
const fieldUpdatePolicies = (node.updates?.map((u) => kysely.ColumnNode.is(u.column) ? u.column.column.name : void 0).filter((f) => !!f) ?? []).map((f) => this.buildFieldPolicyFilter(mutationModel, f, "update"));
|
|
787
|
+
const fieldLevelFilter = conjunction(this.dialect, fieldUpdatePolicies);
|
|
788
|
+
if (isTrueNode(fieldLevelFilter)) return;
|
|
789
|
+
const modelLevelFilter = this.buildPolicyFilter(mutationModel, void 0, "update");
|
|
790
|
+
const updateFilter = conjunction(this.dialect, [modelLevelFilter, node.where?.where ?? trueNode(this.dialect)]);
|
|
791
|
+
if ((await proceed(this.eb.selectFrom(mutationModel).select((eb) => eb.fn.coalesce(eb.fn.sum(this.dialect.castInt(new kysely.ExpressionWrapper(logicalNot(this.dialect, fieldLevelFilter)))), eb.lit(0)).as("$filteredCount")).where(() => new kysely.ExpressionWrapper(updateFilter)).toOperationNode())).rows[0].$filteredCount > 0) throw createRejectedByPolicyError(mutationModel, _zenstackhq_orm.RejectedByPolicyReason.NO_ACCESS, "some rows cannot be updated due to field policies");
|
|
792
|
+
}
|
|
793
|
+
async postUpdateCheck(model, beforeUpdateInfo, updateResult, proceed) {
|
|
794
|
+
let postUpdateRows;
|
|
795
|
+
if (this.dialect.supportsReturning) postUpdateRows = updateResult.rows;
|
|
796
|
+
else {
|
|
797
|
+
(0, _zenstackhq_common_helpers.invariant)(beforeUpdateInfo, "beforeUpdateInfo must be defined for dialects not supporting returning");
|
|
798
|
+
const idConditions = this.buildIdConditions(model, beforeUpdateInfo.rows);
|
|
799
|
+
const idFields = _zenstackhq_orm.QueryUtils.requireIdFields(this.client.$schema, model);
|
|
800
|
+
postUpdateRows = (await proceed({
|
|
801
|
+
kind: "SelectQueryNode",
|
|
802
|
+
from: kysely.FromNode.create([kysely.TableNode.create(model)]),
|
|
803
|
+
where: kysely.WhereNode.create(idConditions),
|
|
804
|
+
selections: idFields.map((field) => kysely.SelectionNode.create(kysely.ColumnNode.create(field)))
|
|
805
|
+
})).rows;
|
|
806
|
+
}
|
|
807
|
+
if (beforeUpdateInfo) {
|
|
808
|
+
if (beforeUpdateInfo.rows.length !== postUpdateRows.length) throw createRejectedByPolicyError(model, _zenstackhq_orm.RejectedByPolicyReason.OTHER, "Before-update and after-update rows do not match. If you have post-update policies on a model, updating id fields is not supported.");
|
|
809
|
+
const idFields = _zenstackhq_orm.QueryUtils.requireIdFields(this.client.$schema, model);
|
|
810
|
+
for (const postRow of postUpdateRows) if (!beforeUpdateInfo.rows.find((r) => idFields.every((f) => r[f] === postRow[f]))) throw createRejectedByPolicyError(model, _zenstackhq_orm.RejectedByPolicyReason.OTHER, "Before-update and after-update rows do not match. If you have post-update policies on a model, updating id fields is not supported.");
|
|
811
|
+
}
|
|
812
|
+
const idConditions = this.buildIdConditions(model, postUpdateRows);
|
|
813
|
+
const postUpdateFilter = this.buildPolicyFilter(model, void 0, "post-update");
|
|
814
|
+
const eb = (0, kysely.expressionBuilder)();
|
|
815
|
+
const needsBeforeUpdateJoin = !!beforeUpdateInfo?.fields;
|
|
816
|
+
let beforeUpdateTable = void 0;
|
|
817
|
+
if (needsBeforeUpdateJoin) {
|
|
818
|
+
const fieldDefs = beforeUpdateInfo.fields.map((name) => _zenstackhq_orm.QueryUtils.requireField(this.client.$schema, model, name));
|
|
819
|
+
const rows = beforeUpdateInfo.rows.map((r) => beforeUpdateInfo.fields.map((f) => r[f]));
|
|
820
|
+
beforeUpdateTable = this.dialect.buildValuesTableSelect(fieldDefs, rows).toOperationNode();
|
|
821
|
+
}
|
|
822
|
+
if (!(await proceed(eb.selectFrom(model).select(() => [eb(eb.fn("COUNT", [eb.lit(1)]), "=", Number(updateResult.numAffectedRows ?? 0)).as("$condition")]).where(() => new kysely.ExpressionWrapper(conjunction(this.dialect, [idConditions, postUpdateFilter]))).$if(needsBeforeUpdateJoin, (qb) => qb.leftJoin(() => new kysely.ExpressionWrapper(beforeUpdateTable).as("$before"), (join) => {
|
|
823
|
+
return _zenstackhq_orm.QueryUtils.requireIdFields(this.client.$schema, model).reduce((acc, f) => acc.onRef(`${model}.${f}`, "=", `$before.${f}`), join);
|
|
824
|
+
})).toOperationNode())).rows[0]?.$condition) throw createRejectedByPolicyError(model, _zenstackhq_orm.RejectedByPolicyReason.NO_ACCESS, "some or all updated rows failed to pass post-update policy check");
|
|
825
|
+
}
|
|
826
|
+
transformSelectQuery(node) {
|
|
827
|
+
if (!node.from) return super.transformSelectQuery(node);
|
|
828
|
+
this.tryRejectNonexistingTables(node.from.froms);
|
|
829
|
+
let result = super.transformSelectQuery(node);
|
|
830
|
+
if (node.from.froms.some((table) => {
|
|
831
|
+
const extractedTable = this.extractTableName(table);
|
|
832
|
+
if (extractedTable) return this.hasFieldLevelPolicies(extractedTable.model, "read");
|
|
833
|
+
else return false;
|
|
834
|
+
})) {
|
|
835
|
+
const updatedFroms = [];
|
|
836
|
+
for (const table of result.from.froms) {
|
|
837
|
+
const extractedTable = this.extractTableName(table);
|
|
838
|
+
if (extractedTable?.model && _zenstackhq_orm.QueryUtils.getModel(this.client.$schema, extractedTable.model)) {
|
|
839
|
+
const { query } = this.createSelectAllFieldsWithPolicies(extractedTable.model, extractedTable.alias, "read");
|
|
840
|
+
updatedFroms.push(query);
|
|
841
|
+
} else updatedFroms.push(table);
|
|
842
|
+
}
|
|
843
|
+
result = {
|
|
844
|
+
...result,
|
|
845
|
+
from: kysely.FromNode.create(updatedFroms)
|
|
846
|
+
};
|
|
847
|
+
} else {
|
|
848
|
+
let whereNode = result.where;
|
|
849
|
+
const policyFilter = this.createPolicyFilterForFrom(result.from);
|
|
850
|
+
if (policyFilter && !isTrueNode(policyFilter)) whereNode = kysely.WhereNode.create(whereNode?.where ? conjunction(this.dialect, [whereNode.where, policyFilter]) : policyFilter);
|
|
851
|
+
result = {
|
|
852
|
+
...result,
|
|
853
|
+
where: whereNode
|
|
854
|
+
};
|
|
855
|
+
}
|
|
856
|
+
return result;
|
|
857
|
+
}
|
|
858
|
+
transformJoin(node) {
|
|
859
|
+
const table = this.extractTableName(node.table);
|
|
860
|
+
if (!table) return super.transformJoin(node);
|
|
861
|
+
this.tryRejectNonexistentModel(table.model);
|
|
862
|
+
if (!_zenstackhq_orm.QueryUtils.getModel(this.client.$schema, table.model)) return super.transformJoin(node);
|
|
863
|
+
const result = super.transformJoin(node);
|
|
864
|
+
const { hasPolicies, query: nestedQuery } = this.createSelectAllFieldsWithPolicies(table.model, table.alias, "read");
|
|
865
|
+
if (!hasPolicies) return result;
|
|
866
|
+
return {
|
|
867
|
+
...result,
|
|
868
|
+
table: nestedQuery
|
|
869
|
+
};
|
|
870
|
+
}
|
|
871
|
+
transformInsertQuery(node) {
|
|
872
|
+
let processedNode = node;
|
|
873
|
+
let onConflict = node.onConflict;
|
|
874
|
+
if (onConflict?.updates) {
|
|
875
|
+
const { mutationModel, alias } = this.getMutationModel(node);
|
|
876
|
+
const filter = this.buildPolicyFilter(mutationModel, alias, "update");
|
|
877
|
+
if (onConflict.updateWhere) onConflict = {
|
|
878
|
+
...onConflict,
|
|
879
|
+
updateWhere: kysely.WhereNode.create(conjunction(this.dialect, [onConflict.updateWhere.where, filter]))
|
|
880
|
+
};
|
|
881
|
+
else onConflict = {
|
|
882
|
+
...onConflict,
|
|
883
|
+
updateWhere: kysely.WhereNode.create(filter)
|
|
884
|
+
};
|
|
885
|
+
processedNode = {
|
|
886
|
+
...node,
|
|
887
|
+
onConflict
|
|
888
|
+
};
|
|
889
|
+
}
|
|
890
|
+
let onDuplicateKey = node.onDuplicateKey;
|
|
891
|
+
if (onDuplicateKey?.updates) {
|
|
892
|
+
const { mutationModel } = this.getMutationModel(node);
|
|
893
|
+
const filterWithTableRef = this.buildPolicyFilter(mutationModel, void 0, "update");
|
|
894
|
+
const filter = this.stripTableReferences(filterWithTableRef, mutationModel);
|
|
895
|
+
const wrappedUpdates = onDuplicateKey.updates.map((update) => {
|
|
896
|
+
const columnName = kysely.ColumnNode.is(update.column) ? update.column.column.name : void 0;
|
|
897
|
+
if (!columnName) return update;
|
|
898
|
+
const wrappedValue = kysely.sql`IF(${new kysely.ExpressionWrapper(filter)}, ${new kysely.ExpressionWrapper(update.value)}, ${kysely.sql.ref(columnName)})`.toOperationNode();
|
|
899
|
+
return {
|
|
900
|
+
...update,
|
|
901
|
+
value: wrappedValue
|
|
902
|
+
};
|
|
903
|
+
});
|
|
904
|
+
onDuplicateKey = {
|
|
905
|
+
...onDuplicateKey,
|
|
906
|
+
updates: wrappedUpdates
|
|
907
|
+
};
|
|
908
|
+
processedNode = {
|
|
909
|
+
...processedNode,
|
|
910
|
+
onDuplicateKey
|
|
911
|
+
};
|
|
912
|
+
}
|
|
913
|
+
const result = super.transformInsertQuery(processedNode);
|
|
914
|
+
let returning = result.returning;
|
|
915
|
+
if (returning) {
|
|
916
|
+
const { mutationModel } = this.getMutationModel(node);
|
|
917
|
+
const idFields = _zenstackhq_orm.QueryUtils.requireIdFields(this.client.$schema, mutationModel);
|
|
918
|
+
returning = kysely.ReturningNode.create(idFields.map((f) => kysely.SelectionNode.create(kysely.ColumnNode.create(f))));
|
|
919
|
+
}
|
|
920
|
+
return {
|
|
921
|
+
...result,
|
|
922
|
+
returning
|
|
923
|
+
};
|
|
924
|
+
}
|
|
925
|
+
transformUpdateQuery(node) {
|
|
926
|
+
const result = super.transformUpdateQuery(node);
|
|
927
|
+
const { mutationModel, alias } = this.getMutationModel(node);
|
|
928
|
+
let filter = this.buildPolicyFilter(mutationModel, alias, "update");
|
|
929
|
+
if (node.from) {
|
|
930
|
+
this.tryRejectNonexistingTables(node.from.froms);
|
|
931
|
+
const joinFilter = this.createPolicyFilterForFrom(node.from);
|
|
932
|
+
if (joinFilter) filter = conjunction(this.dialect, [filter, joinFilter]);
|
|
933
|
+
}
|
|
934
|
+
let returning = result.returning;
|
|
935
|
+
if (this.dialect.supportsReturning && (returning || this.hasPostUpdatePolicies(mutationModel))) {
|
|
936
|
+
const idFields = _zenstackhq_orm.QueryUtils.requireIdFields(this.client.$schema, mutationModel);
|
|
937
|
+
returning = kysely.ReturningNode.create(idFields.map((f) => kysely.SelectionNode.create(kysely.ColumnNode.create(f))));
|
|
938
|
+
}
|
|
939
|
+
return {
|
|
940
|
+
...result,
|
|
941
|
+
where: kysely.WhereNode.create(result.where ? conjunction(this.dialect, [result.where.where, filter]) : filter),
|
|
942
|
+
returning
|
|
943
|
+
};
|
|
944
|
+
}
|
|
945
|
+
transformDeleteQuery(node) {
|
|
946
|
+
const result = super.transformDeleteQuery(node);
|
|
947
|
+
const { mutationModel, alias } = this.getMutationModel(node);
|
|
948
|
+
let filter = this.buildPolicyFilter(mutationModel, alias, "delete");
|
|
949
|
+
if (node.using) {
|
|
950
|
+
this.tryRejectNonexistingTables(node.using.tables);
|
|
951
|
+
const joinFilter = this.createPolicyFilterForTables(node.using.tables);
|
|
952
|
+
if (joinFilter) filter = conjunction(this.dialect, [filter, joinFilter]);
|
|
953
|
+
}
|
|
954
|
+
return {
|
|
955
|
+
...result,
|
|
956
|
+
where: kysely.WhereNode.create(result.where ? conjunction(this.dialect, [result.where.where, filter]) : filter)
|
|
957
|
+
};
|
|
958
|
+
}
|
|
959
|
+
async loadBeforeUpdateEntities(model, where, proceed, forceLoad = false) {
|
|
960
|
+
const beforeUpdateAccessFields = this.getFieldsAccessForBeforeUpdatePolicies(model);
|
|
961
|
+
if (!forceLoad && (!beforeUpdateAccessFields || beforeUpdateAccessFields.length === 0)) return;
|
|
962
|
+
const policyFilter = this.buildPolicyFilter(model, model, "update");
|
|
963
|
+
const combinedFilter = where ? conjunction(this.dialect, [where.where, policyFilter]) : policyFilter;
|
|
964
|
+
const selections = beforeUpdateAccessFields ?? _zenstackhq_orm.QueryUtils.requireIdFields(this.client.$schema, model);
|
|
965
|
+
return {
|
|
966
|
+
fields: beforeUpdateAccessFields,
|
|
967
|
+
rows: (await proceed({
|
|
968
|
+
kind: "SelectQueryNode",
|
|
969
|
+
from: kysely.FromNode.create([kysely.TableNode.create(model)]),
|
|
970
|
+
where: kysely.WhereNode.create(combinedFilter),
|
|
971
|
+
selections: selections.map((f) => kysely.SelectionNode.create(kysely.ColumnNode.create(f)))
|
|
972
|
+
})).rows
|
|
973
|
+
};
|
|
974
|
+
}
|
|
975
|
+
getFieldsAccessForBeforeUpdatePolicies(model) {
|
|
976
|
+
const policies = this.getModelPolicies(model, "post-update");
|
|
977
|
+
if (policies.length === 0) return;
|
|
978
|
+
const fields = /* @__PURE__ */ new Set();
|
|
979
|
+
const fieldCollector = new class extends _zenstackhq_orm.SchemaUtils.ExpressionVisitor {
|
|
980
|
+
visitMember(e) {
|
|
981
|
+
if (isBeforeInvocation(e.receiver)) {
|
|
982
|
+
(0, _zenstackhq_common_helpers.invariant)(e.members.length === 1, "before() can only be followed by a scalar field access");
|
|
983
|
+
fields.add(e.members[0]);
|
|
984
|
+
}
|
|
985
|
+
super.visitMember(e);
|
|
986
|
+
}
|
|
987
|
+
}();
|
|
988
|
+
for (const policy of policies) fieldCollector.visit(policy.condition);
|
|
989
|
+
if (fields.size === 0) return;
|
|
990
|
+
_zenstackhq_orm.QueryUtils.requireIdFields(this.client.$schema, model).forEach((f) => fields.add(f));
|
|
991
|
+
return Array.from(fields).sort();
|
|
992
|
+
}
|
|
993
|
+
hasPostUpdatePolicies(model) {
|
|
994
|
+
return this.getModelPolicies(model, "post-update").length > 0;
|
|
995
|
+
}
|
|
996
|
+
createSelectAllFieldsWithPolicies(model, alias, operation) {
|
|
997
|
+
let hasPolicies = false;
|
|
998
|
+
const modelDef = _zenstackhq_orm.QueryUtils.requireModel(this.client.$schema, model);
|
|
999
|
+
let selections = [];
|
|
1000
|
+
for (const fieldDef of Object.values(modelDef.fields).filter((f) => !f.relation && !f.computed && !f.originModel)) {
|
|
1001
|
+
const { hasPolicies: fieldHasPolicies, selection } = this.createFieldSelectionWithPolicy(model, fieldDef.name, operation);
|
|
1002
|
+
hasPolicies = hasPolicies || fieldHasPolicies;
|
|
1003
|
+
selections.push(selection);
|
|
1004
|
+
}
|
|
1005
|
+
if (!hasPolicies) selections = [kysely.SelectionNode.create(kysely.SelectAllNode.create())];
|
|
1006
|
+
const modelPolicyFilter = this.buildPolicyFilter(model, model, operation);
|
|
1007
|
+
if (!isTrueNode(modelPolicyFilter)) hasPolicies = true;
|
|
1008
|
+
const nestedQuery = {
|
|
1009
|
+
kind: "SelectQueryNode",
|
|
1010
|
+
from: kysely.FromNode.create([kysely.TableNode.create(model)]),
|
|
1011
|
+
where: isTrueNode(modelPolicyFilter) ? void 0 : kysely.WhereNode.create(modelPolicyFilter),
|
|
1012
|
+
selections
|
|
1013
|
+
};
|
|
1014
|
+
return {
|
|
1015
|
+
hasPolicies,
|
|
1016
|
+
query: kysely.AliasNode.create(kysely.ParensNode.create(nestedQuery), kysely.IdentifierNode.create(alias ?? model))
|
|
1017
|
+
};
|
|
1018
|
+
}
|
|
1019
|
+
createFieldSelectionWithPolicy(model, field, operation) {
|
|
1020
|
+
const filter = this.buildFieldPolicyFilter(model, field, operation);
|
|
1021
|
+
if (isTrueNode(filter)) return {
|
|
1022
|
+
hasPolicies: false,
|
|
1023
|
+
selection: kysely.SelectionNode.create(kysely.ColumnNode.create(field))
|
|
1024
|
+
};
|
|
1025
|
+
const eb = (0, kysely.expressionBuilder)();
|
|
1026
|
+
const selection = eb.case().when(new kysely.ExpressionWrapper(filter)).then(eb.ref(field)).else(null).end().as(field).toOperationNode();
|
|
1027
|
+
return {
|
|
1028
|
+
hasPolicies: true,
|
|
1029
|
+
selection: kysely.SelectionNode.create(selection)
|
|
1030
|
+
};
|
|
1031
|
+
}
|
|
1032
|
+
hasFieldLevelPolicies(model, operation) {
|
|
1033
|
+
const modelDef = _zenstackhq_orm.QueryUtils.getModel(this.client.$schema, model);
|
|
1034
|
+
if (!modelDef) return false;
|
|
1035
|
+
return Object.keys(modelDef.fields).some((field) => this.getFieldPolicies(model, field, operation).length > 0);
|
|
1036
|
+
}
|
|
1037
|
+
buildFieldPolicyFilter(model, field, operation) {
|
|
1038
|
+
const policies = this.getFieldPolicies(model, field, operation);
|
|
1039
|
+
const allows = policies.filter((policy) => policy.kind === "allow").map((policy) => this.compilePolicyCondition(model, model, operation, policy));
|
|
1040
|
+
const denies = policies.filter((policy) => policy.kind === "deny").map((policy) => this.compilePolicyCondition(model, model, operation, policy));
|
|
1041
|
+
let combinedPolicy;
|
|
1042
|
+
if (allows.length === 0) combinedPolicy = trueNode(this.dialect);
|
|
1043
|
+
else combinedPolicy = disjunction(this.dialect, allows);
|
|
1044
|
+
if (denies.length !== 0) {
|
|
1045
|
+
const combinedDenies = conjunction(this.dialect, denies.map((d) => buildIsFalse(d, this.dialect)));
|
|
1046
|
+
combinedPolicy = conjunction(this.dialect, [combinedPolicy, combinedDenies]);
|
|
1047
|
+
}
|
|
1048
|
+
return combinedPolicy;
|
|
1049
|
+
}
|
|
1050
|
+
onlyReturningId(node) {
|
|
1051
|
+
if (!node.returning) return true;
|
|
1052
|
+
const { mutationModel } = this.getMutationModel(node);
|
|
1053
|
+
const idFields = _zenstackhq_orm.QueryUtils.requireIdFields(this.client.$schema, mutationModel);
|
|
1054
|
+
if (node.returning.selections.some((s) => kysely.SelectAllNode.is(s.selection))) {
|
|
1055
|
+
const modelDef = _zenstackhq_orm.QueryUtils.requireModel(this.client.$schema, mutationModel);
|
|
1056
|
+
if (Object.keys(modelDef.fields).some((f) => !idFields.includes(f))) return false;
|
|
1057
|
+
else return true;
|
|
1058
|
+
}
|
|
1059
|
+
return new ColumnCollector().collect(node.returning).every((c) => idFields.includes(c));
|
|
1060
|
+
}
|
|
1061
|
+
async enforcePreCreatePolicy(node, mutationModel, isManyToManyJoinTable, proceed) {
|
|
1062
|
+
const fields = node.columns?.map((c) => c.column.name) ?? [];
|
|
1063
|
+
const valueRows = node.values ? this.unwrapCreateValueRows(node.values, mutationModel, fields, isManyToManyJoinTable) : [[]];
|
|
1064
|
+
for (const values of valueRows) if (isManyToManyJoinTable) await this.enforcePreCreatePolicyForManyToManyJoinTable(mutationModel, fields, values.map((v) => v.node), proceed);
|
|
1065
|
+
else await this.enforcePreCreatePolicyForOne(mutationModel, fields, values.map((v) => v.node), proceed);
|
|
1066
|
+
}
|
|
1067
|
+
async enforcePreCreatePolicyForManyToManyJoinTable(tableName, fields, values, proceed) {
|
|
1068
|
+
const m2m = this.resolveManyToManyJoinTable(tableName);
|
|
1069
|
+
(0, _zenstackhq_common_helpers.invariant)(m2m);
|
|
1070
|
+
(0, _zenstackhq_common_helpers.invariant)(fields.includes("A") && fields.includes("B"), "many-to-many join table must have A and B fk fields");
|
|
1071
|
+
const aNode = values[fields.indexOf("A")];
|
|
1072
|
+
const bNode = values[fields.indexOf("B")];
|
|
1073
|
+
(0, _zenstackhq_common_helpers.invariant)(kysely.ValueNode.is(aNode) && kysely.ValueNode.is(bNode), "A and B values must be ValueNode");
|
|
1074
|
+
const aValue = aNode.value;
|
|
1075
|
+
const bValue = bNode.value;
|
|
1076
|
+
(0, _zenstackhq_common_helpers.invariant)(aValue !== null && aValue !== void 0, "A value cannot be null or undefined");
|
|
1077
|
+
(0, _zenstackhq_common_helpers.invariant)(bValue !== null && bValue !== void 0, "B value cannot be null or undefined");
|
|
1078
|
+
const eb = (0, kysely.expressionBuilder)();
|
|
1079
|
+
const filterA = this.buildPolicyFilter(m2m.firstModel, void 0, "update");
|
|
1080
|
+
const queryA = eb.selectFrom(m2m.firstModel).where(eb(eb.ref(`${m2m.firstModel}.${m2m.firstIdField}`), "=", aValue)).select(() => new kysely.ExpressionWrapper(filterA).as("_"));
|
|
1081
|
+
const filterB = this.buildPolicyFilter(m2m.secondModel, void 0, "update");
|
|
1082
|
+
const queryB = eb.selectFrom(m2m.secondModel).where(eb(eb.ref(`${m2m.secondModel}.${m2m.secondIdField}`), "=", bValue)).select(() => new kysely.ExpressionWrapper(filterB).as("_"));
|
|
1083
|
+
const result = await proceed({
|
|
1084
|
+
kind: "SelectQueryNode",
|
|
1085
|
+
selections: [kysely.SelectionNode.create(kysely.AliasNode.create(queryA.toOperationNode(), kysely.IdentifierNode.create("$conditionA"))), kysely.SelectionNode.create(kysely.AliasNode.create(queryB.toOperationNode(), kysely.IdentifierNode.create("$conditionB")))]
|
|
1086
|
+
});
|
|
1087
|
+
if (!result.rows[0]?.$conditionA) throw createRejectedByPolicyError(m2m.firstModel, _zenstackhq_orm.RejectedByPolicyReason.CANNOT_READ_BACK, `many-to-many relation participant model "${m2m.firstModel}" not updatable`);
|
|
1088
|
+
if (!result.rows[0]?.$conditionB) throw createRejectedByPolicyError(m2m.secondModel, _zenstackhq_orm.RejectedByPolicyReason.NO_ACCESS, `many-to-many relation participant model "${m2m.secondModel}" not updatable`);
|
|
1089
|
+
}
|
|
1090
|
+
async enforcePreCreatePolicyForOne(model, fields, values, proceed) {
|
|
1091
|
+
const allFields = _zenstackhq_orm.QueryUtils.getModelFields(this.client.$schema, model, { inherited: true });
|
|
1092
|
+
const allValues = [];
|
|
1093
|
+
for (const def of allFields) {
|
|
1094
|
+
const index = fields.indexOf(def.name);
|
|
1095
|
+
if (index >= 0) allValues.push(new kysely.ExpressionWrapper(values[index]));
|
|
1096
|
+
else allValues.push(this.eb.lit(null));
|
|
1097
|
+
}
|
|
1098
|
+
const valuesTable = this.dialect.buildValuesTableSelect(allFields, [allValues]);
|
|
1099
|
+
const filter = this.buildPolicyFilter(model, void 0, "create");
|
|
1100
|
+
if (!(await proceed(this.eb.selectFrom(valuesTable.as(model)).select(this.eb(this.eb.fn.count(this.eb.lit(1)), ">", 0).as("$condition")).where(() => new kysely.ExpressionWrapper(filter)).toOperationNode())).rows[0]?.$condition) throw createRejectedByPolicyError(model, _zenstackhq_orm.RejectedByPolicyReason.NO_ACCESS);
|
|
1101
|
+
}
|
|
1102
|
+
unwrapCreateValueRows(node, model, fields, isManyToManyJoinTable) {
|
|
1103
|
+
if (kysely.ValuesNode.is(node)) return node.values.map((v) => this.unwrapCreateValueRow(v.values, model, fields, isManyToManyJoinTable));
|
|
1104
|
+
else if (kysely.PrimitiveValueListNode.is(node)) return [this.unwrapCreateValueRow(node.values, model, fields, isManyToManyJoinTable)];
|
|
1105
|
+
else (0, _zenstackhq_common_helpers.invariant)(false, `Unexpected node kind: ${node.kind} for unwrapping create values`);
|
|
1106
|
+
}
|
|
1107
|
+
unwrapCreateValueRow(data, model, fields, isImplicitManyToManyJoinTable) {
|
|
1108
|
+
(0, _zenstackhq_common_helpers.invariant)(data.length === fields.length, "data length must match fields length");
|
|
1109
|
+
const result = [];
|
|
1110
|
+
for (let i = 0; i < data.length; i++) {
|
|
1111
|
+
const item = data[i];
|
|
1112
|
+
if (typeof item === "object" && item && "kind" in item) {
|
|
1113
|
+
if (item.kind === "DefaultInsertValueNode") {
|
|
1114
|
+
result.push({
|
|
1115
|
+
node: kysely.ValueNode.create(null),
|
|
1116
|
+
raw: null
|
|
1117
|
+
});
|
|
1118
|
+
continue;
|
|
1119
|
+
}
|
|
1120
|
+
const fieldDef = _zenstackhq_orm.QueryUtils.requireField(this.client.$schema, model, fields[i]);
|
|
1121
|
+
(0, _zenstackhq_common_helpers.invariant)(item.kind === "ValueNode", "expecting a ValueNode");
|
|
1122
|
+
result.push({
|
|
1123
|
+
node: kysely.ValueNode.create(this.dialect.transformInput(item.value, fieldDef.type, !!fieldDef.array)),
|
|
1124
|
+
raw: item.value
|
|
1125
|
+
});
|
|
1126
|
+
} else {
|
|
1127
|
+
let value = item;
|
|
1128
|
+
if (!isImplicitManyToManyJoinTable) {
|
|
1129
|
+
const fieldDef = _zenstackhq_orm.QueryUtils.requireField(this.client.$schema, model, fields[i]);
|
|
1130
|
+
value = this.dialect.transformInput(item, fieldDef.type, !!fieldDef.array);
|
|
1131
|
+
}
|
|
1132
|
+
if (Array.isArray(value)) {
|
|
1133
|
+
const fieldDef = _zenstackhq_orm.QueryUtils.requireField(this.client.$schema, model, fields[i]);
|
|
1134
|
+
result.push({
|
|
1135
|
+
node: this.dialect.buildArrayValue(value, fieldDef.type).toOperationNode(),
|
|
1136
|
+
raw: value
|
|
1137
|
+
});
|
|
1138
|
+
} else result.push({
|
|
1139
|
+
node: kysely.ValueNode.create(value),
|
|
1140
|
+
raw: value
|
|
1141
|
+
});
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
return result;
|
|
1145
|
+
}
|
|
1146
|
+
tryGetConstantPolicy(model, operation) {
|
|
1147
|
+
const policies = this.getModelPolicies(model, operation);
|
|
1148
|
+
if (!policies.some((p) => p.kind === "allow")) return false;
|
|
1149
|
+
else if (policies.some((p) => p.kind === "deny" && this.isTrueExpr(p.condition))) return false;
|
|
1150
|
+
else if (!policies.some((p) => p.kind === "deny") && policies.some((p) => p.kind === "allow" && this.isTrueExpr(p.condition))) return true;
|
|
1151
|
+
else return;
|
|
1152
|
+
}
|
|
1153
|
+
isTrueExpr(expr) {
|
|
1154
|
+
return _zenstackhq_orm_schema.ExpressionUtils.isLiteral(expr) && expr.value === true;
|
|
1155
|
+
}
|
|
1156
|
+
async processReadBack(node, result, proceed) {
|
|
1157
|
+
if (result.rows.length === 0) return result;
|
|
1158
|
+
if (!this.isMutationQueryNode(node) || !node.returning) return result;
|
|
1159
|
+
const { mutationModel } = this.getMutationModel(node);
|
|
1160
|
+
const idConditions = this.buildIdConditions(mutationModel, result.rows);
|
|
1161
|
+
const policyFilter = this.buildPolicyFilter(mutationModel, void 0, "read");
|
|
1162
|
+
return await proceed({
|
|
1163
|
+
kind: "SelectQueryNode",
|
|
1164
|
+
from: kysely.FromNode.create([kysely.TableNode.create(mutationModel)]),
|
|
1165
|
+
where: kysely.WhereNode.create(conjunction(this.dialect, [idConditions, policyFilter])),
|
|
1166
|
+
selections: node.returning.selections
|
|
1167
|
+
});
|
|
1168
|
+
}
|
|
1169
|
+
buildIdConditions(table, rows) {
|
|
1170
|
+
const idFields = _zenstackhq_orm.QueryUtils.requireIdFields(this.client.$schema, table);
|
|
1171
|
+
return disjunction(this.dialect, rows.map((row) => conjunction(this.dialect, idFields.map((field) => kysely.BinaryOperationNode.create(kysely.ReferenceNode.create(kysely.ColumnNode.create(field), kysely.TableNode.create(table)), kysely.OperatorNode.create("="), kysely.ValueNode.create(row[field]))))));
|
|
1172
|
+
}
|
|
1173
|
+
getMutationModel(node) {
|
|
1174
|
+
const r = (0, ts_pattern.match)(node).when(kysely.InsertQueryNode.is, (node) => ({
|
|
1175
|
+
mutationModel: getTableName(node.into),
|
|
1176
|
+
alias: void 0
|
|
1177
|
+
})).when(kysely.UpdateQueryNode.is, (node) => {
|
|
1178
|
+
if (!node.table) (0, _zenstackhq_common_helpers.invariant)(false, "Update query must have a table");
|
|
1179
|
+
const r = this.extractTableName(node.table);
|
|
1180
|
+
return r ? {
|
|
1181
|
+
mutationModel: r.model,
|
|
1182
|
+
alias: r.alias
|
|
1183
|
+
} : void 0;
|
|
1184
|
+
}).when(kysely.DeleteQueryNode.is, (node) => {
|
|
1185
|
+
if (node.from.froms.length !== 1) throw createUnsupportedError("Only one from table is supported for delete");
|
|
1186
|
+
const r = this.extractTableName(node.from.froms[0]);
|
|
1187
|
+
return r ? {
|
|
1188
|
+
mutationModel: r.model,
|
|
1189
|
+
alias: r.alias
|
|
1190
|
+
} : void 0;
|
|
1191
|
+
}).exhaustive();
|
|
1192
|
+
if (!r) (0, _zenstackhq_common_helpers.invariant)(false, `Unable to get table name for query node: ${node}`);
|
|
1193
|
+
return r;
|
|
1194
|
+
}
|
|
1195
|
+
isCrudQueryNode(node) {
|
|
1196
|
+
return kysely.SelectQueryNode.is(node) || kysely.InsertQueryNode.is(node) || kysely.UpdateQueryNode.is(node) || kysely.DeleteQueryNode.is(node);
|
|
1197
|
+
}
|
|
1198
|
+
isMutationQueryNode(node) {
|
|
1199
|
+
return kysely.InsertQueryNode.is(node) || kysely.UpdateQueryNode.is(node) || kysely.DeleteQueryNode.is(node);
|
|
1200
|
+
}
|
|
1201
|
+
buildPolicyFilter(model, alias, operation) {
|
|
1202
|
+
const m2mFilter = this.getModelPolicyFilterForManyToManyJoinTable(model, alias, operation);
|
|
1203
|
+
if (m2mFilter) return m2mFilter;
|
|
1204
|
+
const policies = this.getModelPolicies(model, operation);
|
|
1205
|
+
const allows = policies.filter((policy) => policy.kind === "allow").map((policy) => this.compilePolicyCondition(model, alias, operation, policy));
|
|
1206
|
+
const denies = policies.filter((policy) => policy.kind === "deny").map((policy) => this.compilePolicyCondition(model, alias, operation, policy));
|
|
1207
|
+
let combinedPolicy;
|
|
1208
|
+
if (allows.length === 0) if (operation === "post-update") combinedPolicy = trueNode(this.dialect);
|
|
1209
|
+
else combinedPolicy = falseNode(this.dialect);
|
|
1210
|
+
else combinedPolicy = disjunction(this.dialect, allows);
|
|
1211
|
+
if (denies.length !== 0) {
|
|
1212
|
+
const combinedDenies = conjunction(this.dialect, denies.map((d) => buildIsFalse(d, this.dialect)));
|
|
1213
|
+
combinedPolicy = conjunction(this.dialect, [combinedPolicy, combinedDenies]);
|
|
1214
|
+
}
|
|
1215
|
+
return combinedPolicy;
|
|
1216
|
+
}
|
|
1217
|
+
extractTableName(node) {
|
|
1218
|
+
if (kysely.TableNode.is(node)) return { model: node.table.identifier.name };
|
|
1219
|
+
if (kysely.AliasNode.is(node)) {
|
|
1220
|
+
const inner = this.extractTableName(node.node);
|
|
1221
|
+
if (!inner) return;
|
|
1222
|
+
return {
|
|
1223
|
+
model: inner.model,
|
|
1224
|
+
alias: kysely.IdentifierNode.is(node.alias) ? node.alias.name : void 0
|
|
1225
|
+
};
|
|
1226
|
+
} else return;
|
|
1227
|
+
}
|
|
1228
|
+
createPolicyFilterForFrom(node) {
|
|
1229
|
+
if (!node) return;
|
|
1230
|
+
return this.createPolicyFilterForTables(node.froms);
|
|
1231
|
+
}
|
|
1232
|
+
createPolicyFilterForTables(tables) {
|
|
1233
|
+
return tables.reduce((acc, table) => {
|
|
1234
|
+
const extractResult = this.extractTableName(table);
|
|
1235
|
+
if (extractResult) {
|
|
1236
|
+
const { model, alias } = extractResult;
|
|
1237
|
+
const filter = this.buildPolicyFilter(model, alias, "read");
|
|
1238
|
+
return acc ? conjunction(this.dialect, [acc, filter]) : filter;
|
|
1239
|
+
}
|
|
1240
|
+
return acc;
|
|
1241
|
+
}, void 0);
|
|
1242
|
+
}
|
|
1243
|
+
compilePolicyCondition(model, alias, operation, policy) {
|
|
1244
|
+
return new ExpressionTransformer(this.client).transform(policy.condition, {
|
|
1245
|
+
modelOrType: model,
|
|
1246
|
+
thisType: model,
|
|
1247
|
+
thisAlias: alias,
|
|
1248
|
+
alias,
|
|
1249
|
+
operation
|
|
1250
|
+
});
|
|
1251
|
+
}
|
|
1252
|
+
getModelPolicies(model, operation) {
|
|
1253
|
+
const modelDef = _zenstackhq_orm.QueryUtils.requireModel(this.client.$schema, model);
|
|
1254
|
+
const result = [];
|
|
1255
|
+
const extractOperations = (expr) => {
|
|
1256
|
+
(0, _zenstackhq_common_helpers.invariant)(_zenstackhq_orm_schema.ExpressionUtils.isLiteral(expr), "expecting a literal");
|
|
1257
|
+
(0, _zenstackhq_common_helpers.invariant)(typeof expr.value === "string", "expecting a string literal");
|
|
1258
|
+
return expr.value.split(",").filter((v) => !!v).map((v) => v.trim());
|
|
1259
|
+
};
|
|
1260
|
+
if (modelDef.attributes) result.push(...modelDef.attributes.filter((attr) => attr.name === "@@allow" || attr.name === "@@deny").map((attr) => ({
|
|
1261
|
+
kind: attr.name === "@@allow" ? "allow" : "deny",
|
|
1262
|
+
operations: extractOperations(attr.args[0].value),
|
|
1263
|
+
condition: attr.args[1].value
|
|
1264
|
+
})).filter((policy) => operation !== "post-update" && policy.operations.includes("all") || policy.operations.includes(operation)));
|
|
1265
|
+
return result;
|
|
1266
|
+
}
|
|
1267
|
+
getFieldPolicies(model, field, operation) {
|
|
1268
|
+
const fieldDef = _zenstackhq_orm.QueryUtils.requireField(this.client.$schema, model, field);
|
|
1269
|
+
const result = [];
|
|
1270
|
+
const extractOperations = (expr) => {
|
|
1271
|
+
(0, _zenstackhq_common_helpers.invariant)(_zenstackhq_orm_schema.ExpressionUtils.isLiteral(expr), "expecting a literal");
|
|
1272
|
+
(0, _zenstackhq_common_helpers.invariant)(typeof expr.value === "string", "expecting a string literal");
|
|
1273
|
+
return expr.value.split(",").filter((v) => !!v).map((v) => v.trim());
|
|
1274
|
+
};
|
|
1275
|
+
if (fieldDef.attributes) result.push(...fieldDef.attributes.filter((attr) => attr.name === "@allow" || attr.name === "@deny").map((attr) => ({
|
|
1276
|
+
kind: attr.name === "@allow" ? "allow" : "deny",
|
|
1277
|
+
operations: extractOperations(attr.args[0].value),
|
|
1278
|
+
condition: attr.args[1].value
|
|
1279
|
+
})).filter((policy) => policy.operations.includes("all") || policy.operations.includes(operation)));
|
|
1280
|
+
return result;
|
|
1281
|
+
}
|
|
1282
|
+
resolveManyToManyJoinTable(tableName) {
|
|
1283
|
+
for (const model of Object.values(this.client.$schema.models)) for (const field of Object.values(model.fields)) {
|
|
1284
|
+
const m2m = _zenstackhq_orm.QueryUtils.getManyToManyRelation(this.client.$schema, model.name, field.name);
|
|
1285
|
+
if (m2m?.joinTable === tableName) {
|
|
1286
|
+
const sortedRecord = [{
|
|
1287
|
+
model: model.name,
|
|
1288
|
+
field: field.name
|
|
1289
|
+
}, {
|
|
1290
|
+
model: m2m.otherModel,
|
|
1291
|
+
field: m2m.otherField
|
|
1292
|
+
}].sort(this.manyToManySorter);
|
|
1293
|
+
const firstIdFields = _zenstackhq_orm.QueryUtils.requireIdFields(this.client.$schema, sortedRecord[0].model);
|
|
1294
|
+
const secondIdFields = _zenstackhq_orm.QueryUtils.requireIdFields(this.client.$schema, sortedRecord[1].model);
|
|
1295
|
+
(0, _zenstackhq_common_helpers.invariant)(firstIdFields.length === 1 && secondIdFields.length === 1, "only single-field id is supported for implicit many-to-many join table");
|
|
1296
|
+
return {
|
|
1297
|
+
firstModel: sortedRecord[0].model,
|
|
1298
|
+
firstField: sortedRecord[0].field,
|
|
1299
|
+
firstIdField: firstIdFields[0],
|
|
1300
|
+
secondModel: sortedRecord[1].model,
|
|
1301
|
+
secondField: sortedRecord[1].field,
|
|
1302
|
+
secondIdField: secondIdFields[0]
|
|
1303
|
+
};
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
manyToManySorter(a, b) {
|
|
1308
|
+
return a.model !== b.model ? a.model.localeCompare(b.model) : a.field.localeCompare(b.field);
|
|
1309
|
+
}
|
|
1310
|
+
isManyToManyJoinTable(tableName) {
|
|
1311
|
+
return !!this.resolveManyToManyJoinTable(tableName);
|
|
1312
|
+
}
|
|
1313
|
+
getModelPolicyFilterForManyToManyJoinTable(tableName, alias, operation) {
|
|
1314
|
+
const m2m = this.resolveManyToManyJoinTable(tableName);
|
|
1315
|
+
if (!m2m) return;
|
|
1316
|
+
const checkForOperation = operation === "read" ? "read" : "update";
|
|
1317
|
+
const joinTable = alias ?? tableName;
|
|
1318
|
+
const aQuery = this.eb.selectFrom(m2m.firstModel).whereRef(`${m2m.firstModel}.${m2m.firstIdField}`, "=", `${joinTable}.A`).select(() => new kysely.ExpressionWrapper(this.buildPolicyFilter(m2m.firstModel, void 0, checkForOperation)).as("$conditionA"));
|
|
1319
|
+
const bQuery = this.eb.selectFrom(m2m.secondModel).whereRef(`${m2m.secondModel}.${m2m.secondIdField}`, "=", `${joinTable}.B`).select(() => new kysely.ExpressionWrapper(this.buildPolicyFilter(m2m.secondModel, void 0, checkForOperation)).as("$conditionB"));
|
|
1320
|
+
return this.eb.and([aQuery, bQuery]).toOperationNode();
|
|
1321
|
+
}
|
|
1322
|
+
tryRejectNonexistentModel(model) {
|
|
1323
|
+
if (!_zenstackhq_orm.QueryUtils.hasModel(this.client.$schema, model) && !this.isManyToManyJoinTable(model)) throw createRejectedByPolicyError(model, _zenstackhq_orm.RejectedByPolicyReason.NO_ACCESS);
|
|
1324
|
+
}
|
|
1325
|
+
tryRejectNonexistingTables(tables) {
|
|
1326
|
+
for (const table of tables) {
|
|
1327
|
+
const extractResult = this.extractTableName(table);
|
|
1328
|
+
if (extractResult) this.tryRejectNonexistentModel(extractResult.model);
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
postProcessMutationResult(result, node) {
|
|
1332
|
+
if (node.returning) return result;
|
|
1333
|
+
else return {
|
|
1334
|
+
...result,
|
|
1335
|
+
rows: [],
|
|
1336
|
+
numAffectedRows: result.numAffectedRows ?? BigInt(result.rows.length)
|
|
1337
|
+
};
|
|
1338
|
+
}
|
|
1339
|
+
stripTableReferences(node, modelName) {
|
|
1340
|
+
return new TableReferenceStripper().strip(node, modelName);
|
|
1341
|
+
}
|
|
1896
1342
|
};
|
|
1897
|
-
var TableReferenceStripper = class
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
if (import_kysely3.ColumnNode.is(node.column) && node.table?.table.identifier.name === this.tableName) {
|
|
1908
|
-
return import_kysely3.ReferenceNode.create(this.transformNode(node.column));
|
|
1909
|
-
}
|
|
1910
|
-
return super.transformReference(node);
|
|
1911
|
-
}
|
|
1343
|
+
var TableReferenceStripper = class extends kysely.OperationNodeTransformer {
|
|
1344
|
+
tableName = "";
|
|
1345
|
+
strip(node, tableName) {
|
|
1346
|
+
this.tableName = tableName;
|
|
1347
|
+
return this.transformNode(node);
|
|
1348
|
+
}
|
|
1349
|
+
transformReference(node) {
|
|
1350
|
+
if (kysely.ColumnNode.is(node.column) && node.table?.table.identifier.name === this.tableName) return kysely.ReferenceNode.create(this.transformNode(node.column));
|
|
1351
|
+
return super.transformReference(node);
|
|
1352
|
+
}
|
|
1912
1353
|
};
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
const op = arg2Node ? arg2Node.value : operation;
|
|
1959
|
-
const policyCondition = policyHandler.buildPolicyFilter(relationModel, void 0, op);
|
|
1960
|
-
const result = eb.selectFrom(eb.selectFrom(relationModel).where(joinCondition).select(new import_kysely4.ExpressionWrapper(policyCondition).as("$condition")).as("$sub")).selectAll();
|
|
1961
|
-
return result;
|
|
1962
|
-
}, "check");
|
|
1963
|
-
|
|
1964
|
-
// src/plugin.ts
|
|
1354
|
+
//#endregion
|
|
1355
|
+
//#region src/functions.ts
|
|
1356
|
+
/**
|
|
1357
|
+
* Relation checker implementation.
|
|
1358
|
+
*/
|
|
1359
|
+
const check = (eb, args, { client, model, modelAlias, operation }) => {
|
|
1360
|
+
(0, _zenstackhq_common_helpers.invariant)(args.length === 1 || args.length === 2, "\"check\" function requires 1 or 2 arguments");
|
|
1361
|
+
const arg1Node = args[0].toOperationNode();
|
|
1362
|
+
const arg2Node = args.length === 2 ? args[1].toOperationNode() : void 0;
|
|
1363
|
+
if (arg2Node) {
|
|
1364
|
+
(0, _zenstackhq_common_helpers.invariant)(kysely.ValueNode.is(arg2Node) && typeof arg2Node.value === "string", "\"operation\" parameter must be a string literal when provided");
|
|
1365
|
+
(0, _zenstackhq_common_helpers.invariant)(_zenstackhq_orm.CRUD.includes(arg2Node.value), "\"operation\" parameter must be one of \"create\", \"read\", \"update\", \"delete\"");
|
|
1366
|
+
}
|
|
1367
|
+
const fieldName = _zenstackhq_orm.QueryUtils.extractFieldName(arg1Node);
|
|
1368
|
+
(0, _zenstackhq_common_helpers.invariant)(fieldName, "Failed to extract field name from the first argument of \"check\" function");
|
|
1369
|
+
const fieldDef = _zenstackhq_orm.QueryUtils.requireField(client.$schema, model, fieldName);
|
|
1370
|
+
(0, _zenstackhq_common_helpers.invariant)(fieldDef.relation, `Field "${fieldName}" is not a relation field in model "${model}"`);
|
|
1371
|
+
(0, _zenstackhq_common_helpers.invariant)(!fieldDef.array, `Field "${fieldName}" is a to-many relation, which is not supported by "check"`);
|
|
1372
|
+
const relationModel = fieldDef.type;
|
|
1373
|
+
const joinConditions = [];
|
|
1374
|
+
const fkInfo = _zenstackhq_orm.QueryUtils.getRelationForeignKeyFieldPairs(client.$schema, model, fieldName);
|
|
1375
|
+
const idFields = _zenstackhq_orm.QueryUtils.requireIdFields(client.$schema, model);
|
|
1376
|
+
const buildBaseSelect = (baseModel, field) => {
|
|
1377
|
+
return eb.selectFrom(baseModel).select(field).where(eb.and(idFields.map((idField) => eb(eb.ref(`${fieldDef.originModel}.${idField}`), "=", eb.ref(`${modelAlias}.${idField}`)))));
|
|
1378
|
+
};
|
|
1379
|
+
if (fkInfo.ownedByModel) joinConditions.push(...fkInfo.keyPairs.map(({ fk, pk }) => {
|
|
1380
|
+
let fkRef;
|
|
1381
|
+
if (fieldDef.originModel && fieldDef.originModel !== model) fkRef = buildBaseSelect(fieldDef.originModel, fk);
|
|
1382
|
+
else fkRef = eb.ref(`${modelAlias}.${fk}`);
|
|
1383
|
+
return eb(fkRef, "=", eb.ref(`${relationModel}.${pk}`));
|
|
1384
|
+
}));
|
|
1385
|
+
else joinConditions.push(...fkInfo.keyPairs.map(({ fk, pk }) => {
|
|
1386
|
+
let pkRef;
|
|
1387
|
+
if (fieldDef.originModel && fieldDef.originModel !== model) pkRef = buildBaseSelect(fieldDef.originModel, pk);
|
|
1388
|
+
else pkRef = eb.ref(`${modelAlias}.${pk}`);
|
|
1389
|
+
return eb(pkRef, "=", eb.ref(`${relationModel}.${fk}`));
|
|
1390
|
+
}));
|
|
1391
|
+
const joinCondition = joinConditions.length === 1 ? joinConditions[0] : eb.and(joinConditions);
|
|
1392
|
+
const policyHandler = new PolicyHandler(client);
|
|
1393
|
+
const op = arg2Node ? arg2Node.value : operation;
|
|
1394
|
+
const policyCondition = policyHandler.buildPolicyFilter(relationModel, void 0, op);
|
|
1395
|
+
return eb.selectFrom(eb.selectFrom(relationModel).where(joinCondition).select(new kysely.ExpressionWrapper(policyCondition).as("$condition")).as("$sub")).selectAll();
|
|
1396
|
+
};
|
|
1397
|
+
//#endregion
|
|
1398
|
+
//#region src/plugin.ts
|
|
1965
1399
|
var PolicyPlugin = class {
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
check
|
|
1985
|
-
};
|
|
1986
|
-
}
|
|
1987
|
-
onKyselyQuery({ query, client, proceed }) {
|
|
1988
|
-
const handler = new PolicyHandler(client, this.options);
|
|
1989
|
-
return handler.handle(query, proceed);
|
|
1990
|
-
}
|
|
1400
|
+
constructor(options = {}) {
|
|
1401
|
+
this.options = options;
|
|
1402
|
+
}
|
|
1403
|
+
get id() {
|
|
1404
|
+
return "policy";
|
|
1405
|
+
}
|
|
1406
|
+
get name() {
|
|
1407
|
+
return "Access Policy";
|
|
1408
|
+
}
|
|
1409
|
+
get description() {
|
|
1410
|
+
return "Enforces access policies defined in the schema.";
|
|
1411
|
+
}
|
|
1412
|
+
get functions() {
|
|
1413
|
+
return { check };
|
|
1414
|
+
}
|
|
1415
|
+
onKyselyQuery({ query, client, proceed }) {
|
|
1416
|
+
return new PolicyHandler(client, this.options).handle(query, proceed);
|
|
1417
|
+
}
|
|
1991
1418
|
};
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
});
|
|
1419
|
+
//#endregion
|
|
1420
|
+
exports.PolicyPlugin = PolicyPlugin;
|
|
1421
|
+
|
|
1996
1422
|
//# sourceMappingURL=index.cjs.map
|