@cerbos/orm-prisma 2.0.1 → 3.1.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/README.md +59 -2
- package/lib/{types/index.d.ts → index.d.ts} +3 -9
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +629 -0
- package/lib/index.js.map +1 -0
- package/package.json +47 -32
- package/src/generated/prisma/browser.ts +59 -0
- package/src/generated/prisma/client.ts +79 -0
- package/src/generated/prisma/commonInputTypes.ts +248 -0
- package/src/generated/prisma/enums.ts +15 -0
- package/src/generated/prisma/internal/class.ts +260 -0
- package/src/generated/prisma/internal/prismaNamespace.ts +1333 -0
- package/src/generated/prisma/internal/prismaNamespaceBrowser.ts +168 -0
- package/src/generated/prisma/models/Category.ts +1407 -0
- package/src/generated/prisma/models/Label.ts +1271 -0
- package/src/generated/prisma/models/NestedResource.ts +1557 -0
- package/src/generated/prisma/models/NextLevelNestedResource.ts +1332 -0
- package/src/generated/prisma/models/Resource.ts +2131 -0
- package/src/generated/prisma/models/SubCategory.ts +1407 -0
- package/src/generated/prisma/models/Tag.ts +1271 -0
- package/src/generated/prisma/models/User.ts +1498 -0
- package/src/generated/prisma/models.ts +19 -0
- package/src/generated/prisma-v6/client.d.ts +1 -0
- package/src/generated/prisma-v6/default.d.ts +1 -0
- package/src/generated/prisma-v6/edge.d.ts +1 -0
- package/src/generated/prisma-v6/index.d.ts +13639 -0
- package/src/generated/prisma-v6/runtime/index-browser.d.ts +370 -0
- package/src/generated/prisma-v6/runtime/library.d.ts +3982 -0
- package/src/generated/prisma-v6/wasm.d.ts +1 -0
- package/src/index.test.ts +4869 -0
- package/src/index.ts +954 -0
- package/src/test-setup.v6.ts +5 -0
- package/src/test-setup.v7.ts +7 -0
- package/lib/cjs/index.js +0 -517
- package/lib/cjs/index.js.map +0 -1
- package/lib/esm/index.js +0 -513
- package/lib/esm/index.js.map +0 -1
- package/lib/types/index.d.ts.map +0 -1
package/src/index.ts
ADDED
|
@@ -0,0 +1,954 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PlanResourcesResponse,
|
|
3
|
+
PlanExpressionOperand,
|
|
4
|
+
PlanKind,
|
|
5
|
+
Value,
|
|
6
|
+
} from "@cerbos/core";
|
|
7
|
+
|
|
8
|
+
export { PlanKind };
|
|
9
|
+
|
|
10
|
+
// Type Definitions
|
|
11
|
+
export type PrismaFilter = Record<string, any>;
|
|
12
|
+
|
|
13
|
+
export type MapperConfig = {
|
|
14
|
+
field?: string;
|
|
15
|
+
relation?: {
|
|
16
|
+
name: string;
|
|
17
|
+
type: "one" | "many";
|
|
18
|
+
field?: string;
|
|
19
|
+
fields?: Record<string, MapperConfig>;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type Mapper =
|
|
24
|
+
| Record<string, MapperConfig>
|
|
25
|
+
| ((key: string) => MapperConfig);
|
|
26
|
+
|
|
27
|
+
export interface QueryPlanToPrismaArgs {
|
|
28
|
+
queryPlan: PlanResourcesResponse;
|
|
29
|
+
mapper?: Mapper;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type QueryPlanToPrismaResult =
|
|
33
|
+
| {
|
|
34
|
+
kind: PlanKind.ALWAYS_ALLOWED | PlanKind.ALWAYS_DENIED;
|
|
35
|
+
}
|
|
36
|
+
| {
|
|
37
|
+
kind: PlanKind.CONDITIONAL;
|
|
38
|
+
filters: PrismaFilter;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// Type guards for operands
|
|
42
|
+
interface NamedOperand {
|
|
43
|
+
name: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface ValueOperand {
|
|
47
|
+
value: Value;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface OperatorOperand {
|
|
51
|
+
operator: string;
|
|
52
|
+
operands: PlanExpressionOperand[];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function isNamedOperand(
|
|
56
|
+
operand: PlanExpressionOperand
|
|
57
|
+
): operand is NamedOperand {
|
|
58
|
+
return "name" in operand && typeof operand.name === "string";
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function isValueOperand(
|
|
62
|
+
operand: PlanExpressionOperand
|
|
63
|
+
): operand is ValueOperand {
|
|
64
|
+
return "value" in operand && operand.value !== undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function isOperatorOperand(
|
|
68
|
+
operand: PlanExpressionOperand
|
|
69
|
+
): operand is OperatorOperand {
|
|
70
|
+
return (
|
|
71
|
+
"operator" in operand &&
|
|
72
|
+
typeof operand.operator === "string" &&
|
|
73
|
+
"operands" in operand &&
|
|
74
|
+
Array.isArray(operand.operands)
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function assertDefined<T>(value: T | undefined, message: string): T {
|
|
79
|
+
if (value === undefined) {
|
|
80
|
+
throw new Error(message);
|
|
81
|
+
}
|
|
82
|
+
return value;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function getLeafField(path: string[]): string {
|
|
86
|
+
const fieldName = path[path.length - 1];
|
|
87
|
+
if (!fieldName) {
|
|
88
|
+
throw new Error("Field path cannot be empty");
|
|
89
|
+
}
|
|
90
|
+
return fieldName;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function getFilterEntry(filter: Record<string, unknown>): [string, unknown] {
|
|
94
|
+
const entry = Object.entries(filter)[0];
|
|
95
|
+
if (!entry) {
|
|
96
|
+
throw new Error("Filter must contain at least one entry");
|
|
97
|
+
}
|
|
98
|
+
return entry;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Field reference resolution types
|
|
102
|
+
type RelationConfig = {
|
|
103
|
+
name: string;
|
|
104
|
+
type: "one" | "many";
|
|
105
|
+
field?: string;
|
|
106
|
+
nestedMapper?: Record<string, MapperConfig>;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
type ResolvedFieldReference = {
|
|
110
|
+
path: string[];
|
|
111
|
+
relations?: RelationConfig[];
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
type ResolvedValue = {
|
|
115
|
+
value: any;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
type ResolvedOperand = ResolvedFieldReference | ResolvedValue;
|
|
119
|
+
|
|
120
|
+
function isResolvedFieldReference(
|
|
121
|
+
operand: ResolvedOperand
|
|
122
|
+
): operand is ResolvedFieldReference {
|
|
123
|
+
return "path" in operand;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function isResolvedValue(operand: ResolvedOperand): operand is ResolvedValue {
|
|
127
|
+
return "value" in operand;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Converts a Cerbos query plan to a Prisma filter.
|
|
132
|
+
*/
|
|
133
|
+
export function queryPlanToPrisma({
|
|
134
|
+
queryPlan,
|
|
135
|
+
mapper = {},
|
|
136
|
+
}: QueryPlanToPrismaArgs): QueryPlanToPrismaResult {
|
|
137
|
+
switch (queryPlan.kind) {
|
|
138
|
+
case PlanKind.ALWAYS_ALLOWED:
|
|
139
|
+
return { kind: PlanKind.ALWAYS_ALLOWED };
|
|
140
|
+
case PlanKind.ALWAYS_DENIED:
|
|
141
|
+
return { kind: PlanKind.ALWAYS_DENIED };
|
|
142
|
+
case PlanKind.CONDITIONAL:
|
|
143
|
+
return {
|
|
144
|
+
kind: PlanKind.CONDITIONAL,
|
|
145
|
+
filters: buildPrismaFilterFromCerbosExpression(
|
|
146
|
+
queryPlan.condition,
|
|
147
|
+
mapper
|
|
148
|
+
),
|
|
149
|
+
};
|
|
150
|
+
default:
|
|
151
|
+
throw Error(`Invalid query plan.`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Resolves a field reference considering relations and nested fields.
|
|
157
|
+
*/
|
|
158
|
+
function resolveFieldReference(
|
|
159
|
+
reference: string,
|
|
160
|
+
mapper: Mapper
|
|
161
|
+
): ResolvedFieldReference {
|
|
162
|
+
const parts = reference.split(".");
|
|
163
|
+
const config =
|
|
164
|
+
typeof mapper === "function" ? mapper(reference) : mapper[reference];
|
|
165
|
+
|
|
166
|
+
let matchedPrefix = "";
|
|
167
|
+
let matchedConfig: MapperConfig | undefined;
|
|
168
|
+
|
|
169
|
+
// If no direct match, look for partial matches
|
|
170
|
+
if (!config) {
|
|
171
|
+
for (let i = parts.length - 1; i >= 0; i--) {
|
|
172
|
+
const prefix = parts.slice(0, i + 1).join(".");
|
|
173
|
+
const prefixConfig =
|
|
174
|
+
typeof mapper === "function" ? mapper(prefix) : mapper[prefix];
|
|
175
|
+
|
|
176
|
+
if (prefixConfig) {
|
|
177
|
+
matchedPrefix = prefix;
|
|
178
|
+
matchedConfig = prefixConfig;
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const activeConfig = config ?? matchedConfig;
|
|
185
|
+
|
|
186
|
+
// Handle relation mapping
|
|
187
|
+
if (activeConfig?.relation) {
|
|
188
|
+
const { name, type, fields } = activeConfig.relation;
|
|
189
|
+
const matchedParts = matchedPrefix ? matchedPrefix.split(".") : [];
|
|
190
|
+
const remainingParts = matchedPrefix
|
|
191
|
+
? parts.slice(matchedParts.length)
|
|
192
|
+
: parts.slice(1);
|
|
193
|
+
|
|
194
|
+
let field: string | undefined;
|
|
195
|
+
const relations: RelationConfig[] = [
|
|
196
|
+
{
|
|
197
|
+
name,
|
|
198
|
+
type,
|
|
199
|
+
field: activeConfig.relation.field,
|
|
200
|
+
nestedMapper: fields,
|
|
201
|
+
},
|
|
202
|
+
];
|
|
203
|
+
|
|
204
|
+
// Process nested relations
|
|
205
|
+
if (fields && remainingParts.length > 0) {
|
|
206
|
+
let currentMapper: Record<string, MapperConfig> | undefined = fields;
|
|
207
|
+
let currentParts = remainingParts;
|
|
208
|
+
|
|
209
|
+
while (currentParts.length > 0) {
|
|
210
|
+
if (!currentMapper) {
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const currentPart = currentParts[0];
|
|
215
|
+
if (!currentPart) {
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const nextConfig: MapperConfig | undefined = currentMapper[currentPart];
|
|
220
|
+
if (nextConfig?.relation) {
|
|
221
|
+
relations.push({
|
|
222
|
+
name: nextConfig.relation.name,
|
|
223
|
+
type: nextConfig.relation.type,
|
|
224
|
+
field: nextConfig.relation.field,
|
|
225
|
+
nestedMapper: nextConfig.relation.fields,
|
|
226
|
+
});
|
|
227
|
+
currentMapper = nextConfig.relation.fields || {};
|
|
228
|
+
currentParts = currentParts.slice(1);
|
|
229
|
+
} else {
|
|
230
|
+
const lastPart = currentParts[currentParts.length - 1];
|
|
231
|
+
if (!lastPart) {
|
|
232
|
+
break;
|
|
233
|
+
}
|
|
234
|
+
field = nextConfig?.field || lastPart;
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return { path: field ? [field] : remainingParts, relations };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Simple field mapping
|
|
244
|
+
return { path: [activeConfig?.field || reference] };
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Determines the appropriate Prisma operator based on relation type.
|
|
249
|
+
*/
|
|
250
|
+
function getPrismaRelationOperator(relation: {
|
|
251
|
+
name: string;
|
|
252
|
+
type: "one" | "many";
|
|
253
|
+
field?: string;
|
|
254
|
+
}): string {
|
|
255
|
+
return relation.type === "one" ? "is" : "some";
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Builds a nested relation filter for Prisma queries.
|
|
260
|
+
*/
|
|
261
|
+
function buildNestedRelationFilter(
|
|
262
|
+
relations: RelationConfig[],
|
|
263
|
+
fieldFilter: any
|
|
264
|
+
): any {
|
|
265
|
+
if (relations.length === 0) return fieldFilter;
|
|
266
|
+
|
|
267
|
+
let currentFilter = fieldFilter;
|
|
268
|
+
|
|
269
|
+
// Build nested structure from inside out
|
|
270
|
+
for (let i = relations.length - 1; i >= 0; i--) {
|
|
271
|
+
const relation = relations[i];
|
|
272
|
+
if (!relation) {
|
|
273
|
+
throw new Error("Relation mapping is missing");
|
|
274
|
+
}
|
|
275
|
+
const relationOperator = getPrismaRelationOperator(relation);
|
|
276
|
+
|
|
277
|
+
// Handle special case for the deepest relation
|
|
278
|
+
if (relation.field && i === relations.length - 1) {
|
|
279
|
+
const [key, filterValue] = getFilterEntry(currentFilter);
|
|
280
|
+
if (key === "NOT") {
|
|
281
|
+
currentFilter = { NOT: { [relation.field]: filterValue } };
|
|
282
|
+
} else {
|
|
283
|
+
currentFilter = { [relation.field]: filterValue };
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
currentFilter = { [relation.name]: { [relationOperator]: currentFilter } };
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
return currentFilter;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Resolves a PlanExpressionOperand into a ResolvedOperand.
|
|
295
|
+
*/
|
|
296
|
+
function resolveOperand(
|
|
297
|
+
operand: PlanExpressionOperand,
|
|
298
|
+
mapper: Mapper
|
|
299
|
+
): ResolvedOperand {
|
|
300
|
+
if (isNamedOperand(operand)) {
|
|
301
|
+
return resolveFieldReference(operand.name, mapper);
|
|
302
|
+
} else if (isValueOperand(operand)) {
|
|
303
|
+
return { value: operand.value };
|
|
304
|
+
} else if (isOperatorOperand(operand)) {
|
|
305
|
+
const nestedResult = buildPrismaFilterFromCerbosExpression(operand, mapper);
|
|
306
|
+
return { value: nestedResult };
|
|
307
|
+
}
|
|
308
|
+
throw new Error("Operand must have name, value, or be an expression");
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Creates a scoped mapper for collection operations
|
|
313
|
+
*/
|
|
314
|
+
function createScopedMapper(
|
|
315
|
+
collectionPath: string,
|
|
316
|
+
variableName: string,
|
|
317
|
+
fullMapper: Mapper
|
|
318
|
+
): Mapper {
|
|
319
|
+
return (key: string) => {
|
|
320
|
+
// If the key starts with the variable name, it's accessing the collection item
|
|
321
|
+
if (key.startsWith(variableName + ".")) {
|
|
322
|
+
const strippedKey = key.replace(variableName + ".", "");
|
|
323
|
+
const parts = strippedKey.split(".");
|
|
324
|
+
|
|
325
|
+
// Get the collection's relation config
|
|
326
|
+
const collectionConfig =
|
|
327
|
+
typeof fullMapper === "function"
|
|
328
|
+
? fullMapper(collectionPath)
|
|
329
|
+
: fullMapper[collectionPath];
|
|
330
|
+
|
|
331
|
+
if (collectionConfig?.relation?.fields) {
|
|
332
|
+
// For nested paths, traverse the fields configuration
|
|
333
|
+
const baseConfig = collectionConfig.relation.fields;
|
|
334
|
+
if (!baseConfig) {
|
|
335
|
+
return { field: strippedKey };
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
let currentConfig = baseConfig;
|
|
339
|
+
let field = parts[0] || strippedKey;
|
|
340
|
+
|
|
341
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
342
|
+
const part = parts[i];
|
|
343
|
+
const nextPart = parts[i + 1];
|
|
344
|
+
if (!part || !nextPart) {
|
|
345
|
+
break;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const nextConfig = currentConfig[part];
|
|
349
|
+
if (nextConfig?.relation?.fields) {
|
|
350
|
+
currentConfig = nextConfig.relation.fields;
|
|
351
|
+
field = nextPart;
|
|
352
|
+
} else {
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
if (!field) {
|
|
358
|
+
field = strippedKey;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// Return the field config if it exists, otherwise create a default one
|
|
362
|
+
return currentConfig[field] || { field };
|
|
363
|
+
}
|
|
364
|
+
return { field: strippedKey };
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// For keys not referencing the collection item, use the full mapper
|
|
368
|
+
if (typeof fullMapper === "function") {
|
|
369
|
+
return fullMapper(key);
|
|
370
|
+
}
|
|
371
|
+
return fullMapper[key] || { field: key };
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Builds a Prisma filter from a Cerbos expression.
|
|
377
|
+
*/
|
|
378
|
+
function buildPrismaFilterFromCerbosExpression(
|
|
379
|
+
expression: PlanExpressionOperand,
|
|
380
|
+
mapper: Mapper
|
|
381
|
+
): PrismaFilter {
|
|
382
|
+
// Validate expression structure
|
|
383
|
+
if (!isOperatorOperand(expression)) {
|
|
384
|
+
throw new Error("Invalid Cerbos expression structure");
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const { operator, operands } = expression;
|
|
388
|
+
|
|
389
|
+
// Process different operator types
|
|
390
|
+
switch (operator) {
|
|
391
|
+
case "and":
|
|
392
|
+
return {
|
|
393
|
+
AND: operands.map((operand) =>
|
|
394
|
+
buildPrismaFilterFromCerbosExpression(operand, mapper)
|
|
395
|
+
),
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
case "or":
|
|
399
|
+
return {
|
|
400
|
+
OR: operands.map((operand) =>
|
|
401
|
+
buildPrismaFilterFromCerbosExpression(operand, mapper)
|
|
402
|
+
),
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
case "not": {
|
|
406
|
+
const operand = operands[0];
|
|
407
|
+
if (!operand) {
|
|
408
|
+
throw new Error("not operator requires an operand");
|
|
409
|
+
}
|
|
410
|
+
return {
|
|
411
|
+
NOT: buildPrismaFilterFromCerbosExpression(operand, mapper),
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
case "eq":
|
|
416
|
+
case "ne":
|
|
417
|
+
case "lt":
|
|
418
|
+
case "le":
|
|
419
|
+
case "gt":
|
|
420
|
+
case "ge": {
|
|
421
|
+
return handleRelationalOperator(operator, operands, mapper);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
case "in": {
|
|
425
|
+
return handleInOperator(operands, mapper);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
case "contains":
|
|
429
|
+
case "startsWith":
|
|
430
|
+
case "endsWith": {
|
|
431
|
+
return handleStringOperator(operator, operands, mapper);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
case "isSet": {
|
|
435
|
+
return handleIsSetOperator(operands, mapper);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
case "hasIntersection": {
|
|
439
|
+
return handleHasIntersectionOperator(operands, mapper);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
case "lambda": {
|
|
443
|
+
return handleLambdaOperator(operands);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
case "exists":
|
|
447
|
+
case "exists_one":
|
|
448
|
+
case "all":
|
|
449
|
+
case "except":
|
|
450
|
+
case "filter": {
|
|
451
|
+
return handleCollectionOperator(operator, operands, mapper);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
case "map": {
|
|
455
|
+
return handleMapOperator(operands, mapper);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
default:
|
|
459
|
+
throw new Error(`Unsupported operator: ${operator}`);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Helper function to process relational operators (eq, ne, lt, etc.)
|
|
465
|
+
*/
|
|
466
|
+
function handleRelationalOperator(
|
|
467
|
+
operator: string,
|
|
468
|
+
operands: PlanExpressionOperand[],
|
|
469
|
+
mapper: Mapper
|
|
470
|
+
): PrismaFilter {
|
|
471
|
+
const prismaOperator = {
|
|
472
|
+
eq: "equals",
|
|
473
|
+
ne: "not",
|
|
474
|
+
lt: "lt",
|
|
475
|
+
le: "lte",
|
|
476
|
+
gt: "gt",
|
|
477
|
+
ge: "gte",
|
|
478
|
+
}[operator];
|
|
479
|
+
|
|
480
|
+
if (!prismaOperator) {
|
|
481
|
+
throw new Error(`Unsupported operator: ${operator}`);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
const leftOperand = operands.find(
|
|
485
|
+
(o) => isNamedOperand(o) || isOperatorOperand(o)
|
|
486
|
+
);
|
|
487
|
+
if (!leftOperand) throw new Error("No valid left operand found");
|
|
488
|
+
|
|
489
|
+
const rightOperand = operands.find((o) => o !== leftOperand);
|
|
490
|
+
if (!rightOperand) throw new Error("No valid right operand found");
|
|
491
|
+
|
|
492
|
+
const left = resolveOperand(leftOperand, mapper);
|
|
493
|
+
const right = resolveOperand(rightOperand, mapper);
|
|
494
|
+
|
|
495
|
+
if (isResolvedFieldReference(left)) {
|
|
496
|
+
const { path, relations } = left;
|
|
497
|
+
|
|
498
|
+
if (!isResolvedValue(right)) {
|
|
499
|
+
throw new Error("Right operand must be a value");
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
const filterValue = { [prismaOperator]: right.value };
|
|
503
|
+
const fieldName = getLeafField(path);
|
|
504
|
+
const fieldFilter = { [fieldName]: filterValue };
|
|
505
|
+
|
|
506
|
+
if (relations && relations.length > 0) {
|
|
507
|
+
return buildNestedRelationFilter(relations, fieldFilter);
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
return fieldFilter;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
if (!isResolvedValue(right)) {
|
|
514
|
+
throw new Error("Right operand must be a value");
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
return { [prismaOperator]: right.value };
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* Helper function to handle "in" operator
|
|
522
|
+
*/
|
|
523
|
+
function handleInOperator(
|
|
524
|
+
operands: PlanExpressionOperand[],
|
|
525
|
+
mapper: Mapper
|
|
526
|
+
): PrismaFilter {
|
|
527
|
+
const nameOperand = operands.find(isNamedOperand);
|
|
528
|
+
if (!nameOperand) throw new Error("Name operand is undefined");
|
|
529
|
+
|
|
530
|
+
const valueOperand = operands.find(isValueOperand);
|
|
531
|
+
if (!valueOperand) throw new Error("Value operand is undefined");
|
|
532
|
+
|
|
533
|
+
const resolved = resolveOperand(nameOperand, mapper);
|
|
534
|
+
if (!isResolvedFieldReference(resolved)) {
|
|
535
|
+
throw new Error("Name operand must resolve to a field reference");
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
const { path, relations } = resolved;
|
|
539
|
+
const resolvedValue = resolveOperand(valueOperand, mapper);
|
|
540
|
+
|
|
541
|
+
if (!isResolvedValue(resolvedValue)) {
|
|
542
|
+
throw new Error("Value operand must resolve to a value");
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
const { value } = resolvedValue;
|
|
546
|
+
const values = Array.isArray(value) ? value : [value];
|
|
547
|
+
const fieldName = getLeafField(path);
|
|
548
|
+
|
|
549
|
+
if (relations && relations.length > 0) {
|
|
550
|
+
const fieldFilter =
|
|
551
|
+
values.length === 1
|
|
552
|
+
? { [fieldName]: values[0] }
|
|
553
|
+
: { [fieldName]: { in: values } };
|
|
554
|
+
return buildNestedRelationFilter(relations, fieldFilter);
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
return values.length === 1
|
|
558
|
+
? { [fieldName]: values[0] }
|
|
559
|
+
: { [fieldName]: { in: values } };
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* Helper function to handle string operators (contains, startsWith, endsWith)
|
|
564
|
+
*/
|
|
565
|
+
function handleStringOperator(
|
|
566
|
+
operator: string,
|
|
567
|
+
operands: PlanExpressionOperand[],
|
|
568
|
+
mapper: Mapper
|
|
569
|
+
): PrismaFilter {
|
|
570
|
+
const nameOperand = operands.find(isNamedOperand);
|
|
571
|
+
if (!nameOperand) throw new Error("Name operand is undefined");
|
|
572
|
+
|
|
573
|
+
const resolved = resolveOperand(nameOperand, mapper);
|
|
574
|
+
if (!isResolvedFieldReference(resolved)) {
|
|
575
|
+
throw new Error("Name operand must resolve to a field reference");
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
const { path, relations } = resolved;
|
|
579
|
+
|
|
580
|
+
const valueOperand = operands.find(isValueOperand);
|
|
581
|
+
if (!valueOperand) throw new Error("Value operand is undefined");
|
|
582
|
+
|
|
583
|
+
const resolvedValue = resolveOperand(valueOperand, mapper);
|
|
584
|
+
if (!isResolvedValue(resolvedValue)) {
|
|
585
|
+
throw new Error("Value operand must resolve to a value");
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
const { value } = resolvedValue;
|
|
589
|
+
if (typeof value !== "string") {
|
|
590
|
+
throw new Error(`${operator} operator requires string value`);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
const fieldName = getLeafField(path);
|
|
594
|
+
const fieldFilter = { [fieldName]: { [operator]: value } };
|
|
595
|
+
|
|
596
|
+
if (relations && relations.length > 0) {
|
|
597
|
+
return buildNestedRelationFilter(relations, fieldFilter);
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
return fieldFilter;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* Helper function to handle "isSet" operator
|
|
605
|
+
*/
|
|
606
|
+
function handleIsSetOperator(
|
|
607
|
+
operands: PlanExpressionOperand[],
|
|
608
|
+
mapper: Mapper
|
|
609
|
+
): PrismaFilter {
|
|
610
|
+
const nameOperand = operands.find(isNamedOperand);
|
|
611
|
+
if (!nameOperand) throw new Error("Name operand is undefined");
|
|
612
|
+
|
|
613
|
+
const resolved = resolveOperand(nameOperand, mapper);
|
|
614
|
+
if (!isResolvedFieldReference(resolved)) {
|
|
615
|
+
throw new Error("Name operand must resolve to a field reference");
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
const { path, relations } = resolved;
|
|
619
|
+
|
|
620
|
+
const valueOperand = operands.find(isValueOperand);
|
|
621
|
+
if (!valueOperand) throw new Error("Value operand is undefined");
|
|
622
|
+
|
|
623
|
+
const resolvedValue = resolveOperand(valueOperand, mapper);
|
|
624
|
+
if (!isResolvedValue(resolvedValue)) {
|
|
625
|
+
throw new Error("Value operand must resolve to a value");
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
const fieldName = getLeafField(path);
|
|
629
|
+
const fieldFilter = {
|
|
630
|
+
[fieldName]: resolvedValue.value ? { not: null } : { equals: null },
|
|
631
|
+
};
|
|
632
|
+
|
|
633
|
+
if (relations && relations.length > 0) {
|
|
634
|
+
return buildNestedRelationFilter(relations, fieldFilter);
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
return fieldFilter;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/**
|
|
641
|
+
* Helper function to handle "hasIntersection" operator
|
|
642
|
+
*/
|
|
643
|
+
function handleHasIntersectionOperator(
|
|
644
|
+
operands: PlanExpressionOperand[],
|
|
645
|
+
mapper: Mapper
|
|
646
|
+
): PrismaFilter {
|
|
647
|
+
if (operands.length !== 2) {
|
|
648
|
+
throw new Error("hasIntersection requires exactly two operands");
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
const leftOperand = assertDefined(
|
|
652
|
+
operands[0],
|
|
653
|
+
"hasIntersection requires a left operand"
|
|
654
|
+
);
|
|
655
|
+
const rightOperand = assertDefined(
|
|
656
|
+
operands[1],
|
|
657
|
+
"hasIntersection requires a right operand"
|
|
658
|
+
);
|
|
659
|
+
|
|
660
|
+
// Check if left operand is a map operation
|
|
661
|
+
if (isOperatorOperand(leftOperand) && leftOperand.operator === "map") {
|
|
662
|
+
if (!isValueOperand(rightOperand)) {
|
|
663
|
+
throw new Error("Second operand of hasIntersection must be a value");
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
const collection = assertDefined(
|
|
667
|
+
leftOperand.operands[0],
|
|
668
|
+
"Map expression must include a collection reference"
|
|
669
|
+
);
|
|
670
|
+
const lambda = assertDefined(
|
|
671
|
+
leftOperand.operands[1],
|
|
672
|
+
"Map expression must include a lambda expression"
|
|
673
|
+
);
|
|
674
|
+
|
|
675
|
+
if (!isNamedOperand(collection)) {
|
|
676
|
+
throw new Error("First operand of map must be a collection reference");
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
// Get variable name from lambda
|
|
680
|
+
if (!isOperatorOperand(lambda)) {
|
|
681
|
+
throw new Error("Lambda expression must have operands");
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
const variable = assertDefined(
|
|
685
|
+
lambda.operands[1],
|
|
686
|
+
"Lambda variable must have a name"
|
|
687
|
+
);
|
|
688
|
+
if (!isNamedOperand(variable)) {
|
|
689
|
+
throw new Error("Lambda variable must have a name");
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
// Create scoped mapper for the collection
|
|
693
|
+
const scopedMapper = createScopedMapper(
|
|
694
|
+
collection.name,
|
|
695
|
+
variable.name,
|
|
696
|
+
mapper
|
|
697
|
+
);
|
|
698
|
+
|
|
699
|
+
const { relations } = resolveFieldReference(collection.name, mapper);
|
|
700
|
+
if (!relations || relations.length === 0) {
|
|
701
|
+
throw new Error("Map operation requires relations");
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
const projection = assertDefined(
|
|
705
|
+
lambda.operands[0],
|
|
706
|
+
"Invalid map lambda expression structure"
|
|
707
|
+
);
|
|
708
|
+
if (!isNamedOperand(projection)) {
|
|
709
|
+
throw new Error("Invalid map lambda expression structure");
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
// Use scoped mapper for resolving the projection
|
|
713
|
+
const resolved = resolveFieldReference(projection.name, scopedMapper);
|
|
714
|
+
const fieldName = getLeafField(resolved.path);
|
|
715
|
+
|
|
716
|
+
return buildNestedRelationFilter(relations, {
|
|
717
|
+
[fieldName]: { in: rightOperand.value },
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
// Handle regular field reference
|
|
722
|
+
if (!isNamedOperand(leftOperand)) {
|
|
723
|
+
throw new Error(
|
|
724
|
+
"First operand of hasIntersection must be a field reference or map expression"
|
|
725
|
+
);
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
if (!isValueOperand(rightOperand)) {
|
|
729
|
+
throw new Error("Second operand of hasIntersection must be a value");
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
const { path, relations } = resolveFieldReference(leftOperand.name, mapper);
|
|
733
|
+
|
|
734
|
+
if (!Array.isArray(rightOperand.value)) {
|
|
735
|
+
throw new Error("hasIntersection requires an array value");
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
if (relations && relations.length > 0) {
|
|
739
|
+
const fieldName = getLeafField(path);
|
|
740
|
+
const fieldFilter = {
|
|
741
|
+
[fieldName]: { in: rightOperand.value },
|
|
742
|
+
};
|
|
743
|
+
return buildNestedRelationFilter(relations, fieldFilter);
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
const fieldName = getLeafField(path);
|
|
747
|
+
return { [fieldName]: { some: rightOperand.value } };
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
/**
|
|
751
|
+
* Helper function to handle "lambda" operator
|
|
752
|
+
*/
|
|
753
|
+
function handleLambdaOperator(operands: PlanExpressionOperand[]): PrismaFilter {
|
|
754
|
+
const condition = assertDefined(
|
|
755
|
+
operands[0],
|
|
756
|
+
"Lambda requires a condition operand"
|
|
757
|
+
);
|
|
758
|
+
const variable = assertDefined(
|
|
759
|
+
operands[1],
|
|
760
|
+
"Lambda requires a variable operand"
|
|
761
|
+
);
|
|
762
|
+
|
|
763
|
+
if (!isNamedOperand(variable)) {
|
|
764
|
+
throw new Error("Lambda variable must have a name");
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
return buildPrismaFilterFromCerbosExpression(condition, (key: string) => ({
|
|
768
|
+
field: key.replace(`${variable.name}.`, ""),
|
|
769
|
+
}));
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
/**
|
|
773
|
+
* Helper function to handle collection operators (exists, all, except, filter)
|
|
774
|
+
*/
|
|
775
|
+
function handleCollectionOperator(
|
|
776
|
+
operator: string,
|
|
777
|
+
operands: PlanExpressionOperand[],
|
|
778
|
+
mapper: Mapper
|
|
779
|
+
): PrismaFilter {
|
|
780
|
+
if (operands.length !== 2) {
|
|
781
|
+
throw new Error(`${operator} requires exactly two operands`);
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
const collection = assertDefined(
|
|
785
|
+
operands[0],
|
|
786
|
+
`${operator} requires a collection operand`
|
|
787
|
+
);
|
|
788
|
+
const lambda = assertDefined(
|
|
789
|
+
operands[1],
|
|
790
|
+
`${operator} requires a lambda operand`
|
|
791
|
+
);
|
|
792
|
+
|
|
793
|
+
if (!isNamedOperand(collection)) {
|
|
794
|
+
throw new Error(
|
|
795
|
+
`First operand of ${operator} must be a collection reference`
|
|
796
|
+
);
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
if (!isOperatorOperand(lambda)) {
|
|
800
|
+
throw new Error(
|
|
801
|
+
`Second operand of ${operator} must be a lambda expression`
|
|
802
|
+
);
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
// Get variable name from lambda
|
|
806
|
+
const variable = assertDefined(
|
|
807
|
+
lambda.operands[1],
|
|
808
|
+
"Lambda variable must have a name"
|
|
809
|
+
);
|
|
810
|
+
if (!isNamedOperand(variable)) {
|
|
811
|
+
throw new Error("Lambda variable must have a name");
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
// Create scoped mapper for the collection
|
|
815
|
+
const scopedMapper = createScopedMapper(
|
|
816
|
+
collection.name,
|
|
817
|
+
variable.name,
|
|
818
|
+
mapper
|
|
819
|
+
);
|
|
820
|
+
|
|
821
|
+
const { relations } = resolveFieldReference(collection.name, mapper);
|
|
822
|
+
if (!relations || relations.length === 0) {
|
|
823
|
+
throw new Error(`${operator} operator requires a relation mapping`);
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
const lambdaConditionOperand = assertDefined(
|
|
827
|
+
lambda.operands[0],
|
|
828
|
+
"Lambda expression must provide a condition"
|
|
829
|
+
);
|
|
830
|
+
const lambdaCondition = buildPrismaFilterFromCerbosExpression(
|
|
831
|
+
lambdaConditionOperand, // Use the condition part of the lambda
|
|
832
|
+
scopedMapper
|
|
833
|
+
);
|
|
834
|
+
|
|
835
|
+
const relation = assertDefined(
|
|
836
|
+
relations[0],
|
|
837
|
+
`${operator} operator requires a relation mapping`
|
|
838
|
+
);
|
|
839
|
+
let filterValue = lambdaCondition;
|
|
840
|
+
|
|
841
|
+
// If the lambda condition already has a relation structure, merge it
|
|
842
|
+
if (lambdaCondition["AND"] || lambdaCondition["OR"]) {
|
|
843
|
+
filterValue = lambdaCondition;
|
|
844
|
+
} else {
|
|
845
|
+
const lambdaKeys = Object.keys(lambdaCondition);
|
|
846
|
+
const defaultKey = lambdaKeys[0];
|
|
847
|
+
if (!defaultKey) {
|
|
848
|
+
throw new Error("Lambda condition must have at least one field");
|
|
849
|
+
}
|
|
850
|
+
const lambdaFieldValue = lambdaCondition[defaultKey];
|
|
851
|
+
if (lambdaFieldValue === undefined) {
|
|
852
|
+
throw new Error("Lambda condition field value cannot be undefined");
|
|
853
|
+
}
|
|
854
|
+
const filterField = relation.field || defaultKey;
|
|
855
|
+
filterValue = {
|
|
856
|
+
[filterField]: lambdaFieldValue,
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
switch (operator) {
|
|
861
|
+
case "exists":
|
|
862
|
+
case "filter":
|
|
863
|
+
return { [relation.name]: { some: filterValue } };
|
|
864
|
+
case "except":
|
|
865
|
+
return { [relation.name]: { some: { NOT: filterValue } } };
|
|
866
|
+
case "exists_one":
|
|
867
|
+
return {
|
|
868
|
+
[relation.name]: {
|
|
869
|
+
some: filterValue,
|
|
870
|
+
},
|
|
871
|
+
AND: [
|
|
872
|
+
{
|
|
873
|
+
[relation.name]: {
|
|
874
|
+
every: {
|
|
875
|
+
OR: [filterValue, { NOT: filterValue }],
|
|
876
|
+
},
|
|
877
|
+
},
|
|
878
|
+
},
|
|
879
|
+
],
|
|
880
|
+
};
|
|
881
|
+
case "all":
|
|
882
|
+
return { [relation.name]: { every: filterValue } };
|
|
883
|
+
default:
|
|
884
|
+
throw new Error(`Unexpected operator: ${operator}`);
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
/**
|
|
889
|
+
* Helper function to handle "map" operator
|
|
890
|
+
*/
|
|
891
|
+
function handleMapOperator(
|
|
892
|
+
operands: PlanExpressionOperand[],
|
|
893
|
+
mapper: Mapper
|
|
894
|
+
): PrismaFilter {
|
|
895
|
+
if (operands.length !== 2) {
|
|
896
|
+
throw new Error("map requires exactly two operands");
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
const collection = assertDefined(
|
|
900
|
+
operands[0],
|
|
901
|
+
"map requires a collection operand"
|
|
902
|
+
);
|
|
903
|
+
const lambda = assertDefined(
|
|
904
|
+
operands[1],
|
|
905
|
+
"map requires a lambda operand"
|
|
906
|
+
);
|
|
907
|
+
|
|
908
|
+
if (!isNamedOperand(collection)) {
|
|
909
|
+
throw new Error("First operand of map must be a collection reference");
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
if (!isOperatorOperand(lambda) || lambda.operator !== "lambda") {
|
|
913
|
+
throw new Error("Second operand of map must be a lambda expression");
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
// Get variable name from lambda
|
|
917
|
+
const projection = assertDefined(
|
|
918
|
+
lambda.operands[0],
|
|
919
|
+
"Map lambda expression must provide a projection"
|
|
920
|
+
);
|
|
921
|
+
const variable = assertDefined(
|
|
922
|
+
lambda.operands[1],
|
|
923
|
+
"Map lambda expression must provide a variable"
|
|
924
|
+
);
|
|
925
|
+
if (!isNamedOperand(projection) || !isNamedOperand(variable)) {
|
|
926
|
+
throw new Error("Invalid map lambda expression structure");
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
// Create scoped mapper for the collection
|
|
930
|
+
const scopedMapper = createScopedMapper(
|
|
931
|
+
collection.name,
|
|
932
|
+
variable.name,
|
|
933
|
+
mapper
|
|
934
|
+
);
|
|
935
|
+
|
|
936
|
+
const { relations } = resolveFieldReference(collection.name, mapper);
|
|
937
|
+
if (!relations || relations.length === 0) {
|
|
938
|
+
throw new Error("map operator requires a relation mapping");
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
// Use scoped mapper for resolving the projection
|
|
942
|
+
const resolved = resolveFieldReference(projection.name, scopedMapper);
|
|
943
|
+
const fieldName = getLeafField(resolved.path);
|
|
944
|
+
const lastRelation = assertDefined(
|
|
945
|
+
relations[relations.length - 1],
|
|
946
|
+
"Relation mapping must contain at least one relation"
|
|
947
|
+
);
|
|
948
|
+
|
|
949
|
+
return buildNestedRelationFilter(relations, {
|
|
950
|
+
[getPrismaRelationOperator(lastRelation)]: {
|
|
951
|
+
select: { [fieldName]: true },
|
|
952
|
+
},
|
|
953
|
+
});
|
|
954
|
+
}
|