@cerebruminc/yates 3.3.0 → 3.3.1-beta.dangerous.402403c
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/CHANGELOG.md +14 -0
- package/dist/ast-fragments.d.ts +68 -0
- package/dist/ast-fragments.js +84 -0
- package/dist/ast-fragments.js.map +1 -0
- package/dist/escape.d.ts +2 -0
- package/dist/escape.js +36 -0
- package/dist/escape.js.map +1 -0
- package/dist/expressions.d.ts +11 -0
- package/dist/expressions.js +431 -0
- package/dist/expressions.js.map +1 -0
- package/dist/index.d.ts +103 -0
- package/dist/index.js +557 -0
- package/dist/index.js.map +1 -0
- package/package.json +9 -3
- package/.prettierrc +0 -8
- package/eslint.config.ts +0 -4
- package/renovate.json +0 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [3.3.2](https://github.com/cerebruminc/yates/compare/v3.3.1...v3.3.2) (2024-02-28)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* do not include test files in published build output ([8bc00dc](https://github.com/cerebruminc/yates/commit/8bc00dc6028df539b8170724757623a8359bd51d))
|
|
9
|
+
|
|
10
|
+
## [3.3.1](https://github.com/cerebruminc/yates/compare/v3.3.0...v3.3.1) (2024-02-28)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Bug Fixes
|
|
14
|
+
|
|
15
|
+
* make sure the dist directory is published to npm ([043cba5](https://github.com/cerebruminc/yates/commit/043cba57f9698fb91868dc439de2eb4e54d613b6))
|
|
16
|
+
|
|
3
17
|
## [3.3.0](https://github.com/cerebruminc/yates/compare/v3.2.0...v3.3.0) (2024-02-26)
|
|
4
18
|
|
|
5
19
|
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates an AST fragment that will check if a column value exists in a JSONB array stored in a `current_setting`
|
|
3
|
+
* The AST fragment represents SQL that looks like this:
|
|
4
|
+
* = ANY (SELECT jsonb_array_elements_text(current_setting('ctx.my_context_value')::jsonb))
|
|
5
|
+
*/
|
|
6
|
+
export declare const jsonb_array_elements_text: (setting: string) => {
|
|
7
|
+
type: string;
|
|
8
|
+
name: string;
|
|
9
|
+
args: {
|
|
10
|
+
type: string;
|
|
11
|
+
value: {
|
|
12
|
+
ast: {
|
|
13
|
+
with: null;
|
|
14
|
+
type: string;
|
|
15
|
+
options: null;
|
|
16
|
+
distinct: {
|
|
17
|
+
type: null;
|
|
18
|
+
};
|
|
19
|
+
columns: {
|
|
20
|
+
type: string;
|
|
21
|
+
expr: {
|
|
22
|
+
type: string;
|
|
23
|
+
name: string;
|
|
24
|
+
args: {
|
|
25
|
+
type: string;
|
|
26
|
+
value: {
|
|
27
|
+
type: string;
|
|
28
|
+
keyword: string;
|
|
29
|
+
expr: {
|
|
30
|
+
type: string;
|
|
31
|
+
name: string;
|
|
32
|
+
args: {
|
|
33
|
+
type: string;
|
|
34
|
+
value: {
|
|
35
|
+
type: string;
|
|
36
|
+
value: string;
|
|
37
|
+
}[];
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
as: null;
|
|
41
|
+
symbol: string;
|
|
42
|
+
target: {
|
|
43
|
+
dataType: string;
|
|
44
|
+
};
|
|
45
|
+
arrows: never[];
|
|
46
|
+
properties: never[];
|
|
47
|
+
}[];
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
as: null;
|
|
51
|
+
}[];
|
|
52
|
+
into: {
|
|
53
|
+
position: null;
|
|
54
|
+
};
|
|
55
|
+
from: null;
|
|
56
|
+
where: null;
|
|
57
|
+
groupby: null;
|
|
58
|
+
having: null;
|
|
59
|
+
orderby: null;
|
|
60
|
+
limit: {
|
|
61
|
+
seperator: string;
|
|
62
|
+
value: never[];
|
|
63
|
+
};
|
|
64
|
+
window: null;
|
|
65
|
+
};
|
|
66
|
+
}[];
|
|
67
|
+
};
|
|
68
|
+
};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.jsonb_array_elements_text = void 0;
|
|
4
|
+
var escape_1 = require("./escape");
|
|
5
|
+
/**
|
|
6
|
+
* Generates an AST fragment that will check if a column value exists in a JSONB array stored in a `current_setting`
|
|
7
|
+
* The AST fragment represents SQL that looks like this:
|
|
8
|
+
* = ANY (SELECT jsonb_array_elements_text(current_setting('ctx.my_context_value')::jsonb))
|
|
9
|
+
*/
|
|
10
|
+
var jsonb_array_elements_text = function (setting) {
|
|
11
|
+
return {
|
|
12
|
+
type: "function",
|
|
13
|
+
name: "ANY",
|
|
14
|
+
args: {
|
|
15
|
+
type: "expr_list",
|
|
16
|
+
value: [
|
|
17
|
+
{
|
|
18
|
+
ast: {
|
|
19
|
+
with: null,
|
|
20
|
+
type: "select",
|
|
21
|
+
options: null,
|
|
22
|
+
distinct: {
|
|
23
|
+
type: null,
|
|
24
|
+
},
|
|
25
|
+
columns: [
|
|
26
|
+
{
|
|
27
|
+
type: "expr",
|
|
28
|
+
expr: {
|
|
29
|
+
type: "function",
|
|
30
|
+
name: "jsonb_array_elements_text",
|
|
31
|
+
args: {
|
|
32
|
+
type: "expr_list",
|
|
33
|
+
value: [
|
|
34
|
+
{
|
|
35
|
+
type: "cast",
|
|
36
|
+
keyword: "cast",
|
|
37
|
+
expr: {
|
|
38
|
+
type: "function",
|
|
39
|
+
name: "current_setting",
|
|
40
|
+
args: {
|
|
41
|
+
type: "expr_list",
|
|
42
|
+
value: [
|
|
43
|
+
{
|
|
44
|
+
type: "parameter",
|
|
45
|
+
value: (0, escape_1.escapeLiteral)(setting.replace(/^___yates_context_/, "")),
|
|
46
|
+
},
|
|
47
|
+
],
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
as: null,
|
|
51
|
+
symbol: "::",
|
|
52
|
+
target: {
|
|
53
|
+
dataType: "jsonb",
|
|
54
|
+
},
|
|
55
|
+
arrows: [],
|
|
56
|
+
properties: [],
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
as: null,
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
into: {
|
|
65
|
+
position: null,
|
|
66
|
+
},
|
|
67
|
+
from: null,
|
|
68
|
+
where: null,
|
|
69
|
+
groupby: null,
|
|
70
|
+
having: null,
|
|
71
|
+
orderby: null,
|
|
72
|
+
limit: {
|
|
73
|
+
seperator: "",
|
|
74
|
+
value: [],
|
|
75
|
+
},
|
|
76
|
+
window: null,
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
exports.jsonb_array_elements_text = jsonb_array_elements_text;
|
|
84
|
+
//# sourceMappingURL=ast-fragments.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ast-fragments.js","sourceRoot":"","sources":["../src/ast-fragments.ts"],"names":[],"mappings":";;;AAAA,mCAAyC;AAEzC;;;;GAIG;AACI,IAAM,yBAAyB,GAAG,UAAC,OAAe;IACxD,OAAO;QACN,IAAI,EAAE,UAAU;QAChB,IAAI,EAAE,KAAK;QACX,IAAI,EAAE;YACL,IAAI,EAAE,WAAW;YACjB,KAAK,EAAE;gBACN;oBACC,GAAG,EAAE;wBACJ,IAAI,EAAE,IAAI;wBACV,IAAI,EAAE,QAAQ;wBACd,OAAO,EAAE,IAAI;wBACb,QAAQ,EAAE;4BACT,IAAI,EAAE,IAAI;yBACV;wBACD,OAAO,EAAE;4BACR;gCACC,IAAI,EAAE,MAAM;gCACZ,IAAI,EAAE;oCACL,IAAI,EAAE,UAAU;oCAChB,IAAI,EAAE,2BAA2B;oCACjC,IAAI,EAAE;wCACL,IAAI,EAAE,WAAW;wCACjB,KAAK,EAAE;4CACN;gDACC,IAAI,EAAE,MAAM;gDACZ,OAAO,EAAE,MAAM;gDACf,IAAI,EAAE;oDACL,IAAI,EAAE,UAAU;oDAChB,IAAI,EAAE,iBAAiB;oDACvB,IAAI,EAAE;wDACL,IAAI,EAAE,WAAW;wDACjB,KAAK,EAAE;4DACN;gEACC,IAAI,EAAE,WAAW;gEACjB,KAAK,EAAE,IAAA,sBAAa,EAAC,OAAO,CAAC,OAAO,CAAC,oBAAoB,EAAE,EAAE,CAAC,CAAC;6DAC/D;yDACD;qDACD;iDACD;gDACD,EAAE,EAAE,IAAI;gDACR,MAAM,EAAE,IAAI;gDACZ,MAAM,EAAE;oDACP,QAAQ,EAAE,OAAO;iDACjB;gDACD,MAAM,EAAE,EAAE;gDACV,UAAU,EAAE,EAAE;6CACd;yCACD;qCACD;iCACD;gCACD,EAAE,EAAE,IAAI;6BACR;yBACD;wBACD,IAAI,EAAE;4BACL,QAAQ,EAAE,IAAI;yBACd;wBACD,IAAI,EAAE,IAAI;wBACV,KAAK,EAAE,IAAI;wBACX,OAAO,EAAE,IAAI;wBACb,MAAM,EAAE,IAAI;wBACZ,OAAO,EAAE,IAAI;wBACb,KAAK,EAAE;4BACN,SAAS,EAAE,EAAE;4BACb,KAAK,EAAE,EAAE;yBACT;wBACD,MAAM,EAAE,IAAI;qBACZ;iBACD;aACD;SACD;KACD,CAAC;AACH,CAAC,CAAC;AAxEW,QAAA,yBAAyB,6BAwEpC"}
|
package/dist/escape.d.ts
ADDED
package/dist/escape.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Source borrowed from node-postgrs (which borrows from PG itself)
|
|
3
|
+
// https://github.com/brianc/node-postgres/blob/3f6760c62ee2a901d374b5e50c2f025b7d550315/packages/pg/lib/client.js#L408-L437
|
|
4
|
+
// We need to manually escape strings because we're interpolating client values into SQL statements that don't support `PREPARE`, such as `CREATE POLICY`
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.escapeLiteral = exports.escapeIdentifier = void 0;
|
|
7
|
+
// Ported from PostgreSQL 9.2.4 source code in src/interfaces/libpq/fe-exec.c
|
|
8
|
+
var escapeIdentifier = function (str) {
|
|
9
|
+
return "\"".concat(str.replace(/"/g, '""'), "\"");
|
|
10
|
+
};
|
|
11
|
+
exports.escapeIdentifier = escapeIdentifier;
|
|
12
|
+
// Ported from PostgreSQL 9.2.4 source code in src/interfaces/libpq/fe-exec.c
|
|
13
|
+
var escapeLiteral = function (str) {
|
|
14
|
+
var hasBackslash = false;
|
|
15
|
+
var escaped = "'";
|
|
16
|
+
for (var i = 0; i < str.length; i++) {
|
|
17
|
+
var c = str[i];
|
|
18
|
+
if (c === "'") {
|
|
19
|
+
escaped += c + c;
|
|
20
|
+
}
|
|
21
|
+
else if (c === "\\") {
|
|
22
|
+
escaped += c + c;
|
|
23
|
+
hasBackslash = true;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
escaped += c;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
escaped += "'";
|
|
30
|
+
if (hasBackslash === true) {
|
|
31
|
+
escaped = " E".concat(escaped);
|
|
32
|
+
}
|
|
33
|
+
return escaped;
|
|
34
|
+
};
|
|
35
|
+
exports.escapeLiteral = escapeLiteral;
|
|
36
|
+
//# sourceMappingURL=escape.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"escape.js","sourceRoot":"","sources":["../src/escape.ts"],"names":[],"mappings":";AAAA,mEAAmE;AACnE,4HAA4H;AAC5H,yJAAyJ;;;AAEzJ,6EAA6E;AACtE,IAAM,gBAAgB,GAAG,UAAU,GAAW;IACpD,OAAO,YAAI,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,OAAG,CAAC;AACvC,CAAC,CAAC;AAFW,QAAA,gBAAgB,oBAE3B;AAEF,6EAA6E;AACtE,IAAM,aAAa,GAAG,UAAU,GAAW;IACjD,IAAI,YAAY,GAAG,KAAK,CAAC;IACzB,IAAI,OAAO,GAAG,GAAG,CAAC;IAElB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACrC,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;QACf,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;YACf,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC;QAClB,CAAC;aAAM,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC;YACvB,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC;YACjB,YAAY,GAAG,IAAI,CAAC;QACrB,CAAC;aAAM,CAAC;YACP,OAAO,IAAI,CAAC,CAAC;QACd,CAAC;IACF,CAAC;IAED,OAAO,IAAI,GAAG,CAAC;IAEf,IAAI,YAAY,KAAK,IAAI,EAAE,CAAC;QAC3B,OAAO,GAAG,YAAK,OAAO,CAAE,CAAC;IAC1B,CAAC;IAED,OAAO,OAAO,CAAC;AAChB,CAAC,CAAC;AAvBW,QAAA,aAAa,iBAuBxB"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Prisma, PrismaClient } from "@prisma/client";
|
|
2
|
+
import { AsyncReturnType } from "type-fest";
|
|
3
|
+
import { defineDmmfProperty } from "@prisma/client/runtime/library";
|
|
4
|
+
export type RuntimeDataModel = Parameters<typeof defineDmmfProperty>[1];
|
|
5
|
+
type FFMeta<M extends Prisma.ModelName> = PrismaClient[Uncapitalize<M>]["findFirst"];
|
|
6
|
+
type ModelWhereArgs<M extends Prisma.ModelName> = Exclude<Parameters<FFMeta<M>>["0"], undefined>["where"];
|
|
7
|
+
type ModelResult<M extends Prisma.ModelName> = AsyncReturnType<FFMeta<M>>;
|
|
8
|
+
type NonNullableModelResult<M extends Prisma.ModelName> = Exclude<ModelResult<M>, null>;
|
|
9
|
+
export type Expression<ContextKeys extends string, M extends Prisma.ModelName> = string | ((client: PrismaClient, row: <K extends keyof NonNullableModelResult<M>>(col: K) => NonNullableModelResult<M>[K], context: (key: ContextKeys) => string) => Promise<ModelResult<Exclude<Prisma.ModelName, M>>> | ModelWhereArgs<M>);
|
|
10
|
+
export declare const expressionToSQL: <ContextKeys extends string, YModel extends Prisma.ModelName>(getExpression: Expression<ContextKeys, YModel>, table: string) => Promise<string>;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,431 @@
|
|
|
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
|
+
};
|
|
63
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
64
|
+
exports.expressionToSQL = void 0;
|
|
65
|
+
var client_1 = require("@prisma/client");
|
|
66
|
+
var random_1 = __importDefault(require("lodash/random"));
|
|
67
|
+
var matches_1 = __importDefault(require("lodash/matches"));
|
|
68
|
+
var node_sql_parser_1 = require("node-sql-parser");
|
|
69
|
+
var escape_1 = require("./escape");
|
|
70
|
+
var ast_fragments_1 = require("./ast-fragments");
|
|
71
|
+
var PRISMA_NUMERIC_TYPES = ["Int", "BigInt", "Float", "Decimal"];
|
|
72
|
+
var deepFind = function (obj, subObj) {
|
|
73
|
+
var matcher = (0, matches_1.default)(subObj);
|
|
74
|
+
for (var key in obj) {
|
|
75
|
+
if (matcher(obj[key])) {
|
|
76
|
+
return obj[key];
|
|
77
|
+
}
|
|
78
|
+
else if (typeof obj[key] === "object") {
|
|
79
|
+
var result = deepFind(obj[key], subObj);
|
|
80
|
+
if (result) {
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
var expressionRowName = function (col) { return "___yates_row_".concat(col); };
|
|
87
|
+
var expressionContext = function (context) { return "___yates_context_".concat(context); };
|
|
88
|
+
// Generate a big 32bit signed integer to use as an ID
|
|
89
|
+
var getLargeRandomInt = function () { return (0, random_1.default)(1000000000, 2147483647); };
|
|
90
|
+
var getDmmfMetaData = function (client, model, field) {
|
|
91
|
+
var runtimeDataModel = client._runtimeDataModel;
|
|
92
|
+
var modelData = runtimeDataModel.models[model];
|
|
93
|
+
if (!modelData) {
|
|
94
|
+
throw new Error("Could not retrieve model data from Prisma Client for model '".concat(model, "'"));
|
|
95
|
+
}
|
|
96
|
+
var fieldData = modelData.fields.find(function (f) { return f.name === field; });
|
|
97
|
+
if (!fieldData) {
|
|
98
|
+
throw new Error("Could not retrieve field data from Prisma Client for field '".concat(model, ".").concat(field, "'"));
|
|
99
|
+
}
|
|
100
|
+
return fieldData;
|
|
101
|
+
};
|
|
102
|
+
// Perform substitution of Ints so that Prisma doesn't throw an error due to mismatched type values
|
|
103
|
+
// After we've captured the SQL, we can replace the Ints with the original values
|
|
104
|
+
// The returned tokens are a map of the token int, and the AST fragment that will replace it.
|
|
105
|
+
// We can then reconstruct the query using the AST fragments.
|
|
106
|
+
var tokenizeWhereExpression = function (
|
|
107
|
+
/** The Prisma client to use for metadata */
|
|
108
|
+
client,
|
|
109
|
+
/** The Prisma where expression to be tokenized */
|
|
110
|
+
where,
|
|
111
|
+
/** The base table we are generating an expression for */
|
|
112
|
+
table,
|
|
113
|
+
/** The model name being queried. e.g. 'User' */
|
|
114
|
+
model,
|
|
115
|
+
/** The tokens object to add the new tokens to */
|
|
116
|
+
tokens) {
|
|
117
|
+
var e_1, _a;
|
|
118
|
+
if (tokens === void 0) { tokens = {}; }
|
|
119
|
+
for (var field in where) {
|
|
120
|
+
// Get field data from the prisma client for the model and field being queried
|
|
121
|
+
var fieldData = getDmmfMetaData(client, model, field);
|
|
122
|
+
var int = void 0;
|
|
123
|
+
// Small loop to make sure we get a unique int for the token key
|
|
124
|
+
do {
|
|
125
|
+
int = getLargeRandomInt();
|
|
126
|
+
} while (tokens[int]);
|
|
127
|
+
var astFragment = {};
|
|
128
|
+
var value = where[field];
|
|
129
|
+
// Check if the field is an object, if so, we need to recurse
|
|
130
|
+
// This is a fairly simple approach but covers most cases like "some", "every", "none" etc.
|
|
131
|
+
if (fieldData.kind === "object") {
|
|
132
|
+
// List queries will always have a sub-object of "every", "some" or "none", so we need to dropdown and iterate through them
|
|
133
|
+
if (fieldData.isList) {
|
|
134
|
+
for (var subField in value) {
|
|
135
|
+
var subValue = value[subField];
|
|
136
|
+
var _b = tokenizeWhereExpression(client, subValue, table, fieldData.type, tokens), subTokens = _b.tokens, subWhere = _b.where;
|
|
137
|
+
tokens = __assign(__assign({}, tokens), subTokens);
|
|
138
|
+
where[field][subField] = subWhere;
|
|
139
|
+
}
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
var _c = tokenizeWhereExpression(client, value, table, fieldData.type, tokens), subTokens = _c.tokens, subWhere = _c.where;
|
|
144
|
+
tokens = __assign(__assign({}, tokens), subTokens);
|
|
145
|
+
where[field] = subWhere;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
var isNumeric = PRISMA_NUMERIC_TYPES.includes(fieldData.type);
|
|
150
|
+
var isColumnName = typeof value === "string" && !!value.match(/^___yates_row_/);
|
|
151
|
+
var isContext = typeof value === "string" && !!value.match(/^___yates_context_/);
|
|
152
|
+
var isInStatement = !!value.in;
|
|
153
|
+
switch (true) {
|
|
154
|
+
case isColumnName:
|
|
155
|
+
// Substiture the yates row placeholder for the actual column name
|
|
156
|
+
var column = value.replace(/^___yates_row_/, "");
|
|
157
|
+
if (!getDmmfMetaData(client, table, column)) {
|
|
158
|
+
throw new Error("Invalid field name \"".concat(column, "\""));
|
|
159
|
+
}
|
|
160
|
+
astFragment = {
|
|
161
|
+
type: "column_ref",
|
|
162
|
+
schema: "public",
|
|
163
|
+
table: table,
|
|
164
|
+
column: column,
|
|
165
|
+
};
|
|
166
|
+
break;
|
|
167
|
+
case isContext && isNumeric:
|
|
168
|
+
astFragment = {
|
|
169
|
+
as: null,
|
|
170
|
+
type: "cast",
|
|
171
|
+
expr: {
|
|
172
|
+
type: "function",
|
|
173
|
+
name: "current_setting",
|
|
174
|
+
args: {
|
|
175
|
+
type: "expr_list",
|
|
176
|
+
value: [
|
|
177
|
+
{
|
|
178
|
+
type: "parameter",
|
|
179
|
+
value: (0, escape_1.escapeLiteral)(value.replace(/^___yates_context_/, "")),
|
|
180
|
+
},
|
|
181
|
+
],
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
symbol: "::",
|
|
185
|
+
target: {
|
|
186
|
+
dataType: "float",
|
|
187
|
+
suffix: [],
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
break;
|
|
191
|
+
case isContext && !isNumeric:
|
|
192
|
+
astFragment = {
|
|
193
|
+
type: "function",
|
|
194
|
+
name: "current_setting",
|
|
195
|
+
args: {
|
|
196
|
+
type: "expr_list",
|
|
197
|
+
value: [
|
|
198
|
+
{
|
|
199
|
+
type: "parameter",
|
|
200
|
+
value: (0, escape_1.escapeLiteral)(value.replace(/^___yates_context_/, "")),
|
|
201
|
+
},
|
|
202
|
+
],
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
break;
|
|
206
|
+
case isNumeric:
|
|
207
|
+
if (typeof value !== "number") {
|
|
208
|
+
throw new Error("Numeric fields can only be queried with numbers: querying field '".concat(field, "' with value '").concat(value, "'"));
|
|
209
|
+
}
|
|
210
|
+
astFragment = {
|
|
211
|
+
type: "number",
|
|
212
|
+
value: value,
|
|
213
|
+
};
|
|
214
|
+
break;
|
|
215
|
+
case isInStatement:
|
|
216
|
+
// This is a bit hokey, but we are going to assume that each value here is static, and
|
|
217
|
+
// perform tokenization on each value in the `in` array.
|
|
218
|
+
// The ideal solution is to rework this tokenization function so that it recurses until it
|
|
219
|
+
// finds a scalar value, and then tokenizes that value, with checking for row/context values.
|
|
220
|
+
if (Array.isArray(value.in)) {
|
|
221
|
+
var tokenList = [];
|
|
222
|
+
try {
|
|
223
|
+
for (var _d = (e_1 = void 0, __values(value.in)), _e = _d.next(); !_e.done; _e = _d.next()) {
|
|
224
|
+
var item = _e.value;
|
|
225
|
+
var inToken = void 0;
|
|
226
|
+
do {
|
|
227
|
+
inToken = getLargeRandomInt();
|
|
228
|
+
} while (tokens[int]);
|
|
229
|
+
tokens[inToken] = {
|
|
230
|
+
astFragment: {
|
|
231
|
+
type: "parameter",
|
|
232
|
+
value: (0, escape_1.escapeLiteral)(item),
|
|
233
|
+
},
|
|
234
|
+
};
|
|
235
|
+
tokenList.push(isNumeric ? inToken : "".concat(inToken));
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
239
|
+
finally {
|
|
240
|
+
try {
|
|
241
|
+
if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
|
|
242
|
+
}
|
|
243
|
+
finally { if (e_1) throw e_1.error; }
|
|
244
|
+
}
|
|
245
|
+
where[field] = {
|
|
246
|
+
in: tokenList,
|
|
247
|
+
};
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
// If the value of `in` is a context value, we assume that it is an array that has been JSON encoded
|
|
252
|
+
// We create an AST fragment representing a function call to `jsonb_array_elements_text` with the context value as the argument
|
|
253
|
+
astFragment = (0, ast_fragments_1.jsonb_array_elements_text)(value.in);
|
|
254
|
+
}
|
|
255
|
+
break;
|
|
256
|
+
// All other types are treated as strings
|
|
257
|
+
default:
|
|
258
|
+
astFragment = {
|
|
259
|
+
type: "parameter",
|
|
260
|
+
value: (0, escape_1.escapeLiteral)(value),
|
|
261
|
+
};
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
tokens[int] = {
|
|
265
|
+
astFragment: astFragment,
|
|
266
|
+
};
|
|
267
|
+
where[field] = isNumeric ? int : "".concat(int);
|
|
268
|
+
}
|
|
269
|
+
return {
|
|
270
|
+
tokens: tokens,
|
|
271
|
+
where: where,
|
|
272
|
+
};
|
|
273
|
+
};
|
|
274
|
+
var expressionToSQL = function (getExpression, table) { return __awaiter(void 0, void 0, void 0, function () {
|
|
275
|
+
var baseClient, tokens, expressionClient, sql;
|
|
276
|
+
return __generator(this, function (_a) {
|
|
277
|
+
switch (_a.label) {
|
|
278
|
+
case 0:
|
|
279
|
+
if (typeof getExpression === "string") {
|
|
280
|
+
return [2 /*return*/, getExpression];
|
|
281
|
+
}
|
|
282
|
+
baseClient = new client_1.PrismaClient({
|
|
283
|
+
log: [{ level: "query", emit: "event" }],
|
|
284
|
+
});
|
|
285
|
+
tokens = {};
|
|
286
|
+
expressionClient = baseClient.$extends({
|
|
287
|
+
name: "expressionClient",
|
|
288
|
+
query: {
|
|
289
|
+
$allModels: {
|
|
290
|
+
$allOperations: function (_a) {
|
|
291
|
+
var model = _a.model, operation = _a.operation, args = _a.args, query = _a.query;
|
|
292
|
+
// if not findFirst or findUnique
|
|
293
|
+
if (operation !== "findFirst" && operation !== "findUnique") {
|
|
294
|
+
throw new Error('Only "findFirst" and "findUnique" are supported in client expressions');
|
|
295
|
+
}
|
|
296
|
+
if ("where" in args && args.where) {
|
|
297
|
+
var where = tokenizeWhereExpression(baseClient, args.where, table, model, tokens).where;
|
|
298
|
+
args.where = where;
|
|
299
|
+
}
|
|
300
|
+
return query(args);
|
|
301
|
+
},
|
|
302
|
+
},
|
|
303
|
+
},
|
|
304
|
+
});
|
|
305
|
+
return [4 /*yield*/, new Promise(
|
|
306
|
+
// rome-ignore lint/suspicious/noAsyncPromiseExecutor: future cleanup
|
|
307
|
+
function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () {
|
|
308
|
+
var rawExpression, isSubselect, error_1;
|
|
309
|
+
return __generator(this, function (_a) {
|
|
310
|
+
switch (_a.label) {
|
|
311
|
+
case 0:
|
|
312
|
+
rawExpression = getExpression(expressionClient, expressionRowName, expressionContext);
|
|
313
|
+
isSubselect = typeof rawExpression === "object" &&
|
|
314
|
+
"then" in rawExpression &&
|
|
315
|
+
typeof rawExpression.then === "function";
|
|
316
|
+
baseClient.$on("query", function (e) {
|
|
317
|
+
var e_2, _a, e_3, _b;
|
|
318
|
+
try {
|
|
319
|
+
var parser = new node_sql_parser_1.Parser();
|
|
320
|
+
// Parse the query into an AST
|
|
321
|
+
var ast = parser.astify(e.query, {
|
|
322
|
+
database: "postgresql",
|
|
323
|
+
});
|
|
324
|
+
var params = JSON.parse(e.params);
|
|
325
|
+
// By default Prisma will use a parameter for the limit, for Yates, the value is always "1"
|
|
326
|
+
ast.limit = { seperator: "", value: [{ type: "number", value: 1 }] };
|
|
327
|
+
// Now that the SQL has been generated, we can replace the tokens with the original values
|
|
328
|
+
for (var i = 0; i < params.length; i++) {
|
|
329
|
+
var param = params[i];
|
|
330
|
+
var token = tokens[param];
|
|
331
|
+
// 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
|
|
332
|
+
if (!token) {
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
var parameterizedStatement = deepFind(ast, {
|
|
336
|
+
type: "var",
|
|
337
|
+
name: i + 1,
|
|
338
|
+
prefix: "$",
|
|
339
|
+
});
|
|
340
|
+
// If we found a matching parameterized statement, we can replace it with the AST fragment.
|
|
341
|
+
// This will replace the parameter with the original value.
|
|
342
|
+
// We do this by mutating the object returned from the deepfind function.
|
|
343
|
+
if (parameterizedStatement) {
|
|
344
|
+
try {
|
|
345
|
+
// First, scrub all the keys from the parameterized statement
|
|
346
|
+
for (var _c = (e_2 = void 0, __values(Object.keys(parameterizedStatement))), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
347
|
+
var key = _d.value;
|
|
348
|
+
Reflect.deleteProperty(parameterizedStatement, key);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
352
|
+
finally {
|
|
353
|
+
try {
|
|
354
|
+
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
|
355
|
+
}
|
|
356
|
+
finally { if (e_2) throw e_2.error; }
|
|
357
|
+
}
|
|
358
|
+
try {
|
|
359
|
+
// Second, add all the keys from the AST fragment to the parameterized statement
|
|
360
|
+
for (var _e = (e_3 = void 0, __values(Object.keys(token.astFragment))), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
361
|
+
var key = _f.value;
|
|
362
|
+
parameterizedStatement[key] = token.astFragment[key];
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
366
|
+
finally {
|
|
367
|
+
try {
|
|
368
|
+
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
|
|
369
|
+
}
|
|
370
|
+
finally { if (e_3) throw e_3.error; }
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
if (isSubselect) {
|
|
375
|
+
// For subselects, we need to convert the entire query and wrap in EXISTS so it converts to a binary expression
|
|
376
|
+
var subSelect = parser.sqlify(ast, {
|
|
377
|
+
database: "postgresql",
|
|
378
|
+
});
|
|
379
|
+
resolve("EXISTS(".concat(subSelect, ")"));
|
|
380
|
+
}
|
|
381
|
+
else {
|
|
382
|
+
// For basic expressions, we're only interested in the WHERE clause and can convert just the WHERE clause into SQL
|
|
383
|
+
var where = parser.exprToSQL(ast.where, {
|
|
384
|
+
database: "postgresql",
|
|
385
|
+
});
|
|
386
|
+
resolve(where);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
catch (error) {
|
|
390
|
+
reject(error);
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
_a.label = 1;
|
|
394
|
+
case 1:
|
|
395
|
+
_a.trys.push([1, 6, , 7]);
|
|
396
|
+
if (!isSubselect) return [3 /*break*/, 3];
|
|
397
|
+
return [4 /*yield*/, rawExpression];
|
|
398
|
+
case 2:
|
|
399
|
+
_a.sent();
|
|
400
|
+
return [3 /*break*/, 5];
|
|
401
|
+
case 3: return [4 /*yield*/, expressionClient[table].findFirst({
|
|
402
|
+
where: rawExpression,
|
|
403
|
+
})];
|
|
404
|
+
case 4:
|
|
405
|
+
_a.sent();
|
|
406
|
+
_a.label = 5;
|
|
407
|
+
case 5: return [3 /*break*/, 7];
|
|
408
|
+
case 6:
|
|
409
|
+
error_1 = _a.sent();
|
|
410
|
+
reject(error_1);
|
|
411
|
+
return [3 /*break*/, 7];
|
|
412
|
+
case 7: return [2 /*return*/];
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
}); })];
|
|
416
|
+
case 1:
|
|
417
|
+
sql = _a.sent();
|
|
418
|
+
// Close the client
|
|
419
|
+
return [4 /*yield*/, expressionClient.$disconnect()];
|
|
420
|
+
case 2:
|
|
421
|
+
// Close the client
|
|
422
|
+
_a.sent();
|
|
423
|
+
return [4 /*yield*/, baseClient.$disconnect()];
|
|
424
|
+
case 3:
|
|
425
|
+
_a.sent();
|
|
426
|
+
return [2 /*return*/, sql];
|
|
427
|
+
}
|
|
428
|
+
});
|
|
429
|
+
}); };
|
|
430
|
+
exports.expressionToSQL = expressionToSQL;
|
|
431
|
+
//# sourceMappingURL=expressions.js.map
|