@cerbos/orm-prisma 1.0.0 → 1.1.0-alpha.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/lib/cjs/index.d.ts +1 -1
- package/lib/cjs/index.js +49 -26
- package/lib/index.d.ts +1 -1
- package/lib/index.js +50 -27
- package/package.json +2 -2
package/lib/cjs/index.d.ts
CHANGED
|
@@ -20,5 +20,5 @@ interface QueryPlanToPrismaResult {
|
|
|
20
20
|
kind: PlanKind;
|
|
21
21
|
filters?: any;
|
|
22
22
|
}
|
|
23
|
-
export declare function queryPlanToPrisma({ queryPlan, fieldNameMapper, relationMapper }: QueryPlanToPrismaArgs): QueryPlanToPrismaResult;
|
|
23
|
+
export declare function queryPlanToPrisma({ queryPlan, fieldNameMapper, relationMapper, }: QueryPlanToPrismaArgs): QueryPlanToPrismaResult;
|
|
24
24
|
export {};
|
package/lib/cjs/index.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.queryPlanToPrisma = exports.PlanKind = void 0;
|
|
4
4
|
const core_1 = require("@cerbos/core");
|
|
5
5
|
exports.PlanKind = core_1.PlanKind;
|
|
6
|
-
function queryPlanToPrisma({ queryPlan, fieldNameMapper, relationMapper = {} }) {
|
|
6
|
+
function queryPlanToPrisma({ queryPlan, fieldNameMapper, relationMapper = {}, }) {
|
|
7
7
|
const toFieldName = (key) => {
|
|
8
8
|
if (typeof fieldNameMapper === "function") {
|
|
9
9
|
return fieldNameMapper(key);
|
|
@@ -28,16 +28,16 @@ function queryPlanToPrisma({ queryPlan, fieldNameMapper, relationMapper = {} })
|
|
|
28
28
|
case exports.PlanKind.ALWAYS_ALLOWED:
|
|
29
29
|
return {
|
|
30
30
|
kind: exports.PlanKind.ALWAYS_ALLOWED,
|
|
31
|
-
filters: {}
|
|
31
|
+
filters: {},
|
|
32
32
|
};
|
|
33
33
|
case exports.PlanKind.ALWAYS_DENIED:
|
|
34
34
|
return {
|
|
35
|
-
kind: exports.PlanKind.ALWAYS_DENIED
|
|
35
|
+
kind: exports.PlanKind.ALWAYS_DENIED,
|
|
36
36
|
};
|
|
37
37
|
case exports.PlanKind.CONDITIONAL:
|
|
38
38
|
return {
|
|
39
39
|
kind: exports.PlanKind.CONDITIONAL,
|
|
40
|
-
filters: mapOperand(queryPlan.condition, toFieldName, toRelationName)
|
|
40
|
+
filters: mapOperand(queryPlan.condition, toFieldName, toRelationName),
|
|
41
41
|
};
|
|
42
42
|
default:
|
|
43
43
|
throw Error(`Invalid query plan.`);
|
|
@@ -54,42 +54,42 @@ function isVariable(e) {
|
|
|
54
54
|
return e.variable !== undefined;
|
|
55
55
|
}
|
|
56
56
|
function getOperandVariable(operands) {
|
|
57
|
-
const op = operands.find(o => o.hasOwnProperty("name"));
|
|
57
|
+
const op = operands.find((o) => o.hasOwnProperty("name"));
|
|
58
58
|
if (!op)
|
|
59
59
|
return;
|
|
60
60
|
return op.name;
|
|
61
61
|
}
|
|
62
62
|
function getOperandValue(operands) {
|
|
63
|
-
const op = operands.find(o => isValue(o));
|
|
63
|
+
const op = operands.find((o) => isValue(o));
|
|
64
64
|
if (!op)
|
|
65
65
|
return;
|
|
66
66
|
return op.value;
|
|
67
67
|
}
|
|
68
68
|
const OPERATORS = {
|
|
69
|
-
|
|
69
|
+
eq: {
|
|
70
70
|
relationalCondition: "is",
|
|
71
|
-
fieldCondition: "equals"
|
|
71
|
+
fieldCondition: "equals",
|
|
72
72
|
},
|
|
73
|
-
|
|
73
|
+
ne: {
|
|
74
74
|
relationalCondition: "isNot",
|
|
75
|
-
fieldCondition: "not"
|
|
75
|
+
fieldCondition: "not",
|
|
76
76
|
},
|
|
77
|
-
|
|
77
|
+
in: {
|
|
78
78
|
relationalCondition: "some",
|
|
79
|
-
fieldCondition: "in"
|
|
79
|
+
fieldCondition: "in",
|
|
80
80
|
},
|
|
81
|
-
|
|
82
|
-
fieldCondition: "lt"
|
|
81
|
+
lt: {
|
|
82
|
+
fieldCondition: "lt",
|
|
83
83
|
},
|
|
84
|
-
|
|
85
|
-
fieldCondition: "gt"
|
|
84
|
+
gt: {
|
|
85
|
+
fieldCondition: "gt",
|
|
86
86
|
},
|
|
87
|
-
|
|
88
|
-
fieldCondition: "lte"
|
|
87
|
+
le: {
|
|
88
|
+
fieldCondition: "lte",
|
|
89
|
+
},
|
|
90
|
+
ge: {
|
|
91
|
+
fieldCondition: "gte",
|
|
89
92
|
},
|
|
90
|
-
"ge": {
|
|
91
|
-
fieldCondition: "gte"
|
|
92
|
-
}
|
|
93
93
|
};
|
|
94
94
|
function mapOperand(operand, getFieldName, getRelationName, output = {}) {
|
|
95
95
|
if (!isExpression(operand))
|
|
@@ -128,19 +128,42 @@ function mapOperand(operand, getFieldName, getRelationName, output = {}) {
|
|
|
128
128
|
if (relation && operation.relationalCondition) {
|
|
129
129
|
output[relation.relation] = {
|
|
130
130
|
[operation.relationalCondition]: {
|
|
131
|
-
[relation.field]: opValue
|
|
132
|
-
}
|
|
131
|
+
[relation.field]: opValue,
|
|
132
|
+
},
|
|
133
133
|
};
|
|
134
134
|
return output;
|
|
135
135
|
}
|
|
136
136
|
else if (fieldName && operation.fieldCondition) {
|
|
137
137
|
// There is a field mapper for this variable
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
138
|
+
const fieldPath = fieldName.split(".");
|
|
139
|
+
if (fieldPath.length > 1) {
|
|
140
|
+
output[fieldPath[0]] = convertPathToJSON(fieldPath.splice(1), {
|
|
141
|
+
[operation.fieldCondition]: opValue,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
output[fieldPath[0]] = {
|
|
146
|
+
[operation.fieldCondition]: opValue,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
141
149
|
return output;
|
|
142
150
|
}
|
|
143
151
|
else {
|
|
144
152
|
throw Error("Failed to map");
|
|
145
153
|
}
|
|
146
154
|
}
|
|
155
|
+
function convertPathToJSON(segments, value) {
|
|
156
|
+
const result = {};
|
|
157
|
+
let current = result;
|
|
158
|
+
for (let i = 0; i < segments.length; i++) {
|
|
159
|
+
const segment = segments[i];
|
|
160
|
+
if (i === segments.length - 1) {
|
|
161
|
+
current[segment] = value;
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
current[segment] = {};
|
|
165
|
+
current = current[segment];
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return result;
|
|
169
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -20,5 +20,5 @@ interface QueryPlanToPrismaResult {
|
|
|
20
20
|
kind: PlanKind;
|
|
21
21
|
filters?: any;
|
|
22
22
|
}
|
|
23
|
-
export declare function queryPlanToPrisma({ queryPlan, fieldNameMapper, relationMapper }: QueryPlanToPrismaArgs): QueryPlanToPrismaResult;
|
|
23
|
+
export declare function queryPlanToPrisma({ queryPlan, fieldNameMapper, relationMapper, }: QueryPlanToPrismaArgs): QueryPlanToPrismaResult;
|
|
24
24
|
export {};
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { PlanKind as PK } from "@cerbos/core";
|
|
1
|
+
import { PlanKind as PK, } from "@cerbos/core";
|
|
2
2
|
export const PlanKind = PK;
|
|
3
|
-
export function queryPlanToPrisma({ queryPlan, fieldNameMapper, relationMapper = {} }) {
|
|
3
|
+
export function queryPlanToPrisma({ queryPlan, fieldNameMapper, relationMapper = {}, }) {
|
|
4
4
|
const toFieldName = (key) => {
|
|
5
5
|
if (typeof fieldNameMapper === "function") {
|
|
6
6
|
return fieldNameMapper(key);
|
|
@@ -25,16 +25,16 @@ export function queryPlanToPrisma({ queryPlan, fieldNameMapper, relationMapper =
|
|
|
25
25
|
case PlanKind.ALWAYS_ALLOWED:
|
|
26
26
|
return {
|
|
27
27
|
kind: PlanKind.ALWAYS_ALLOWED,
|
|
28
|
-
filters: {}
|
|
28
|
+
filters: {},
|
|
29
29
|
};
|
|
30
30
|
case PlanKind.ALWAYS_DENIED:
|
|
31
31
|
return {
|
|
32
|
-
kind: PlanKind.ALWAYS_DENIED
|
|
32
|
+
kind: PlanKind.ALWAYS_DENIED,
|
|
33
33
|
};
|
|
34
34
|
case PlanKind.CONDITIONAL:
|
|
35
35
|
return {
|
|
36
36
|
kind: PlanKind.CONDITIONAL,
|
|
37
|
-
filters: mapOperand(queryPlan.condition, toFieldName, toRelationName)
|
|
37
|
+
filters: mapOperand(queryPlan.condition, toFieldName, toRelationName),
|
|
38
38
|
};
|
|
39
39
|
default:
|
|
40
40
|
throw Error(`Invalid query plan.`);
|
|
@@ -50,42 +50,42 @@ function isVariable(e) {
|
|
|
50
50
|
return e.variable !== undefined;
|
|
51
51
|
}
|
|
52
52
|
function getOperandVariable(operands) {
|
|
53
|
-
const op = operands.find(o => o.hasOwnProperty("name"));
|
|
53
|
+
const op = operands.find((o) => o.hasOwnProperty("name"));
|
|
54
54
|
if (!op)
|
|
55
55
|
return;
|
|
56
56
|
return op.name;
|
|
57
57
|
}
|
|
58
58
|
function getOperandValue(operands) {
|
|
59
|
-
const op = operands.find(o => isValue(o));
|
|
59
|
+
const op = operands.find((o) => isValue(o));
|
|
60
60
|
if (!op)
|
|
61
61
|
return;
|
|
62
62
|
return op.value;
|
|
63
63
|
}
|
|
64
64
|
const OPERATORS = {
|
|
65
|
-
|
|
65
|
+
eq: {
|
|
66
66
|
relationalCondition: "is",
|
|
67
|
-
fieldCondition: "equals"
|
|
67
|
+
fieldCondition: "equals",
|
|
68
68
|
},
|
|
69
|
-
|
|
69
|
+
ne: {
|
|
70
70
|
relationalCondition: "isNot",
|
|
71
|
-
fieldCondition: "not"
|
|
71
|
+
fieldCondition: "not",
|
|
72
72
|
},
|
|
73
|
-
|
|
73
|
+
in: {
|
|
74
74
|
relationalCondition: "some",
|
|
75
|
-
fieldCondition: "in"
|
|
75
|
+
fieldCondition: "in",
|
|
76
76
|
},
|
|
77
|
-
|
|
78
|
-
fieldCondition: "lt"
|
|
77
|
+
lt: {
|
|
78
|
+
fieldCondition: "lt",
|
|
79
79
|
},
|
|
80
|
-
|
|
81
|
-
fieldCondition: "gt"
|
|
80
|
+
gt: {
|
|
81
|
+
fieldCondition: "gt",
|
|
82
82
|
},
|
|
83
|
-
|
|
84
|
-
fieldCondition: "lte"
|
|
83
|
+
le: {
|
|
84
|
+
fieldCondition: "lte",
|
|
85
|
+
},
|
|
86
|
+
ge: {
|
|
87
|
+
fieldCondition: "gte",
|
|
85
88
|
},
|
|
86
|
-
"ge": {
|
|
87
|
-
fieldCondition: "gte"
|
|
88
|
-
}
|
|
89
89
|
};
|
|
90
90
|
function mapOperand(operand, getFieldName, getRelationName, output = {}) {
|
|
91
91
|
if (!isExpression(operand))
|
|
@@ -124,19 +124,42 @@ function mapOperand(operand, getFieldName, getRelationName, output = {}) {
|
|
|
124
124
|
if (relation && operation.relationalCondition) {
|
|
125
125
|
output[relation.relation] = {
|
|
126
126
|
[operation.relationalCondition]: {
|
|
127
|
-
[relation.field]: opValue
|
|
128
|
-
}
|
|
127
|
+
[relation.field]: opValue,
|
|
128
|
+
},
|
|
129
129
|
};
|
|
130
130
|
return output;
|
|
131
131
|
}
|
|
132
132
|
else if (fieldName && operation.fieldCondition) {
|
|
133
133
|
// There is a field mapper for this variable
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
134
|
+
const fieldPath = fieldName.split(".");
|
|
135
|
+
if (fieldPath.length > 1) {
|
|
136
|
+
output[fieldPath[0]] = convertPathToJSON(fieldPath.splice(1), {
|
|
137
|
+
[operation.fieldCondition]: opValue,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
output[fieldPath[0]] = {
|
|
142
|
+
[operation.fieldCondition]: opValue,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
137
145
|
return output;
|
|
138
146
|
}
|
|
139
147
|
else {
|
|
140
148
|
throw Error("Failed to map");
|
|
141
149
|
}
|
|
142
150
|
}
|
|
151
|
+
function convertPathToJSON(segments, value) {
|
|
152
|
+
const result = {};
|
|
153
|
+
let current = result;
|
|
154
|
+
for (let i = 0; i < segments.length; i++) {
|
|
155
|
+
const segment = segments[i];
|
|
156
|
+
if (i === segments.length - 1) {
|
|
157
|
+
current[segment] = value;
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
current[segment] = {};
|
|
161
|
+
current = current[segment];
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return result;
|
|
165
|
+
}
|
package/package.json
CHANGED