@cerebruminc/yates 3.8.1-beta.dangerous.df7fdbd → 3.8.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 +70 -313
- package/dist/expressions.d.ts +5 -4
- package/dist/expressions.js +438 -0
- package/dist/expressions.js.map +1 -1
- package/dist/index.d.ts +51 -8
- package/dist/index.js +789 -1280
- package/dist/index.js.map +1 -1
- package/package.json +2 -9
package/dist/expressions.js
CHANGED
|
@@ -1,3 +1,441 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var __values = (this && this.__values) || function(o) {
|
|
50
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
51
|
+
if (m) return m.call(o);
|
|
52
|
+
if (o && typeof o.length === "number") return {
|
|
53
|
+
next: function () {
|
|
54
|
+
if (o && i >= o.length) o = void 0;
|
|
55
|
+
return { value: o && o[i++], done: !o };
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
59
|
+
};
|
|
60
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
61
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
62
|
+
};
|
|
2
63
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
64
|
+
exports.expressionToSQL = void 0;
|
|
65
|
+
var client_1 = require("@prisma/client");
|
|
66
|
+
var debug_1 = __importDefault(require("debug"));
|
|
67
|
+
var matches_1 = __importDefault(require("lodash/matches"));
|
|
68
|
+
var random_1 = __importDefault(require("lodash/random"));
|
|
69
|
+
var node_sql_parser_1 = require("node-sql-parser");
|
|
70
|
+
var ast_fragments_1 = require("./ast-fragments");
|
|
71
|
+
var escape_1 = require("./escape");
|
|
72
|
+
var debug = (0, debug_1.default)("yates");
|
|
73
|
+
var PRISMA_NUMERIC_TYPES = ["Int", "BigInt", "Float", "Decimal"];
|
|
74
|
+
// This function is a recursive function that will search through an object and
|
|
75
|
+
// its children to find a matching object.
|
|
76
|
+
// It's used to find a matching AST fragment so that we can replace it with a token
|
|
77
|
+
// TODO future cleanup
|
|
78
|
+
var deepFind = function (obj, subObj) {
|
|
79
|
+
var matcher = (0, matches_1.default)(subObj);
|
|
80
|
+
for (var key in obj) {
|
|
81
|
+
if (matcher(obj[key])) {
|
|
82
|
+
return obj[key];
|
|
83
|
+
}
|
|
84
|
+
if (typeof obj[key] === "object") {
|
|
85
|
+
var result = deepFind(obj[key], subObj);
|
|
86
|
+
if (result) {
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
// TODO fix this
|
|
93
|
+
var expressionRowName = function (col) { return "___yates_row_".concat(col); };
|
|
94
|
+
var expressionContext = function (context) { return "___yates_context_".concat(context); };
|
|
95
|
+
// Generate a big 32bit signed integer to use as an ID
|
|
96
|
+
var getLargeRandomInt = function () { return (0, random_1.default)(1000000000, 2147483647); };
|
|
97
|
+
var getDmmfMetaData = function (client, model, field) {
|
|
98
|
+
var runtimeDataModel = client
|
|
99
|
+
._runtimeDataModel;
|
|
100
|
+
var modelData = runtimeDataModel.models[model];
|
|
101
|
+
if (!modelData) {
|
|
102
|
+
throw new Error("Could not retrieve model data from Prisma Client for model '".concat(model, "'"));
|
|
103
|
+
}
|
|
104
|
+
var fieldData = modelData.fields.find(function (f) { return f.name === field; });
|
|
105
|
+
if (!fieldData) {
|
|
106
|
+
throw new Error("Could not retrieve field data from Prisma Client for field '".concat(model, ".").concat(field, "'"));
|
|
107
|
+
}
|
|
108
|
+
return fieldData;
|
|
109
|
+
};
|
|
110
|
+
// Perform substitution of Ints so that Prisma doesn't throw an error due to mismatched type values
|
|
111
|
+
// After we've captured the SQL, we can replace the Ints with the original values
|
|
112
|
+
// The returned tokens are a map of the token int, and the AST fragment that will replace it.
|
|
113
|
+
// We can then reconstruct the query using the AST fragments.
|
|
114
|
+
var tokenizeWhereExpression = function (
|
|
115
|
+
/** The Prisma client to use for metadata */
|
|
116
|
+
client,
|
|
117
|
+
/** The Prisma where expression to be tokenized */
|
|
118
|
+
where,
|
|
119
|
+
/** The base table we are generating an expression for */
|
|
120
|
+
table,
|
|
121
|
+
/** The model name being queried. e.g. 'User' */
|
|
122
|
+
model,
|
|
123
|
+
/** The tokens object to add the new tokens to */
|
|
124
|
+
tokens) {
|
|
125
|
+
var e_1, _a;
|
|
126
|
+
if (tokens === void 0) { tokens = {}; }
|
|
127
|
+
for (var field in where) {
|
|
128
|
+
// Get field data from the prisma client for the model and field being queried
|
|
129
|
+
var fieldData = getDmmfMetaData(client, model, field);
|
|
130
|
+
var int = void 0;
|
|
131
|
+
// Small loop to make sure we get a unique int for the token key
|
|
132
|
+
do {
|
|
133
|
+
int = getLargeRandomInt();
|
|
134
|
+
} while (tokens[int]);
|
|
135
|
+
var astFragment = {};
|
|
136
|
+
var value = where[field];
|
|
137
|
+
// Check if the field is an object, if so, we need to recurse
|
|
138
|
+
// This is a fairly simple approach but covers most cases like "some", "every", "none" etc.
|
|
139
|
+
if (fieldData.kind === "object") {
|
|
140
|
+
// List queries will always have a sub-object of "every", "some" or "none", so we need to dropdown and iterate through them
|
|
141
|
+
if (fieldData.isList) {
|
|
142
|
+
for (var subField in value) {
|
|
143
|
+
var subValue = value[subField];
|
|
144
|
+
var _b = tokenizeWhereExpression(client, subValue, table, fieldData.type, tokens), subTokens_1 = _b.tokens, subWhere_1 = _b.where;
|
|
145
|
+
// biome-ignore lint/style/noParameterAssign: TODO fix this
|
|
146
|
+
tokens = __assign(__assign({}, tokens), subTokens_1);
|
|
147
|
+
where[field][subField] = subWhere_1;
|
|
148
|
+
}
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
var _c = tokenizeWhereExpression(client, value, table, fieldData.type, tokens), subTokens = _c.tokens, subWhere = _c.where;
|
|
152
|
+
// biome-ignore lint/style/noParameterAssign: TODO fix this
|
|
153
|
+
tokens = __assign(__assign({}, tokens), subTokens);
|
|
154
|
+
where[field] = subWhere;
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
var isNumeric = PRISMA_NUMERIC_TYPES.includes(fieldData.type);
|
|
158
|
+
var isColumnName = typeof value === "string" && !!value.match(/^___yates_row_/);
|
|
159
|
+
var isContext = typeof value === "string" && !!value.match(/^___yates_context_/);
|
|
160
|
+
var isInStatement = !!value.in;
|
|
161
|
+
switch (true) {
|
|
162
|
+
case isColumnName:
|
|
163
|
+
// Substiture the yates row placeholder for the actual column name
|
|
164
|
+
// biome-ignore lint/correctness/noSwitchDeclarations: TODO fix this
|
|
165
|
+
var column = value.replace(/^___yates_row_/, "");
|
|
166
|
+
if (!getDmmfMetaData(client, table, column)) {
|
|
167
|
+
throw new Error("Invalid field name \"".concat(column, "\""));
|
|
168
|
+
}
|
|
169
|
+
astFragment = {
|
|
170
|
+
type: "column_ref",
|
|
171
|
+
schema: "public",
|
|
172
|
+
table: table,
|
|
173
|
+
column: column,
|
|
174
|
+
};
|
|
175
|
+
break;
|
|
176
|
+
case isContext && isNumeric:
|
|
177
|
+
astFragment = {
|
|
178
|
+
as: null,
|
|
179
|
+
type: "cast",
|
|
180
|
+
expr: {
|
|
181
|
+
type: "function",
|
|
182
|
+
name: "current_setting",
|
|
183
|
+
args: {
|
|
184
|
+
type: "expr_list",
|
|
185
|
+
value: [
|
|
186
|
+
{
|
|
187
|
+
type: "parameter",
|
|
188
|
+
value: (0, escape_1.escapeLiteral)(value.replace(/^___yates_context_/, "")),
|
|
189
|
+
},
|
|
190
|
+
],
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
symbol: "::",
|
|
194
|
+
target: {
|
|
195
|
+
dataType: "float",
|
|
196
|
+
suffix: [],
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
break;
|
|
200
|
+
case isContext && !isNumeric:
|
|
201
|
+
astFragment = {
|
|
202
|
+
type: "function",
|
|
203
|
+
name: "current_setting",
|
|
204
|
+
args: {
|
|
205
|
+
type: "expr_list",
|
|
206
|
+
value: [
|
|
207
|
+
{
|
|
208
|
+
type: "parameter",
|
|
209
|
+
value: (0, escape_1.escapeLiteral)(value.replace(/^___yates_context_/, "")),
|
|
210
|
+
},
|
|
211
|
+
],
|
|
212
|
+
},
|
|
213
|
+
};
|
|
214
|
+
break;
|
|
215
|
+
case isNumeric:
|
|
216
|
+
if (typeof value !== "number") {
|
|
217
|
+
throw new Error("Numeric fields can only be queried with numbers: querying field '".concat(field, "' with value '").concat(value, "'"));
|
|
218
|
+
}
|
|
219
|
+
astFragment = {
|
|
220
|
+
type: "number",
|
|
221
|
+
value: value,
|
|
222
|
+
};
|
|
223
|
+
break;
|
|
224
|
+
case isInStatement:
|
|
225
|
+
// This is a bit hokey, but we are going to assume that each value here is static, and
|
|
226
|
+
// perform tokenization on each value in the `in` array.
|
|
227
|
+
// The ideal solution is to rework this tokenization function so that it recurses until it
|
|
228
|
+
// finds a scalar value, and then tokenizes that value, with checking for row/context values.
|
|
229
|
+
if (Array.isArray(value.in)) {
|
|
230
|
+
var tokenList = [];
|
|
231
|
+
try {
|
|
232
|
+
for (var _d = (e_1 = void 0, __values(value.in)), _e = _d.next(); !_e.done; _e = _d.next()) {
|
|
233
|
+
var item = _e.value;
|
|
234
|
+
// biome-ignore lint/suspicious/noImplicitAnyLet: TODO fix this
|
|
235
|
+
var inToken = void 0;
|
|
236
|
+
do {
|
|
237
|
+
inToken = getLargeRandomInt();
|
|
238
|
+
} while (tokens[int]);
|
|
239
|
+
tokens[inToken] = {
|
|
240
|
+
astFragment: {
|
|
241
|
+
type: "parameter",
|
|
242
|
+
value: (0, escape_1.escapeLiteral)(item),
|
|
243
|
+
},
|
|
244
|
+
};
|
|
245
|
+
tokenList.push(isNumeric ? inToken : "".concat(inToken));
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
249
|
+
finally {
|
|
250
|
+
try {
|
|
251
|
+
if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
|
|
252
|
+
}
|
|
253
|
+
finally { if (e_1) throw e_1.error; }
|
|
254
|
+
}
|
|
255
|
+
where[field] = {
|
|
256
|
+
in: tokenList,
|
|
257
|
+
};
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
// If the value of `in` is a context value, we assume that it is an array that has been JSON encoded
|
|
261
|
+
// We create an AST fragment representing a function call to `jsonb_array_elements_text` with the context value as the argument
|
|
262
|
+
astFragment = (0, ast_fragments_1.jsonb_array_elements_text)(value.in);
|
|
263
|
+
break;
|
|
264
|
+
// All other types are treated as strings
|
|
265
|
+
default:
|
|
266
|
+
astFragment = {
|
|
267
|
+
type: "parameter",
|
|
268
|
+
value: (0, escape_1.escapeLiteral)(value),
|
|
269
|
+
};
|
|
270
|
+
break;
|
|
271
|
+
}
|
|
272
|
+
tokens[int] = {
|
|
273
|
+
astFragment: astFragment,
|
|
274
|
+
};
|
|
275
|
+
where[field] = isNumeric ? int : "".concat(int);
|
|
276
|
+
}
|
|
277
|
+
return {
|
|
278
|
+
tokens: tokens,
|
|
279
|
+
where: where,
|
|
280
|
+
};
|
|
281
|
+
};
|
|
282
|
+
var expressionToSQL = function (getExpression, table) { return __awaiter(void 0, void 0, void 0, function () {
|
|
283
|
+
var baseClient, tokens, expressionClient, sql;
|
|
284
|
+
return __generator(this, function (_a) {
|
|
285
|
+
switch (_a.label) {
|
|
286
|
+
case 0:
|
|
287
|
+
if (typeof getExpression === "string") {
|
|
288
|
+
return [2 /*return*/, getExpression];
|
|
289
|
+
}
|
|
290
|
+
debug("Creating RLS expression from", getExpression.toString());
|
|
291
|
+
baseClient = new client_1.PrismaClient({
|
|
292
|
+
log: [{ level: "query", emit: "event" }],
|
|
293
|
+
});
|
|
294
|
+
tokens = {};
|
|
295
|
+
expressionClient = baseClient.$extends({
|
|
296
|
+
name: "expressionClient",
|
|
297
|
+
query: {
|
|
298
|
+
$allModels: {
|
|
299
|
+
$allOperations: function (_a) {
|
|
300
|
+
var model = _a.model, operation = _a.operation, args = _a.args, query = _a.query;
|
|
301
|
+
// if not findFirst or findUnique
|
|
302
|
+
if (operation !== "findFirst" && operation !== "findUnique") {
|
|
303
|
+
throw new Error('Only "findFirst" and "findUnique" are supported in client expressions');
|
|
304
|
+
}
|
|
305
|
+
if ("where" in args && args.where) {
|
|
306
|
+
var where = tokenizeWhereExpression(baseClient, args.where, table, model, tokens).where;
|
|
307
|
+
args.where = where;
|
|
308
|
+
}
|
|
309
|
+
return query(args);
|
|
310
|
+
},
|
|
311
|
+
},
|
|
312
|
+
},
|
|
313
|
+
});
|
|
314
|
+
return [4 /*yield*/, new Promise(
|
|
315
|
+
// biome-ignore lint/suspicious/noAsyncPromiseExecutor: future cleanup
|
|
316
|
+
function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () {
|
|
317
|
+
var rawExpression, isSubselect, error_1;
|
|
318
|
+
return __generator(this, function (_a) {
|
|
319
|
+
switch (_a.label) {
|
|
320
|
+
case 0:
|
|
321
|
+
rawExpression = getExpression(expressionClient, expressionRowName, expressionContext);
|
|
322
|
+
isSubselect = typeof rawExpression === "object" &&
|
|
323
|
+
"then" in rawExpression &&
|
|
324
|
+
typeof rawExpression.then === "function";
|
|
325
|
+
baseClient.$on("query", function (e) {
|
|
326
|
+
var e_2, _a, e_3, _b;
|
|
327
|
+
try {
|
|
328
|
+
var parser = new node_sql_parser_1.Parser();
|
|
329
|
+
// Parse the query into an AST
|
|
330
|
+
var ast = parser.astify(e.query, {
|
|
331
|
+
database: "postgresql",
|
|
332
|
+
});
|
|
333
|
+
var params = JSON.parse(e.params);
|
|
334
|
+
// By default Prisma will use a parameter for the limit, for Yates, the value is always "1"
|
|
335
|
+
ast.limit = { seperator: "", value: [{ type: "number", value: 1 }] };
|
|
336
|
+
// Now that the SQL has been generated, we can replace the tokens with the original values
|
|
337
|
+
for (var i = 0; i < params.length; i++) {
|
|
338
|
+
var param = params[i];
|
|
339
|
+
var token = tokens[param];
|
|
340
|
+
// If there is no token, we can skip this. The most likely cause of this is that the parameter is for a limit or offset, which we cull from the SQL anyway
|
|
341
|
+
if (!token) {
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
var parameterizedStatement = deepFind(ast, {
|
|
345
|
+
type: "var",
|
|
346
|
+
name: i + 1,
|
|
347
|
+
prefix: "$",
|
|
348
|
+
});
|
|
349
|
+
// If we found a matching parameterized statement, we can replace it with the AST fragment.
|
|
350
|
+
// This will replace the parameter with the original value.
|
|
351
|
+
// We do this by mutating the object returned from the deepfind function.
|
|
352
|
+
if (parameterizedStatement) {
|
|
353
|
+
try {
|
|
354
|
+
// First, scrub all the keys from the parameterized statement
|
|
355
|
+
for (var _c = (e_2 = void 0, __values(Object.keys(parameterizedStatement))), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
356
|
+
var key = _d.value;
|
|
357
|
+
Reflect.deleteProperty(parameterizedStatement, key);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
361
|
+
finally {
|
|
362
|
+
try {
|
|
363
|
+
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
|
364
|
+
}
|
|
365
|
+
finally { if (e_2) throw e_2.error; }
|
|
366
|
+
}
|
|
367
|
+
try {
|
|
368
|
+
// Second, add all the keys from the AST fragment to the parameterized statement
|
|
369
|
+
for (var _e = (e_3 = void 0, __values(Object.keys(token.astFragment))), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
370
|
+
var key = _f.value;
|
|
371
|
+
parameterizedStatement[key] = token.astFragment[key];
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
375
|
+
finally {
|
|
376
|
+
try {
|
|
377
|
+
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
|
|
378
|
+
}
|
|
379
|
+
finally { if (e_3) throw e_3.error; }
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
if (isSubselect) {
|
|
384
|
+
// For subselects, we need to convert the entire query and wrap in EXISTS so it converts to a binary expression
|
|
385
|
+
var subSelect = parser.sqlify(ast, {
|
|
386
|
+
database: "postgresql",
|
|
387
|
+
});
|
|
388
|
+
resolve("EXISTS(".concat(subSelect, ")"));
|
|
389
|
+
}
|
|
390
|
+
else {
|
|
391
|
+
// For basic expressions, we're only interested in the WHERE clause and can convert just the WHERE clause into SQL
|
|
392
|
+
var where = parser.exprToSQL(ast.where, {
|
|
393
|
+
database: "postgresql",
|
|
394
|
+
});
|
|
395
|
+
resolve(where);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
catch (error) {
|
|
399
|
+
reject(error);
|
|
400
|
+
}
|
|
401
|
+
});
|
|
402
|
+
_a.label = 1;
|
|
403
|
+
case 1:
|
|
404
|
+
_a.trys.push([1, 6, , 7]);
|
|
405
|
+
if (!isSubselect) return [3 /*break*/, 3];
|
|
406
|
+
return [4 /*yield*/, rawExpression];
|
|
407
|
+
case 2:
|
|
408
|
+
_a.sent();
|
|
409
|
+
return [3 /*break*/, 5];
|
|
410
|
+
case 3: return [4 /*yield*/, expressionClient[table].findFirst({
|
|
411
|
+
where: rawExpression,
|
|
412
|
+
})];
|
|
413
|
+
case 4:
|
|
414
|
+
_a.sent();
|
|
415
|
+
_a.label = 5;
|
|
416
|
+
case 5: return [3 /*break*/, 7];
|
|
417
|
+
case 6:
|
|
418
|
+
error_1 = _a.sent();
|
|
419
|
+
reject(error_1);
|
|
420
|
+
return [3 /*break*/, 7];
|
|
421
|
+
case 7: return [2 /*return*/];
|
|
422
|
+
}
|
|
423
|
+
});
|
|
424
|
+
}); })];
|
|
425
|
+
case 1:
|
|
426
|
+
sql = _a.sent();
|
|
427
|
+
// Close the client
|
|
428
|
+
return [4 /*yield*/, expressionClient.$disconnect()];
|
|
429
|
+
case 2:
|
|
430
|
+
// Close the client
|
|
431
|
+
_a.sent();
|
|
432
|
+
return [4 /*yield*/, baseClient.$disconnect()];
|
|
433
|
+
case 3:
|
|
434
|
+
_a.sent();
|
|
435
|
+
debug("Generated RLS expression", sql);
|
|
436
|
+
return [2 /*return*/, sql];
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
}); };
|
|
440
|
+
exports.expressionToSQL = expressionToSQL;
|
|
3
441
|
//# sourceMappingURL=expressions.js.map
|
package/dist/expressions.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"expressions.js","sourceRoot":"","sources":["../src/expressions.ts"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"expressions.js","sourceRoot":"","sources":["../src/expressions.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA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AO,CAAC,KAAK,CAAC,CAAC;4CAChB,CAAC;wCACF,CAAC;wCAAC,OAAO,KAAK,EAAE,CAAC;4CAChB,MAAM,CAAC,KAAK,CAAC,CAAC;wCACf,CAAC;oCACF,CAAC,CAAC,CAAC;;;;yCAIE,WAAW,EAAX,wBAAW;oCACd,qBAAM,aAAa,EAAA;;oCAAnB,SAAmB,CAAC;;wCAEpB,qBAAO,gBAAwB,CAAC,KAAK,CAAC,CAAC,SAAS,CAAC;wCAChD,KAAK,EAAE,aAAa;qCACpB,CAAC,EAAA;;oCAFF,SAEE,CAAC;;;;;oCAGJ,MAAM,CAAC,OAAK,CAAC,CAAC;;;;;yBAEf,CACD,EAAA;;gBA3FK,GAAG,GAAG,SA2FX;gBAED,mBAAmB;gBACnB,qBAAM,gBAAgB,CAAC,WAAW,EAAE,EAAA;;gBADpC,mBAAmB;gBACnB,SAAoC,CAAC;gBACrC,qBAAM,UAAU,CAAC,WAAW,EAAE,EAAA;;gBAA9B,SAA8B,CAAC;gBAE/B,KAAK,CAAC,0BAA0B,EAAE,GAAG,CAAC,CAAC;gBAEvC,sBAAO,GAAG,EAAC;;;KACX,CAAC;AA1JW,QAAA,eAAe,mBA0J1B"}
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,12 @@ import { Expression, RuntimeDataModel } from "./expressions";
|
|
|
3
3
|
declare const VALID_OPERATIONS: readonly ["SELECT", "UPDATE", "INSERT", "DELETE"];
|
|
4
4
|
type Operation = (typeof VALID_OPERATIONS)[number];
|
|
5
5
|
export type Models = Prisma.ModelName;
|
|
6
|
+
interface ClientOptions {
|
|
7
|
+
/** The maximum amount of time Yates will wait to acquire a transaction from the database. The default value is 30 seconds. */
|
|
8
|
+
txMaxWait?: number;
|
|
9
|
+
/** The maximum amount of time the Yates query transaction can run before being canceled and rolled back. The default value is 30 seconds. */
|
|
10
|
+
txTimeout?: number;
|
|
11
|
+
}
|
|
6
12
|
export interface Ability<ContextKeys extends string, M extends Models> {
|
|
7
13
|
description?: string;
|
|
8
14
|
expression?: Expression<ContextKeys, M>;
|
|
@@ -26,10 +32,16 @@ export type CustomAbilities<ContextKeys extends string = string, YModels extends
|
|
|
26
32
|
};
|
|
27
33
|
export type GetContextFn<ContextKeys extends string = string> = () => {
|
|
28
34
|
role: string;
|
|
35
|
+
transactionId?: string;
|
|
29
36
|
context?: {
|
|
30
37
|
[key in ContextKeys]: string | number | string[];
|
|
31
38
|
};
|
|
32
39
|
} | null;
|
|
40
|
+
declare module "@prisma/client" {
|
|
41
|
+
interface PrismaClient {
|
|
42
|
+
_executeRequest: (params: any) => Promise<any>;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
33
45
|
export interface SetupParams<ContextKeys extends string = string, YModels extends Models = Models, K extends CustomAbilities<ContextKeys, YModels> = CustomAbilities<ContextKeys, YModels>> {
|
|
34
46
|
/**
|
|
35
47
|
* The Prisma client instance. Used for database queries and model introspection.
|
|
@@ -49,26 +61,57 @@ export interface SetupParams<ContextKeys extends string = string, YModels extend
|
|
|
49
61
|
/**
|
|
50
62
|
* A function that returns the context for the current request.
|
|
51
63
|
* This is called on every prisma query, and is needed to determine the current user's role.
|
|
64
|
+
* You can also provide additional context here, which will be available in any RLS expressions you've defined.
|
|
52
65
|
* Returning `null` will result in the permissions being skipped entirely.
|
|
53
66
|
*/
|
|
54
67
|
getContext: GetContextFn<ContextKeys>;
|
|
68
|
+
options?: ClientOptions;
|
|
55
69
|
}
|
|
56
70
|
export declare const sanitizeSlug: (slug: string) => string;
|
|
57
71
|
export declare class Yates {
|
|
58
72
|
private prisma;
|
|
73
|
+
private databaseScope;
|
|
59
74
|
constructor(prisma: PrismaClient);
|
|
60
|
-
|
|
75
|
+
init: () => Promise<void>;
|
|
76
|
+
createDatabaseScope: (databaseName: string) => string;
|
|
77
|
+
getDatabaseScope: () => string;
|
|
78
|
+
ensureDatabaseScope: () => Promise<string>;
|
|
79
|
+
setupAbilityTable: () => Promise<[number, number, number]>;
|
|
80
|
+
createAbilityName: (model: string, ability: string) => string;
|
|
81
|
+
createRoleName: (name: string) => string;
|
|
61
82
|
getDefaultAbilities: (models: Models[]) => Partial<DefaultAbilities<string, Prisma.ModelName>>;
|
|
83
|
+
getBatchId(query: any): string | undefined;
|
|
84
|
+
buildKeysString(obj: object): string;
|
|
85
|
+
createClient: (getContext: GetContextFn, options?: ClientOptions) => import("@prisma/client/runtime/library").DynamicClientExtensionThis<Prisma.TypeMap<import("@prisma/client/runtime/library").InternalArgs & {
|
|
86
|
+
result: {};
|
|
87
|
+
model: {};
|
|
88
|
+
query: {};
|
|
89
|
+
client: {};
|
|
90
|
+
}>, Prisma.TypeMapCb, {
|
|
91
|
+
result: {};
|
|
92
|
+
model: {};
|
|
93
|
+
query: {};
|
|
94
|
+
client: {};
|
|
95
|
+
}>;
|
|
96
|
+
setRLS: <ContextKeys extends string, YModel extends Prisma.ModelName>(prisma: PrismaClient, table: string, roleName: string, slug: string, ability: Ability<ContextKeys, YModel>) => Promise<void>;
|
|
97
|
+
createRoles: <ContextKeys extends string, YModels extends Prisma.ModelName, K extends CustomAbilities<string, Prisma.ModelName> = CustomAbilities<string, Prisma.ModelName>, T = DefaultAbilities<ContextKeys, YModels> & K>({ customAbilities, getRoles, }: {
|
|
98
|
+
customAbilities?: Partial<K> | undefined;
|
|
99
|
+
getRoles: (abilities: T) => {
|
|
100
|
+
[role: string]: "*" | AllAbilities<ContextKeys, YModels>[];
|
|
101
|
+
};
|
|
102
|
+
}) => Promise<void>;
|
|
103
|
+
inspectDBRoles: (role: string) => Promise<{
|
|
104
|
+
tablename: string;
|
|
105
|
+
policyname: string;
|
|
106
|
+
cmd: string;
|
|
107
|
+
policy_roles: string[];
|
|
108
|
+
matched_role: string[];
|
|
109
|
+
}[]>;
|
|
110
|
+
inspectRunTimeDataModel: () => RuntimeDataModel;
|
|
62
111
|
}
|
|
63
112
|
/**
|
|
64
|
-
* Creates an extended client that
|
|
113
|
+
* Creates an extended client that sets contextual parameters and user role on every query
|
|
65
114
|
**/
|
|
66
|
-
export declare const __private: {
|
|
67
|
-
denyWhere: (runtimeDataModel: RuntimeDataModel, model: string) => Record<string, any>;
|
|
68
|
-
getIdField: (runtimeDataModel: RuntimeDataModel, model: string) => string | null;
|
|
69
|
-
matchesScalarFilter: (value: any, filter: any, data: Record<string, any>) => boolean;
|
|
70
|
-
validateContext: (context: Record<string, any> | undefined) => void;
|
|
71
|
-
};
|
|
72
115
|
export declare const setup: <ContextKeys extends string = string, YModels extends Prisma.ModelName = Prisma.ModelName, K extends CustomAbilities<ContextKeys, YModels> = CustomAbilities<ContextKeys, YModels>>(params: SetupParams<ContextKeys, YModels, K>) => Promise<import("@prisma/client/runtime/library").DynamicClientExtensionThis<Prisma.TypeMap<import("@prisma/client/runtime/library").InternalArgs & {
|
|
73
116
|
result: {};
|
|
74
117
|
model: {};
|