@technicity/data-service-generator 0.22.2 → 0.23.0-next.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/dist/src/generation/generate.d.ts +21 -0
- package/dist/src/generation/generate.js +2349 -0
- package/dist/src/index.d.ts +4 -0
- package/dist/src/index.js +11 -0
- package/dist/src/lib/CustomError.d.ts +3 -0
- package/dist/src/lib/CustomError.js +10 -0
- package/dist/src/lib/capitalizeFirstLetter.d.ts +1 -0
- package/dist/src/lib/capitalizeFirstLetter.js +6 -0
- package/dist/src/lib/getDuplicates.d.ts +1 -0
- package/dist/src/lib/getDuplicates.js +9 -0
- package/dist/src/lib/isNotNullOrUndefined.d.ts +1 -0
- package/dist/src/lib/isNotNullOrUndefined.js +7 -0
- package/dist/src/runtime/Cache.d.ts +28 -0
- package/dist/src/runtime/Cache.js +142 -0
- package/dist/src/runtime/IRuntime.d.ts +209 -0
- package/dist/src/runtime/IRuntime.js +12 -0
- package/dist/src/runtime/RuntimeMySQL.d.ts +26 -0
- package/dist/src/runtime/RuntimeMySQL.js +132 -0
- package/dist/src/runtime/RuntimePostgreSQL.d.ts +30 -0
- package/dist/src/runtime/RuntimePostgreSQL.js +73 -0
- package/dist/src/runtime/RuntimeSQLite.d.ts +42 -0
- package/dist/src/runtime/RuntimeSQLite.js +150 -0
- package/dist/src/runtime/Stats.d.ts +8 -0
- package/dist/src/runtime/Stats.js +31 -0
- package/dist/src/runtime/lib/MySQL.d.ts +13 -0
- package/dist/src/runtime/lib/MySQL.js +116 -0
- package/dist/src/runtime/lib/PostgreSQL.d.ts +14 -0
- package/dist/src/runtime/lib/PostgreSQL.js +110 -0
- package/dist/src/runtime/lib/SDKBadWhereError.d.ts +4 -0
- package/dist/src/runtime/lib/SDKBadWhereError.js +10 -0
- package/dist/src/runtime/lib/SDKNotFoundError.d.ts +4 -0
- package/dist/src/runtime/lib/SDKNotFoundError.js +10 -0
- package/dist/src/runtime/lib/addNullFallbacks.d.ts +1 -0
- package/dist/src/runtime/lib/addNullFallbacks.js +32 -0
- package/dist/src/runtime/lib/addNullFallbacks.test.d.ts +1 -0
- package/dist/src/runtime/lib/addNullFallbacks.test.js +206 -0
- package/dist/src/runtime/lib/cursor.d.ts +2 -0
- package/dist/src/runtime/lib/cursor.js +10 -0
- package/dist/src/runtime/lib/getDateTimeStringMySQL.d.ts +1 -0
- package/dist/src/runtime/lib/getDateTimeStringMySQL.js +7 -0
- package/dist/src/runtime/lib/getOrderBy.d.ts +5 -0
- package/dist/src/runtime/lib/getOrderBy.js +52 -0
- package/dist/src/runtime/lib/getSqlAst.d.ts +2 -0
- package/dist/src/runtime/lib/getSqlAst.js +245 -0
- package/dist/src/runtime/lib/getWhere.d.ts +2 -0
- package/dist/src/runtime/lib/getWhere.js +20 -0
- package/dist/src/runtime/lib/shared.d.ts +13 -0
- package/dist/src/runtime/lib/shared.js +1118 -0
- package/dist/src/runtime/lib/stringifyWhere.d.ts +18 -0
- package/dist/src/runtime/lib/stringifyWhere.js +257 -0
- package/dist/src/runtime/lib/stringifyWhere.test.d.ts +1 -0
- package/dist/src/runtime/lib/stringifyWhere.test.js +245 -0
- package/dist/src/runtime/lib/utility.d.ts +5 -0
- package/dist/src/runtime/lib/utility.js +14 -0
- package/dist/src/traverseFieldArgs.d.ts +2 -0
- package/dist/src/traverseFieldArgs.js +17 -0
- package/dist/src/traverseFieldArgs.test.d.ts +1 -0
- package/dist/src/traverseFieldArgs.test.js +56 -0
- package/dist/test/addWhereValidTrue.d.ts +1 -0
- package/dist/test/addWhereValidTrue.js +39 -0
- package/dist/test/globalSetup.d.ts +13 -0
- package/dist/test/globalSetup.js +436 -0
- package/dist/test/postgres/__generated__/sdk-ts/artifacts.d.ts +8425 -0
- package/dist/test/postgres/__generated__/sdk-ts/artifacts.js +10469 -0
- package/dist/test/postgres/__generated__/sdk-ts/index.js +12162 -0
- package/package.json +5 -1
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const globals_1 = require("@jest/globals");
|
|
7
|
+
const strict_1 = __importDefault(require("node:assert/strict"));
|
|
8
|
+
const addNullFallbacks_1 = require("./addNullFallbacks");
|
|
9
|
+
(0, globals_1.describe)("addNullFallbacks", () => {
|
|
10
|
+
// The actual AST has other fields like `args` that
|
|
11
|
+
// aren't relevant to the function
|
|
12
|
+
const sqlAST = {
|
|
13
|
+
type: "table",
|
|
14
|
+
name: "Session",
|
|
15
|
+
as: "Session",
|
|
16
|
+
fieldName: "session",
|
|
17
|
+
grabMany: false,
|
|
18
|
+
children: [
|
|
19
|
+
{
|
|
20
|
+
type: "column",
|
|
21
|
+
name: "id",
|
|
22
|
+
fieldName: "id",
|
|
23
|
+
as: "a"
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
type: "table",
|
|
27
|
+
name: "SessionPlan",
|
|
28
|
+
as: "SessionPlan",
|
|
29
|
+
fieldName: "sessionPlan",
|
|
30
|
+
grabMany: false,
|
|
31
|
+
children: [
|
|
32
|
+
{
|
|
33
|
+
type: "column",
|
|
34
|
+
name: "id",
|
|
35
|
+
fieldName: "id",
|
|
36
|
+
as: "a"
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
type: "column",
|
|
40
|
+
name: "uuid",
|
|
41
|
+
fieldName: "uuid",
|
|
42
|
+
as: "c"
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
type: "column",
|
|
46
|
+
name: "id",
|
|
47
|
+
fieldName: "id",
|
|
48
|
+
as: "a"
|
|
49
|
+
}
|
|
50
|
+
]
|
|
51
|
+
}
|
|
52
|
+
]
|
|
53
|
+
};
|
|
54
|
+
(0, globals_1.test)("should work for object", () => {
|
|
55
|
+
const data = {
|
|
56
|
+
id: 1,
|
|
57
|
+
sessionPlanId: null,
|
|
58
|
+
sessionPlan: undefined
|
|
59
|
+
};
|
|
60
|
+
(0, addNullFallbacks_1.addNullFallbacks)(sqlAST, data);
|
|
61
|
+
strict_1.default.deepEqual(data, {
|
|
62
|
+
id: 1,
|
|
63
|
+
sessionPlanId: null,
|
|
64
|
+
sessionPlan: null
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
(0, globals_1.test)("should work for array", () => {
|
|
68
|
+
const data = [
|
|
69
|
+
{
|
|
70
|
+
id: 1,
|
|
71
|
+
sessionPlanId: null,
|
|
72
|
+
sessionPlan: undefined
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
id: 2,
|
|
76
|
+
sessionPlanId: null,
|
|
77
|
+
sessionPlan: undefined
|
|
78
|
+
}
|
|
79
|
+
];
|
|
80
|
+
(0, addNullFallbacks_1.addNullFallbacks)(sqlAST, data);
|
|
81
|
+
strict_1.default.deepEqual(data, [
|
|
82
|
+
{
|
|
83
|
+
id: 1,
|
|
84
|
+
sessionPlanId: null,
|
|
85
|
+
sessionPlan: null
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
id: 2,
|
|
89
|
+
sessionPlanId: null,
|
|
90
|
+
sessionPlan: null
|
|
91
|
+
}
|
|
92
|
+
]);
|
|
93
|
+
});
|
|
94
|
+
(0, globals_1.test)("should work for nested array", () => {
|
|
95
|
+
const sqlAST = {
|
|
96
|
+
type: "table",
|
|
97
|
+
name: "Session",
|
|
98
|
+
as: "Session",
|
|
99
|
+
fieldName: "session",
|
|
100
|
+
grabMany: false,
|
|
101
|
+
children: [
|
|
102
|
+
{
|
|
103
|
+
type: "column",
|
|
104
|
+
name: "id",
|
|
105
|
+
fieldName: "id",
|
|
106
|
+
as: "a"
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
type: "table",
|
|
110
|
+
name: "SessionPlan",
|
|
111
|
+
as: "SessionPlan",
|
|
112
|
+
fieldName: "sessionPlan",
|
|
113
|
+
grabMany: false,
|
|
114
|
+
children: [
|
|
115
|
+
{
|
|
116
|
+
type: "column",
|
|
117
|
+
name: "id",
|
|
118
|
+
fieldName: "id",
|
|
119
|
+
as: "a"
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
type: "column",
|
|
123
|
+
name: "uuid",
|
|
124
|
+
fieldName: "uuid",
|
|
125
|
+
as: "c"
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
type: "column",
|
|
129
|
+
name: "id",
|
|
130
|
+
fieldName: "id",
|
|
131
|
+
as: "a"
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
type: "table",
|
|
135
|
+
name: "SessionPlan",
|
|
136
|
+
as: "SessionPlan",
|
|
137
|
+
fieldName: "sessionPlan",
|
|
138
|
+
grabMany: false,
|
|
139
|
+
children: [
|
|
140
|
+
{
|
|
141
|
+
type: "column",
|
|
142
|
+
name: "id",
|
|
143
|
+
fieldName: "id",
|
|
144
|
+
as: "a"
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
type: "column",
|
|
148
|
+
name: "uuid",
|
|
149
|
+
fieldName: "uuid",
|
|
150
|
+
as: "c"
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
type: "column",
|
|
154
|
+
name: "id",
|
|
155
|
+
fieldName: "id",
|
|
156
|
+
as: "a"
|
|
157
|
+
}
|
|
158
|
+
]
|
|
159
|
+
}
|
|
160
|
+
]
|
|
161
|
+
}
|
|
162
|
+
]
|
|
163
|
+
};
|
|
164
|
+
const data = [
|
|
165
|
+
{
|
|
166
|
+
id: 1,
|
|
167
|
+
sessionPlanId: 1,
|
|
168
|
+
sessionPlan: {
|
|
169
|
+
id: 1,
|
|
170
|
+
uuid: "stub",
|
|
171
|
+
sessionPlan: undefined
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
id: 2,
|
|
176
|
+
sessionPlanId: 2,
|
|
177
|
+
sessionPlan: {
|
|
178
|
+
id: 2,
|
|
179
|
+
uuid: "stub",
|
|
180
|
+
sessionPlan: undefined
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
];
|
|
184
|
+
(0, addNullFallbacks_1.addNullFallbacks)(sqlAST, data);
|
|
185
|
+
strict_1.default.deepEqual(data, [
|
|
186
|
+
{
|
|
187
|
+
id: 1,
|
|
188
|
+
sessionPlanId: 1,
|
|
189
|
+
sessionPlan: {
|
|
190
|
+
id: 1,
|
|
191
|
+
uuid: "stub",
|
|
192
|
+
sessionPlan: null
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
id: 2,
|
|
197
|
+
sessionPlanId: 2,
|
|
198
|
+
sessionPlan: {
|
|
199
|
+
id: 2,
|
|
200
|
+
uuid: "stub",
|
|
201
|
+
sessionPlan: null
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
]);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.encodeCursor = encodeCursor;
|
|
4
|
+
exports.decodeCursor = decodeCursor;
|
|
5
|
+
function encodeCursor(cursor) {
|
|
6
|
+
return Buffer.from(JSON.stringify(cursor)).toString("base64");
|
|
7
|
+
}
|
|
8
|
+
function decodeCursor(cursor) {
|
|
9
|
+
return JSON.parse(Buffer.from(cursor, "base64").toString("utf8"));
|
|
10
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getDateTimeStringMySQL(dateTimeString: string): string;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getDateTimeStringMySQL = getDateTimeStringMySQL;
|
|
4
|
+
// Assumes input uses zero offset
|
|
5
|
+
function getDateTimeStringMySQL(dateTimeString) {
|
|
6
|
+
return dateTimeString.replace("T", " ").slice(0, 19);
|
|
7
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getOrderBy = getOrderBy;
|
|
4
|
+
// https://gist.github.com/pcattori/2bb645d587e45c9fdbcabf5cef7a7106
|
|
5
|
+
function getOrderBy(args, primaryKey) {
|
|
6
|
+
let out = undefined;
|
|
7
|
+
let flip = false;
|
|
8
|
+
if (Array.isArray(args?.$orderBy)) {
|
|
9
|
+
out = args?.$orderBy.map((x) => {
|
|
10
|
+
const [column, direction] = Object.entries(x)[0];
|
|
11
|
+
return { column, direction };
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
else if (typeof args?.$orderBy === "object" && args.$orderBy != null) {
|
|
15
|
+
const [column, direction] = Object.entries(args.$orderBy)[0];
|
|
16
|
+
out = [{ column, direction }];
|
|
17
|
+
}
|
|
18
|
+
if (args?.$paginate?.first != null || args?.$paginate?.last != null) {
|
|
19
|
+
// If paginating and primaryKey is not in orderBy, add it to end.
|
|
20
|
+
// Not necessarily "asc"; see flip.
|
|
21
|
+
if (!out?.find((x) => x.column === primaryKey)) {
|
|
22
|
+
const orderByPrimaryKey = { column: primaryKey, direction: "asc" };
|
|
23
|
+
if (out == null) {
|
|
24
|
+
out = [orderByPrimaryKey];
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
out = out.concat(orderByPrimaryKey);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
flip = getFlip(args);
|
|
31
|
+
if (flip) {
|
|
32
|
+
out = flipOrderByDirection(out);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (Array.isArray(out)) {
|
|
36
|
+
for (let x of out) {
|
|
37
|
+
if (!["asc", "desc"].includes(x.direction)) {
|
|
38
|
+
throw new Error(`Expected \`asc\` or \`desc\` for \`direction\` but got: ${x.direction}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return { orderBy: out, flip };
|
|
43
|
+
}
|
|
44
|
+
function getFlip(args) {
|
|
45
|
+
return args?.$paginate?.last != null;
|
|
46
|
+
}
|
|
47
|
+
function flipOrderByDirection(orderBy) {
|
|
48
|
+
return orderBy.map((x) => ({ ...x, direction: flipDirection(x.direction) }));
|
|
49
|
+
}
|
|
50
|
+
function flipDirection(direction) {
|
|
51
|
+
return direction === "asc" ? "desc" : "asc";
|
|
52
|
+
}
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.getSqlAst = getSqlAst;
|
|
30
|
+
const SqlString = __importStar(require("sqlstring"));
|
|
31
|
+
// @ts-ignore
|
|
32
|
+
const alias_namespace_1 = __importDefault(require("join-monster/dist/alias-namespace"));
|
|
33
|
+
const _ = __importStar(require("lodash/fp"));
|
|
34
|
+
const getOrderBy_1 = require("./getOrderBy");
|
|
35
|
+
const namespace = new alias_namespace_1.default(true);
|
|
36
|
+
function getSqlAst(input) {
|
|
37
|
+
const { table, fieldName, fields, args, grabMany, sqlJoin, sqlBatch, junction, getWhere, artifacts, rowWithMatchingCursor, dialect, escapeId } = input;
|
|
38
|
+
const tableArtifacts = artifacts[table];
|
|
39
|
+
const primaryKey = tableArtifacts.primaryKey;
|
|
40
|
+
const format = SqlString.format.bind(SqlString);
|
|
41
|
+
const orderBy = input.orderBy ?? (0, getOrderBy_1.getOrderBy)(args, primaryKey)?.orderBy;
|
|
42
|
+
let where = input.where;
|
|
43
|
+
if (input.where == null) {
|
|
44
|
+
where = (table, args) => {
|
|
45
|
+
let argsMapped = args;
|
|
46
|
+
if (junction != null &&
|
|
47
|
+
typeof argsMapped?.$where === "object" &&
|
|
48
|
+
argsMapped.$where != null &&
|
|
49
|
+
argsMapped.$where[input.table] != null) {
|
|
50
|
+
argsMapped = _.cloneDeep(argsMapped);
|
|
51
|
+
argsMapped.$where = argsMapped.$where[input.table];
|
|
52
|
+
}
|
|
53
|
+
const whereResult = getWhere(
|
|
54
|
+
// table is escaped already
|
|
55
|
+
table, argsMapped, dialect, orderBy, rowWithMatchingCursor);
|
|
56
|
+
if (whereResult == null) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
return whereResult;
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
// A unique key needs to be first in children.
|
|
63
|
+
// https://github.com/join-monster/join-monster/blob/819cd03c8eca3421bf3008594ea84db8fad057d4/src/query-ast-to-sql-ast/index.js#L388
|
|
64
|
+
// https://github.com/CoursePark/NestHydrationJS#sql-ish-example
|
|
65
|
+
// https://github.com/CoursePark/NestHydrationJS#ids-that-arent-first-in-definition-properties
|
|
66
|
+
// For many-to-many batches, the unique key of the junction table should be first.
|
|
67
|
+
// https://github.com/join-monster/join-monster/blob/819cd03c8eca3421bf3008594ea84db8fad057d4/src/query-ast-to-sql-ast/index.js#L335
|
|
68
|
+
const children = [];
|
|
69
|
+
if (input.firstChild != null) {
|
|
70
|
+
children.push(input.firstChild);
|
|
71
|
+
}
|
|
72
|
+
children.push(keyToASTChild(primaryKey, namespace));
|
|
73
|
+
return {
|
|
74
|
+
type: "table",
|
|
75
|
+
name: table,
|
|
76
|
+
as: table,
|
|
77
|
+
fieldName,
|
|
78
|
+
grabMany,
|
|
79
|
+
junction,
|
|
80
|
+
sqlJoin,
|
|
81
|
+
sqlBatch,
|
|
82
|
+
args,
|
|
83
|
+
where,
|
|
84
|
+
orderBy,
|
|
85
|
+
children: children.concat(Object.entries(fields)
|
|
86
|
+
.filter(([_, v]) => v !== false)
|
|
87
|
+
.map(([k, v]) => {
|
|
88
|
+
const mappedField = tableArtifacts.mappedFields?.[k];
|
|
89
|
+
const relationField = tableArtifacts.relationFields?.[k];
|
|
90
|
+
if (mappedField != null) {
|
|
91
|
+
return {
|
|
92
|
+
type: "expression",
|
|
93
|
+
sqlExpr: (table) => {
|
|
94
|
+
const referencedTableAlias = namespace.generate("table", mappedField.referencedTable);
|
|
95
|
+
return format(`(SELECT ?? FROM ?? AS ?? WHERE ??.?? = ${table}.??)`, [
|
|
96
|
+
mappedField.name,
|
|
97
|
+
mappedField.referencedTable,
|
|
98
|
+
referencedTableAlias,
|
|
99
|
+
referencedTableAlias,
|
|
100
|
+
mappedField.referencedKey,
|
|
101
|
+
mappedField.foreignKey
|
|
102
|
+
]);
|
|
103
|
+
},
|
|
104
|
+
name: k,
|
|
105
|
+
fieldName: k,
|
|
106
|
+
as: namespace.generate("column", k)
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
else if (relationField != null) {
|
|
110
|
+
const fields = typeof v === "object"
|
|
111
|
+
? v.$fields ??
|
|
112
|
+
tableArtifacts.scalarFields.reduce((acc, x) => {
|
|
113
|
+
acc[x] = true;
|
|
114
|
+
return acc;
|
|
115
|
+
}, {})
|
|
116
|
+
: tableArtifacts.scalarFields.reduce((acc, x) => {
|
|
117
|
+
acc[x] = true;
|
|
118
|
+
return acc;
|
|
119
|
+
}, {});
|
|
120
|
+
const args = typeof v === "object" ? v : {};
|
|
121
|
+
if (relationField.type === "many-to-many") {
|
|
122
|
+
const asJunction = namespace.generate("table", relationField.junctionTable);
|
|
123
|
+
const uniqueKey = artifacts[relationField.junctionTable].primaryKey;
|
|
124
|
+
return getSqlAst({
|
|
125
|
+
table: relationField.table,
|
|
126
|
+
fieldName: k,
|
|
127
|
+
fields,
|
|
128
|
+
args,
|
|
129
|
+
grabMany: relationField.grabMany,
|
|
130
|
+
firstChild: {
|
|
131
|
+
...keyToASTChild(uniqueKey, namespace, asJunction),
|
|
132
|
+
// There's either a bug in join-monster, or join-monster doesn't expect a junction table to have a primary key.
|
|
133
|
+
// Work around by ensuring a unique name, to avoid e.g. SELECT `UserRole`.`id` AS `b`, `Role`.`id` AS `b`
|
|
134
|
+
// Notice the duplicate names in the select. This results in missing rows because the
|
|
135
|
+
// shape definition passed into NestHydrationJS will only have 1 `id`.
|
|
136
|
+
fieldName: String(Date.now()),
|
|
137
|
+
as: String(Date.now())
|
|
138
|
+
},
|
|
139
|
+
junction: {
|
|
140
|
+
sqlTable: relationField.junctionTable,
|
|
141
|
+
as: asJunction,
|
|
142
|
+
uniqueKey,
|
|
143
|
+
where: (table, args) => {
|
|
144
|
+
if (typeof args?.$where !== "object" ||
|
|
145
|
+
typeof args?.$where == null ||
|
|
146
|
+
args.$where[relationField.junctionTable] == null) {
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
const argsMapped = _.cloneDeep(args);
|
|
150
|
+
argsMapped.$where =
|
|
151
|
+
argsMapped.$where[relationField.junctionTable];
|
|
152
|
+
const whereResult = getWhere(
|
|
153
|
+
// table is escaped already
|
|
154
|
+
table, argsMapped, dialect, orderBy, rowWithMatchingCursor);
|
|
155
|
+
if (whereResult == null) {
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
return whereResult;
|
|
159
|
+
},
|
|
160
|
+
// Because we used the where in sqlJoin
|
|
161
|
+
// where: () => undefined,
|
|
162
|
+
// TODO - where
|
|
163
|
+
// sqlJoins: [
|
|
164
|
+
// (t: string, junctionTable: string, args: any) =>
|
|
165
|
+
// `${t}.${escapeId(relationField.relations[0].referencedKey)} = ${junctionTable}.${escapeId(relationField.relations[0].foreignKey)}`,
|
|
166
|
+
// (junctionTable: string, t: string, args: any) =>
|
|
167
|
+
// `${junctionTable}.${escapeId(relationField.relations[1].foreignKey)} = ${t}.${escapeId(relationField.relations[1].referencedKey)}`,
|
|
168
|
+
// ],
|
|
169
|
+
// We have to use sqlBatch instead of sqlJoin because pagination is not
|
|
170
|
+
// supported with `mysql` dialect, and LIMITing the root list means
|
|
171
|
+
// sub lists are limited, too.
|
|
172
|
+
sqlBatch: {
|
|
173
|
+
thisKey: columnToASTChild(relationField.relations[0].foreignKey, namespace, asJunction),
|
|
174
|
+
parentKey: columnToASTChild(relationField.relations[0].referencedKey, namespace),
|
|
175
|
+
sqlJoin: (junctionTable, t, args) => `${junctionTable}.${escapeId(relationField.relations[1].foreignKey)} = ${t}.${escapeId(relationField.relations[1].referencedKey)}`
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
getWhere,
|
|
179
|
+
artifacts,
|
|
180
|
+
dialect,
|
|
181
|
+
escapeId
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
return getSqlAst({
|
|
185
|
+
table: relationField.table,
|
|
186
|
+
fieldName: k,
|
|
187
|
+
fields,
|
|
188
|
+
args,
|
|
189
|
+
grabMany: relationField.grabMany,
|
|
190
|
+
// Because we used the where in sqlJoin
|
|
191
|
+
// where: () => undefined,
|
|
192
|
+
// sqlJoin: (t1: string, t2: string, args: any) => {
|
|
193
|
+
// let sql = `${t1}.?? = ${t2}.??`;
|
|
194
|
+
// let values = [relationField.relation.foreignKey, relationField.relation.referencedKey];
|
|
195
|
+
// if (args?.$where != null) {
|
|
196
|
+
// const whereResult = getWhere(t2, args);
|
|
197
|
+
// sql += " and " + whereResult.sql;
|
|
198
|
+
// values.push(whereResult.values);
|
|
199
|
+
// }
|
|
200
|
+
// return s.format(sql, values);
|
|
201
|
+
// },
|
|
202
|
+
// We have to use sqlBatch instead of sqlJoin because pagination is not
|
|
203
|
+
// supported with `mysql` dialect, and LIMITing the root list means
|
|
204
|
+
// sub lists are limited, too.
|
|
205
|
+
sqlBatch: {
|
|
206
|
+
thisKey: columnToASTChild(relationField.relation.referencedKey, namespace),
|
|
207
|
+
parentKey: columnToASTChild(relationField.relation.foreignKey, namespace)
|
|
208
|
+
},
|
|
209
|
+
getWhere,
|
|
210
|
+
artifacts,
|
|
211
|
+
dialect,
|
|
212
|
+
escapeId
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
return columnToASTChild(k, namespace);
|
|
217
|
+
}
|
|
218
|
+
}))
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
function keyToASTChild(key, namespace, fromOtherTable) {
|
|
222
|
+
if (Array.isArray(key)) {
|
|
223
|
+
const clumsyName = toClumsyName(key);
|
|
224
|
+
return {
|
|
225
|
+
type: "composite",
|
|
226
|
+
name: key,
|
|
227
|
+
fieldName: clumsyName,
|
|
228
|
+
as: namespace.generate("column", clumsyName),
|
|
229
|
+
fromOtherTable
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
return columnToASTChild(key, namespace, fromOtherTable);
|
|
233
|
+
}
|
|
234
|
+
function columnToASTChild(columnName, namespace, fromOtherTable) {
|
|
235
|
+
return {
|
|
236
|
+
type: "column",
|
|
237
|
+
name: columnName,
|
|
238
|
+
fieldName: columnName,
|
|
239
|
+
as: namespace.generate("column", columnName),
|
|
240
|
+
fromOtherTable
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
function toClumsyName(keyArr) {
|
|
244
|
+
return keyArr.map((name) => name.slice(0, 3)).join("#");
|
|
245
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getWhere = getWhere;
|
|
4
|
+
const stringifyWhere_1 = require("./stringifyWhere");
|
|
5
|
+
function getWhere(
|
|
6
|
+
// Note: `table` is not escaped in getWhere, so it should be escaped beforehand.
|
|
7
|
+
table, args, dialect, orderBy, rowWithMatchingCursor) {
|
|
8
|
+
if (args?.$where == null && args?.$paginate == null) {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
let where = args?.$where ?? {};
|
|
12
|
+
return ((0, stringifyWhere_1.stringifyWhere)({
|
|
13
|
+
where,
|
|
14
|
+
table,
|
|
15
|
+
dialect,
|
|
16
|
+
args,
|
|
17
|
+
orderBy,
|
|
18
|
+
rowWithMatchingCursor,
|
|
19
|
+
}) || null);
|
|
20
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type IGetSQLASTInput, type IArtifacts, type IDialect, type TDbCall, type TFormatQuery, type TBeginTransaction, type TContext, type TMiddleware, type TResolveParams } from "../IRuntime";
|
|
2
|
+
import Cache from "../Cache";
|
|
3
|
+
export declare function resolve(input: TResolveParams, dbCall: TDbCall, formatQuery: TFormatQuery, beginTransaction: TBeginTransaction, dialect: IDialect, middlewareHandler: MiddlewareHandler<TMiddleware>, context: TContext, cache?: Cache): Promise<any>;
|
|
4
|
+
export declare class MiddlewareHandler<M extends Function> {
|
|
5
|
+
private _middlewares;
|
|
6
|
+
register(middleware: M): void;
|
|
7
|
+
get(id: number): M | undefined;
|
|
8
|
+
has(id: number): boolean;
|
|
9
|
+
length(): number;
|
|
10
|
+
}
|
|
11
|
+
export declare function postProcess(data: any, fields: IGetSQLASTInput["fields"], shouldRemoveExtraKeys: boolean): void;
|
|
12
|
+
export declare function whereNeedsProcessing(where: any): boolean;
|
|
13
|
+
export declare function _prepareWhere(artifacts: IArtifacts, table: string, data: any, dbCall: TDbCall, formatQuery: TFormatQuery): Promise<{}>;
|