@cerbos/orm-prisma 3.0.1 → 3.1.1
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 +2 -2
- package/lib/index.js +54 -1
- package/lib/index.js.map +1 -1
- package/package.json +17 -12
- 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 +262 -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 +702 -2
- package/src/index.ts +79 -1
- package/src/test-setup.v6.ts +5 -0
- package/src/test-setup.v7.ts +7 -0
- package/lib/index.test.d.ts +0 -2
- package/lib/index.test.d.ts.map +0 -1
- package/lib/index.test.js +0 -4093
- package/lib/index.test.js.map +0 -1
package/src/index.ts
CHANGED
|
@@ -379,7 +379,17 @@ function buildPrismaFilterFromCerbosExpression(
|
|
|
379
379
|
expression: PlanExpressionOperand,
|
|
380
380
|
mapper: Mapper
|
|
381
381
|
): PrismaFilter {
|
|
382
|
-
//
|
|
382
|
+
// A bare named operand represents a boolean field reference (e.g. `R.attr.booleanAttr`)
|
|
383
|
+
if (isNamedOperand(expression)) {
|
|
384
|
+
const { path, relations } = resolveFieldReference(expression.name, mapper);
|
|
385
|
+
const fieldName = getLeafField(path);
|
|
386
|
+
const fieldFilter = { [fieldName]: { equals: true } };
|
|
387
|
+
if (relations && relations.length > 0) {
|
|
388
|
+
return buildNestedRelationFilter(relations, fieldFilter);
|
|
389
|
+
}
|
|
390
|
+
return fieldFilter;
|
|
391
|
+
}
|
|
392
|
+
|
|
383
393
|
if (!isOperatorOperand(expression)) {
|
|
384
394
|
throw new Error("Invalid Cerbos expression structure");
|
|
385
395
|
}
|
|
@@ -407,6 +417,16 @@ function buildPrismaFilterFromCerbosExpression(
|
|
|
407
417
|
if (!operand) {
|
|
408
418
|
throw new Error("not operator requires an operand");
|
|
409
419
|
}
|
|
420
|
+
if (isNamedOperand(operand)) {
|
|
421
|
+
const { path, relations } = resolveFieldReference(
|
|
422
|
+
operand.name,
|
|
423
|
+
mapper
|
|
424
|
+
);
|
|
425
|
+
if (!relations || relations.length === 0) {
|
|
426
|
+
const fieldName = getLeafField(path);
|
|
427
|
+
return { [fieldName]: { equals: false } };
|
|
428
|
+
}
|
|
429
|
+
}
|
|
410
430
|
return {
|
|
411
431
|
NOT: buildPrismaFilterFromCerbosExpression(operand, mapper),
|
|
412
432
|
};
|
|
@@ -460,6 +480,60 @@ function buildPrismaFilterFromCerbosExpression(
|
|
|
460
480
|
}
|
|
461
481
|
}
|
|
462
482
|
|
|
483
|
+
function handleSizeComparison(
|
|
484
|
+
operator: string,
|
|
485
|
+
sizeOperand: OperatorOperand,
|
|
486
|
+
valueOperand: PlanExpressionOperand,
|
|
487
|
+
mapper: Mapper
|
|
488
|
+
): PrismaFilter {
|
|
489
|
+
const collectionOperand = sizeOperand.operands[0];
|
|
490
|
+
if (!collectionOperand || !isNamedOperand(collectionOperand)) {
|
|
491
|
+
throw new Error("size operator requires a named collection operand");
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
if (!isValueOperand(valueOperand)) {
|
|
495
|
+
throw new Error("size comparison requires a numeric value operand");
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
const count = valueOperand.value;
|
|
499
|
+
if (typeof count !== "number") {
|
|
500
|
+
throw new Error("size comparison requires a numeric value");
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
const isNonEmpty =
|
|
504
|
+
(operator === "gt" && count === 0) || (operator === "ge" && count === 1);
|
|
505
|
+
|
|
506
|
+
const isEmpty =
|
|
507
|
+
(operator === "eq" && count === 0) ||
|
|
508
|
+
(operator === "lt" && count === 1) ||
|
|
509
|
+
(operator === "le" && count === 0);
|
|
510
|
+
|
|
511
|
+
if (!isNonEmpty && !isEmpty) {
|
|
512
|
+
throw new Error(
|
|
513
|
+
`Unsupported size comparison: size(...) ${operator} ${count}`
|
|
514
|
+
);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
const { relations } = resolveFieldReference(collectionOperand.name, mapper);
|
|
518
|
+
if (!relations || relations.length === 0) {
|
|
519
|
+
throw new Error("size operator requires a relation mapping");
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
const deepest = relations[relations.length - 1];
|
|
523
|
+
if (!deepest) {
|
|
524
|
+
throw new Error("size operator requires a relation mapping");
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
const prismaOp = isNonEmpty ? "some" : "none";
|
|
528
|
+
const leafFilter = { [deepest.name]: { [prismaOp]: {} } };
|
|
529
|
+
|
|
530
|
+
if (relations.length === 1) {
|
|
531
|
+
return leafFilter;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
return buildNestedRelationFilter(relations.slice(0, -1), leafFilter);
|
|
535
|
+
}
|
|
536
|
+
|
|
463
537
|
/**
|
|
464
538
|
* Helper function to process relational operators (eq, ne, lt, etc.)
|
|
465
539
|
*/
|
|
@@ -489,6 +563,10 @@ function handleRelationalOperator(
|
|
|
489
563
|
const rightOperand = operands.find((o) => o !== leftOperand);
|
|
490
564
|
if (!rightOperand) throw new Error("No valid right operand found");
|
|
491
565
|
|
|
566
|
+
if (isOperatorOperand(leftOperand) && leftOperand.operator === "size") {
|
|
567
|
+
return handleSizeComparison(operator, leftOperand, rightOperand, mapper);
|
|
568
|
+
}
|
|
569
|
+
|
|
492
570
|
const left = resolveOperand(leftOperand, mapper);
|
|
493
571
|
const right = resolveOperand(rightOperand, mapper);
|
|
494
572
|
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { PrismaBetterSqlite3 } from "@prisma/adapter-better-sqlite3";
|
|
2
|
+
import { Prisma, PrismaClient } from "./generated/prisma/client";
|
|
3
|
+
|
|
4
|
+
const adapter = new PrismaBetterSqlite3({ url: "./prisma/dev.db" });
|
|
5
|
+
const prisma = new PrismaClient({ adapter });
|
|
6
|
+
|
|
7
|
+
export { prisma, Prisma };
|
package/lib/index.test.d.ts
DELETED
package/lib/index.test.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":""}
|