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