@technicity/data-service-generator 0.9.0 → 0.10.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/index.d.ts +1 -2
- package/dist/index.js +3 -4
- package/dist/runtime/Cache.d.ts +18 -0
- package/dist/runtime/Cache.js +90 -0
- package/dist/runtime/Runtime.d.ts +23 -0
- package/dist/runtime/Runtime.js +29 -0
- package/dist/runtime/RuntimeKSQL.d.ts +1 -1
- package/dist/runtime/RuntimeKSQL.js +13 -11
- package/dist/runtime/RuntimeMSSQL.d.ts +18 -14
- package/dist/runtime/RuntimeMSSQL.js +48 -43
- package/dist/runtime/RuntimeMySQL.d.ts +11 -12
- package/dist/runtime/RuntimeMySQL.js +46 -79
- package/dist/runtime/lib/create.d.ts +12 -0
- package/dist/runtime/lib/create.js +175 -0
- package/dist/runtime/lib/delete.d.ts +5 -0
- package/dist/runtime/lib/delete.js +43 -0
- package/dist/runtime/lib/error.d.ts +9 -0
- package/dist/runtime/lib/error.js +22 -0
- package/dist/runtime/lib/getData.d.ts +4 -0
- package/dist/runtime/lib/getData.js +227 -0
- package/dist/runtime/lib/getSqlAst.js +2 -2
- package/dist/runtime/lib/getWhere.d.ts +17 -0
- package/dist/runtime/lib/getWhere.js +228 -4
- package/dist/runtime/lib/prepareWhere.d.ts +2 -0
- package/dist/runtime/lib/prepareWhere.js +158 -0
- package/dist/runtime/lib/resolve.d.ts +10 -0
- package/dist/runtime/lib/resolve.js +66 -0
- package/dist/runtime/lib/shared.d.ts +22 -12
- package/dist/runtime/lib/shared.js +16 -746
- package/dist/runtime/lib/typeCastMSSQL.js +1 -1
- package/dist/runtime/lib/update.d.ts +4 -0
- package/dist/runtime/lib/update.js +143 -0
- package/dist/runtime/lib/utility.d.ts +5 -0
- package/dist/runtime/lib/utility.js +15 -0
- package/package.json +7 -6
- package/dist/runtime/lib/MSSQL.d.ts +0 -13
- package/dist/runtime/lib/MSSQL.js +0 -73
- package/dist/runtime/lib/SDKBadWhereError.d.ts +0 -4
- package/dist/runtime/lib/SDKBadWhereError.js +0 -10
- package/dist/runtime/lib/SDKNotFoundError.d.ts +0 -4
- package/dist/runtime/lib/SDKNotFoundError.js +0 -10
- package/dist/runtime/lib/getDateTimeStringMySQL.d.ts +0 -1
- package/dist/runtime/lib/getDateTimeStringMySQL.js +0 -8
- package/dist/runtime/lib/stringifyWhere.d.ts +0 -18
- package/dist/runtime/lib/stringifyWhere.js +0 -228
|
@@ -1,278 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
// @ts-ignore
|
|
5
|
-
// import * as queryAST from "join-monster/dist/query-ast-to-sql-ast";
|
|
6
|
-
// @ts-ignore
|
|
7
|
-
// import arrToConnection from "join-monster/dist/array-to-connection";
|
|
8
|
-
// @ts-ignore
|
|
9
|
-
const batch_planner_1 = require("join-monster/dist/batch-planner");
|
|
3
|
+
exports.postProcess = exports.mapMappedFields = exports.hasMappedFields = exports.getScalarFields = exports.getTotalCount = exports.wrapListPaginationCursor = exports.getDateTimeStringMySQL = exports.wrapListPaginationLimitOffset = void 0;
|
|
10
4
|
// @ts-ignore
|
|
11
5
|
const util_1 = require("join-monster/dist/util");
|
|
12
|
-
const async_hooks_1 = require("async_hooks");
|
|
13
6
|
const _ = require("lodash/fp");
|
|
14
|
-
const
|
|
15
|
-
const getSqlAst_1 = require("./getSqlAst");
|
|
16
|
-
const getWhere_1 = require("./getWhere");
|
|
17
|
-
const getDateTimeStringMySQL_1 = require("./getDateTimeStringMySQL");
|
|
7
|
+
const error_1 = require("./error");
|
|
18
8
|
const cursor_1 = require("./cursor");
|
|
19
9
|
const runTransforms_1 = require("./runTransforms");
|
|
20
|
-
const addNullFallbacks_1 = require("./addNullFallbacks");
|
|
21
|
-
const SDKNotFoundError_1 = require("./SDKNotFoundError");
|
|
22
|
-
const SDKBadWhereError_1 = require("./SDKBadWhereError");
|
|
23
|
-
const stringifyWhere_1 = require("./stringifyWhere");
|
|
24
|
-
const getOrderBy_1 = require("./getOrderBy");
|
|
25
|
-
async function resolve(input, dbCall, formatQuery, beginTransaction, dialect, middlewareHandler, context) {
|
|
26
|
-
// https://github.com/prisma/prisma/blob/822198e5ba21535364d20c86901b8c3778ebf6a3/packages/client/src/runtime/getPrismaClient.ts#L1087
|
|
27
|
-
let index = -1;
|
|
28
|
-
if (middlewareHandler.length() > 0) {
|
|
29
|
-
const resource = new async_hooks_1.AsyncResource("sdk-request");
|
|
30
|
-
const params = input;
|
|
31
|
-
const consumer = (paramsMaybeMutated) => {
|
|
32
|
-
const nextMiddleware = middlewareHandler.get(++index);
|
|
33
|
-
if (nextMiddleware != null) {
|
|
34
|
-
return nextMiddleware(paramsMaybeMutated, consumer);
|
|
35
|
-
}
|
|
36
|
-
const paramsChanged = { ...input, ...params };
|
|
37
|
-
return _resolve(paramsChanged, dbCall, formatQuery, beginTransaction, dialect, context);
|
|
38
|
-
};
|
|
39
|
-
return resource.runInAsyncScope(() => consumer(params));
|
|
40
|
-
}
|
|
41
|
-
return _resolve(input, dbCall, formatQuery, beginTransaction, dialect, context);
|
|
42
|
-
}
|
|
43
|
-
exports.resolve = resolve;
|
|
44
|
-
function _resolve(input, dbCall, formatQuery, beginTransaction, dialect, context) {
|
|
45
|
-
if (input.action === "findUnique") {
|
|
46
|
-
return getData(input, dbCall, formatQuery, dialect);
|
|
47
|
-
}
|
|
48
|
-
if (input.action === "findMany") {
|
|
49
|
-
return getData(input, dbCall, formatQuery, dialect);
|
|
50
|
-
}
|
|
51
|
-
if (input.action === "findManyPaginated") {
|
|
52
|
-
return getData(input, dbCall, formatQuery, dialect);
|
|
53
|
-
}
|
|
54
|
-
if (input.action === "create") {
|
|
55
|
-
return create(input, dbCall, formatQuery, beginTransaction, dialect, context);
|
|
56
|
-
}
|
|
57
|
-
if (input.action === "update") {
|
|
58
|
-
return update(input, dbCall, formatQuery, dialect);
|
|
59
|
-
}
|
|
60
|
-
if (input.action === "updateMany") {
|
|
61
|
-
return updateMany(input, dbCall, formatQuery, dialect);
|
|
62
|
-
}
|
|
63
|
-
if (input.action === "delete") {
|
|
64
|
-
return del(input, dbCall, formatQuery, dialect);
|
|
65
|
-
}
|
|
66
|
-
if (input.action === "deleteMany") {
|
|
67
|
-
return deleteMany(input, dbCall, formatQuery, dialect);
|
|
68
|
-
}
|
|
69
|
-
throw new Error("Invalid action: " + input.action);
|
|
70
|
-
}
|
|
71
|
-
class MiddlewareHandler {
|
|
72
|
-
constructor() {
|
|
73
|
-
this._middlewares = [];
|
|
74
|
-
}
|
|
75
|
-
register(middleware) {
|
|
76
|
-
this._middlewares.push(middleware);
|
|
77
|
-
}
|
|
78
|
-
get(id) {
|
|
79
|
-
return this._middlewares[id];
|
|
80
|
-
}
|
|
81
|
-
has(id) {
|
|
82
|
-
return !!this._middlewares[id];
|
|
83
|
-
}
|
|
84
|
-
length() {
|
|
85
|
-
return this._middlewares.length;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
exports.MiddlewareHandler = MiddlewareHandler;
|
|
89
|
-
async function getData(input, dbCall, formatQuery, dialect) {
|
|
90
|
-
const context = {};
|
|
91
|
-
const action = input.action;
|
|
92
|
-
const primaryKey = input.artifacts[input.resource].primaryKey;
|
|
93
|
-
let paginationType;
|
|
94
|
-
let limit = undefined;
|
|
95
|
-
let offset = undefined;
|
|
96
|
-
let rowWithMatchingCursor = undefined;
|
|
97
|
-
if (action === "findManyPaginated") {
|
|
98
|
-
if (input.args?.$paginate == null) {
|
|
99
|
-
throw new Error("$paginate required but not supplied");
|
|
100
|
-
}
|
|
101
|
-
if (typeof input?.args?.$paginate?.limit === "number") {
|
|
102
|
-
paginationType = "limit-offset";
|
|
103
|
-
limit = input.args.$paginate.limit;
|
|
104
|
-
offset = input.args.$paginate.offset;
|
|
105
|
-
}
|
|
106
|
-
else if (typeof input?.args?.$paginate?.first === "number" ||
|
|
107
|
-
typeof input?.args?.$paginate?.last === "number") {
|
|
108
|
-
paginationType = "cursor";
|
|
109
|
-
limit = (typeof input?.args?.$paginate?.first === "number"
|
|
110
|
-
? input?.args?.$paginate?.first
|
|
111
|
-
: input?.args?.$paginate?.last);
|
|
112
|
-
// + 1 to peek if there is more data
|
|
113
|
-
limit += 1;
|
|
114
|
-
if (input.args.$paginate.after != null ||
|
|
115
|
-
input.args.$paginate.before != null) {
|
|
116
|
-
const cursor = input.args.$paginate.after != null
|
|
117
|
-
? input.args.$paginate.after
|
|
118
|
-
: input.args.$paginate.before;
|
|
119
|
-
rowWithMatchingCursor = await dbCall(formatQuery("SELECT * FROM ?? WHERE ?? = ?", [
|
|
120
|
-
input.resource,
|
|
121
|
-
primaryKey,
|
|
122
|
-
(0, cursor_1.decodeCursor)(cursor),
|
|
123
|
-
])).then((xs) => xs[0]);
|
|
124
|
-
if (rowWithMatchingCursor == null) {
|
|
125
|
-
throw new Error(`Invalid cursor: ${cursor}`);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
else {
|
|
130
|
-
throw new Error(`Invalid $paginate: ${input?.args?.$paginate}`);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
else if (action === "findMany") {
|
|
134
|
-
if (typeof input?.args?.$limit === "number") {
|
|
135
|
-
limit = input?.args?.$limit;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
// we need to read the query AST and build a new "SQL AST" from which the SQL and
|
|
139
|
-
// const sqlAST = queryAST.queryASTToSqlAST(resolveInfo, options, context);
|
|
140
|
-
const fields = input.fields ?? getScalarFields(input.resource, input.artifacts);
|
|
141
|
-
const orderByListPaginatedRootResult =
|
|
142
|
-
// MSSQL's OFFSET and FETCH requires ORDER BY, so we need to provide a fallback
|
|
143
|
-
dialect === "mssql" && paginationType === "limit-offset"
|
|
144
|
-
? {
|
|
145
|
-
orderBy: (0, getOrderBy_1.getOrderBy)(input.args, primaryKey)?.orderBy ?? [
|
|
146
|
-
{ column: primaryKey, direction: "asc" },
|
|
147
|
-
],
|
|
148
|
-
flip: false,
|
|
149
|
-
}
|
|
150
|
-
: action === "findManyPaginated"
|
|
151
|
-
? (0, getOrderBy_1.getOrderBy)(input.args, primaryKey)
|
|
152
|
-
: undefined;
|
|
153
|
-
const grabMany = action === "findMany" ||
|
|
154
|
-
action === "findManyPaginated" ||
|
|
155
|
-
action === "updateMany" ||
|
|
156
|
-
action === "deleteMany";
|
|
157
|
-
const sqlAST = (0, getSqlAst_1.getSqlAst)({
|
|
158
|
-
...input,
|
|
159
|
-
table: input.resource,
|
|
160
|
-
fieldName: "data",
|
|
161
|
-
fields,
|
|
162
|
-
getWhere: getWhere_1.getWhere,
|
|
163
|
-
orderBy: orderByListPaginatedRootResult?.orderBy,
|
|
164
|
-
rowWithMatchingCursor,
|
|
165
|
-
dialect,
|
|
166
|
-
grabMany,
|
|
167
|
-
});
|
|
168
|
-
const options = { dialect };
|
|
169
|
-
let { sql, shapeDefinition } = await (0, util_1.compileSqlAST)(sqlAST, context, options);
|
|
170
|
-
if (!sql) {
|
|
171
|
-
// return {};
|
|
172
|
-
throw new Error("No SQL");
|
|
173
|
-
}
|
|
174
|
-
// TODO - remove if limit support added for mysql dialect
|
|
175
|
-
if (action === "findMany" || action === "findManyPaginated") {
|
|
176
|
-
if (typeof limit === "number") {
|
|
177
|
-
const escape = (0, stringifyWhere_1.getEscape)(dialect);
|
|
178
|
-
if (dialect === "mssql") {
|
|
179
|
-
if (typeof offset === "number") {
|
|
180
|
-
sql += ` OFFSET ${escape(offset)} ROWS FETCH NEXT ${escape(limit)} ROWS ONLY`;
|
|
181
|
-
}
|
|
182
|
-
else {
|
|
183
|
-
sql = sql.replace("SELECT", `SELECT TOP ${escape(limit)}`);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
else {
|
|
187
|
-
sql += ` LIMIT ${escape(limit)}`;
|
|
188
|
-
if (typeof offset === "number") {
|
|
189
|
-
sql += ` OFFSET ${escape(offset)}`;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
// call their function for querying the DB, handle the different cases, do some validation, return a promise of the object
|
|
195
|
-
let data = await (0, util_1.handleUserDbCall)(dbCall, sql, sqlAST, shapeDefinition);
|
|
196
|
-
// if they are paginating, we'll get back an array which is essentially a "slice" of the whole data.
|
|
197
|
-
// this function goes through the data tree and converts the arrays to Connection Objects
|
|
198
|
-
// data = arrToConnection(data, sqlAST);
|
|
199
|
-
// so far we handled the first "batch". up until now, additional batches were ignored
|
|
200
|
-
// this function recursively scanss the sqlAST and runs remaining batches
|
|
201
|
-
await (0, batch_planner_1.default)(sqlAST, data, dbCall, context, options);
|
|
202
|
-
(0, addNullFallbacks_1.addNullFallbacks)(sqlAST, data);
|
|
203
|
-
// We only need to remove extra keys if input.fields isn't
|
|
204
|
-
// specified, since otherwise there wouldn't be extra keys
|
|
205
|
-
const shouldRemoveExtraKeys = input.fields != null;
|
|
206
|
-
if (action !== "findManyPaginated") {
|
|
207
|
-
// Remove additional keys that are added for batches and joins
|
|
208
|
-
// Do later for `listPaginated`, since the `id` is needed for
|
|
209
|
-
// creating the cursor
|
|
210
|
-
postProcess(data, fields, shouldRemoveExtraKeys);
|
|
211
|
-
}
|
|
212
|
-
// check for batch data
|
|
213
|
-
if (Array.isArray(data)) {
|
|
214
|
-
// TODO - not sure why this code exists in original
|
|
215
|
-
// source code of join-monster; doesn't make sense to me.
|
|
216
|
-
// const childrenToCheck = sqlAST.children.filter(
|
|
217
|
-
// (child: any) => child.sqlBatch
|
|
218
|
-
// );
|
|
219
|
-
// data = data.filter((d) => {
|
|
220
|
-
// for (const child of childrenToCheck) {
|
|
221
|
-
// if (d[child.fieldName] == null) {
|
|
222
|
-
// return false;
|
|
223
|
-
// }
|
|
224
|
-
// }
|
|
225
|
-
// return true;
|
|
226
|
-
// });
|
|
227
|
-
if (action === "findManyPaginated") {
|
|
228
|
-
const argsTotalCount = input.args && _.cloneDeep(input.args);
|
|
229
|
-
if (argsTotalCount != null) {
|
|
230
|
-
if (argsTotalCount.$paginate != null) {
|
|
231
|
-
// We don't want the where clause to include cursor-related stuff
|
|
232
|
-
delete argsTotalCount.$paginate.after;
|
|
233
|
-
delete argsTotalCount.$paginate.before;
|
|
234
|
-
// We don't need offset
|
|
235
|
-
delete argsTotalCount.$paginate.offset;
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
const sqlASTTotalCount = (0, getSqlAst_1.getSqlAst)({
|
|
239
|
-
...input,
|
|
240
|
-
table: input.resource,
|
|
241
|
-
fieldName: "data",
|
|
242
|
-
args: argsTotalCount,
|
|
243
|
-
// Because we're going to manually set children anyway
|
|
244
|
-
fields: [],
|
|
245
|
-
getWhere: getWhere_1.getWhere,
|
|
246
|
-
// We don't want the where clause to include cursor-related stuff
|
|
247
|
-
rowWithMatchingCursor: null,
|
|
248
|
-
dialect,
|
|
249
|
-
grabMany: true,
|
|
250
|
-
});
|
|
251
|
-
// Because orderBy doesn't matter for total count.
|
|
252
|
-
// getOrderBy adds an element if paginating, so deleting args.$orderBy
|
|
253
|
-
// isn't sufficient.
|
|
254
|
-
delete sqlASTTotalCount.orderBy;
|
|
255
|
-
const totalCount = await getTotalCount(sqlASTTotalCount, dbCall, context, options);
|
|
256
|
-
if (paginationType === "cursor") {
|
|
257
|
-
data = wrapListPaginationCursor(data, input.args, orderByListPaginatedRootResult.flip, (xs) => {
|
|
258
|
-
postProcess(xs, fields, shouldRemoveExtraKeys);
|
|
259
|
-
}, input.artifacts[input.resource].primaryKey, totalCount);
|
|
260
|
-
}
|
|
261
|
-
else {
|
|
262
|
-
postProcess(data, fields, shouldRemoveExtraKeys);
|
|
263
|
-
data = wrapListPaginationLimitOffset(data, totalCount);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
return data;
|
|
267
|
-
}
|
|
268
|
-
if (data == null && !grabMany) {
|
|
269
|
-
throw new SDKNotFoundError_1.SDKNotFoundError();
|
|
270
|
-
}
|
|
271
|
-
return data;
|
|
272
|
-
}
|
|
273
10
|
function wrapListPaginationLimitOffset(data, totalCount) {
|
|
274
11
|
return { paginationInfo: { totalCount }, results: data };
|
|
275
12
|
}
|
|
13
|
+
exports.wrapListPaginationLimitOffset = wrapListPaginationLimitOffset;
|
|
14
|
+
function getDateTimeStringMySQL(dateTimeString) {
|
|
15
|
+
return dateTimeString.replace("T", " ").slice(0, 19);
|
|
16
|
+
}
|
|
17
|
+
exports.getDateTimeStringMySQL = getDateTimeStringMySQL;
|
|
276
18
|
// Not recursive at the moment; only supported at the root.
|
|
277
19
|
// TODO - remove if support for pagination for mysql dialect
|
|
278
20
|
// is added (I doubt it)
|
|
@@ -304,6 +46,7 @@ function wrapListPaginationCursor(data, args, flip, cb, primaryKey, totalCount)
|
|
|
304
46
|
results: data,
|
|
305
47
|
};
|
|
306
48
|
}
|
|
49
|
+
exports.wrapListPaginationCursor = wrapListPaginationCursor;
|
|
307
50
|
async function getTotalCount(sqlASTTotalCount, dbCall, context, options) {
|
|
308
51
|
// Replace field selection with `COUNT(*)`
|
|
309
52
|
const fieldNameTotalCount = "totalCount";
|
|
@@ -319,332 +62,13 @@ async function getTotalCount(sqlASTTotalCount, dbCall, context, options) {
|
|
|
319
62
|
const totalCount = await dbCall(sqlTotalCount).then((xs) => xs[0][fieldNameTotalCount]);
|
|
320
63
|
return totalCount;
|
|
321
64
|
}
|
|
322
|
-
|
|
323
|
-
let allColumns = columns;
|
|
324
|
-
if (typeof referencedKey === "string") {
|
|
325
|
-
allColumns = allColumns.slice().map((xs) => xs.concat(referencedKey));
|
|
326
|
-
}
|
|
327
|
-
let allValues = values;
|
|
328
|
-
if (referencedKeyValue != null) {
|
|
329
|
-
allValues = allValues.slice().map((xs) => xs.concat(referencedKeyValue));
|
|
330
|
-
}
|
|
331
|
-
return Promise.all(allColumns.map((cs, i) => dbCall(formatQuery(`INSERT INTO ?? (??) VALUES (?)`, [table, cs, allValues[i]])).then((x) => x.insertId)));
|
|
332
|
-
};
|
|
333
|
-
// This doesn't use bulk inserts because:
|
|
334
|
-
// 1. columns aren't necessarily uniform
|
|
335
|
-
// 2. We don't want to do backflips to get all the inserted IDs
|
|
336
|
-
const runCreateTreeMSSQL = async (table, referencedKey, referencedKeyValue, columns, values, dbCall, formatQuery) => {
|
|
337
|
-
let allColumns = columns;
|
|
338
|
-
if (typeof referencedKey === "string") {
|
|
339
|
-
allColumns = allColumns.slice().map((xs) => xs.concat(referencedKey));
|
|
340
|
-
}
|
|
341
|
-
let allValues = values;
|
|
342
|
-
if (referencedKeyValue != null) {
|
|
343
|
-
allValues = allValues.slice().map((xs) => xs.concat(referencedKeyValue));
|
|
344
|
-
}
|
|
345
|
-
// https://github.com/tediousjs/node-mssql/issues/302
|
|
346
|
-
// return Promise.all(
|
|
347
|
-
// allColumns.map((cs, i) =>
|
|
348
|
-
// dbCall(
|
|
349
|
-
// formatQuery(
|
|
350
|
-
// `INSERT INTO ?? (??) VALUES (?) SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]`,
|
|
351
|
-
// [table, cs, allValues[i]]
|
|
352
|
-
// )
|
|
353
|
-
// ).then((xs) => xs[0]["SCOPE_IDENTITY"])
|
|
354
|
-
// )
|
|
355
|
-
// );
|
|
356
|
-
let out = [];
|
|
357
|
-
let i = 0;
|
|
358
|
-
for (let cs of allColumns) {
|
|
359
|
-
out.push(await dbCall(formatQuery(`INSERT INTO ?? (??) VALUES (?) SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]`, [table, cs, allValues[i]])).then((xs) => xs[0]["SCOPE_IDENTITY"]));
|
|
360
|
-
i += 1;
|
|
361
|
-
}
|
|
362
|
-
return out;
|
|
363
|
-
};
|
|
364
|
-
async function create(input, dbCall, formatQuery, beginTransaction, dialect, context) {
|
|
365
|
-
async function _create() {
|
|
366
|
-
// Shallow clone, as we're going to mutate later
|
|
367
|
-
let data = { ...input.data };
|
|
368
|
-
const tableArtifacts = input.artifacts[input.resource];
|
|
369
|
-
// Top-level only
|
|
370
|
-
if (hasMappedFields(input.artifacts, input.resource, data)) {
|
|
371
|
-
await mapMappedFields(tableArtifacts, data, dbCall, formatQuery);
|
|
372
|
-
}
|
|
373
|
-
const hasChildren = Object.keys(data).some((k) => {
|
|
374
|
-
const oneToManyRelation = tableArtifacts.relationFields[k];
|
|
375
|
-
if (oneToManyRelation == null) {
|
|
376
|
-
return false;
|
|
377
|
-
}
|
|
378
|
-
if (oneToManyRelation.type === "one-to-many__many-to-one" &&
|
|
379
|
-
oneToManyRelation.kind === "one-to-many") {
|
|
380
|
-
return true;
|
|
381
|
-
}
|
|
382
|
-
});
|
|
383
|
-
if (hasChildren) {
|
|
384
|
-
const { dbCall: dbCallTransaction, commit } = await beginTransaction();
|
|
385
|
-
const id = await runCreateTree(getCreateTree([data], input.resource, null, context?.specialCaseUuidColumn, dialect, input.artifacts), null, dialect === "mssql" ? runCreateTreeMSSQL : runCreateTreeMySQL, dbCallTransaction, formatQuery, dialect).then((xs) => xs[0]);
|
|
386
|
-
await commit();
|
|
387
|
-
return id;
|
|
388
|
-
}
|
|
389
|
-
else {
|
|
390
|
-
data = processCreateData(data, tableArtifacts, dialect, context?.specialCaseUuidColumn);
|
|
391
|
-
if (dialect === "mysql") {
|
|
392
|
-
const inserted = await dbCall(formatQuery("INSERT INTO ?? SET ?", [input.resource, data]));
|
|
393
|
-
return inserted.insertId;
|
|
394
|
-
}
|
|
395
|
-
else {
|
|
396
|
-
const columns = Object.keys(data);
|
|
397
|
-
const values = Object.values(data);
|
|
398
|
-
const inserted = await dbCall(formatQuery(`INSERT INTO ?? (??) VALUES (?) SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]`, [input.resource, columns, values]));
|
|
399
|
-
return inserted[0]["SCOPE_IDENTITY"];
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
const id = await _create();
|
|
404
|
-
return getData({ ...input, args: { $where: { id } } }, dbCall, formatQuery, dialect);
|
|
405
|
-
}
|
|
406
|
-
function processCreateData(data, tableArtifacts, dialect, specialCaseUuidColumn) {
|
|
407
|
-
let out = { ...data };
|
|
408
|
-
if (dialect === "mysql" && tableArtifacts.dateTimeFieldsCount > 0) {
|
|
409
|
-
for (let k in tableArtifacts.dateTimeFields) {
|
|
410
|
-
if (out[k] != null) {
|
|
411
|
-
out[k] = (0, getDateTimeStringMySQL_1.getDateTimeStringMySQL)(out[k]);
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
// Delete keys with value of undefined
|
|
416
|
-
for (let k in out) {
|
|
417
|
-
if (out[k] === void 0) {
|
|
418
|
-
delete out[k];
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
const scalarFieldSet = new Set(tableArtifacts.scalarFields);
|
|
422
|
-
if (specialCaseUuidColumn &&
|
|
423
|
-
scalarFieldSet.has("uuid") &&
|
|
424
|
-
!Object.prototype.hasOwnProperty.call(out, "uuid")) {
|
|
425
|
-
out["uuid"] = (0, uuid_1.v4)();
|
|
426
|
-
}
|
|
427
|
-
return out;
|
|
428
|
-
}
|
|
429
|
-
async function runCreateTree(tree, referencedKeyValue, runCreateTreeSQL, dbCall, formatQuery, dialect) {
|
|
430
|
-
const ids = await runCreateTreeSQL(tree.table, tree.referencedKey, referencedKeyValue, tree.columns, tree.values, dbCall, formatQuery);
|
|
431
|
-
if (tree.children?.length > 0) {
|
|
432
|
-
// https://github.com/tediousjs/node-mssql/issues/302
|
|
433
|
-
if (dialect === "mssql") {
|
|
434
|
-
const idIndexes = Array(ids.length)
|
|
435
|
-
.fill(null)
|
|
436
|
-
.map((_, i) => i);
|
|
437
|
-
for (let i of idIndexes) {
|
|
438
|
-
for (let c of tree.children[i]) {
|
|
439
|
-
await runCreateTree(c, ids[i], runCreateTreeSQL, dbCall, formatQuery, dialect);
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
else {
|
|
444
|
-
await Promise.all(ids.flatMap((id, i) => tree.children[i].map((c) => runCreateTree(c, id, runCreateTreeSQL, dbCall, formatQuery, dialect))));
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
return ids;
|
|
448
|
-
}
|
|
449
|
-
function getCreateTree(data, table, referencedKey, specialCaseUuidColumn, dialect, artifacts) {
|
|
450
|
-
const tableArtifacts = artifacts[table];
|
|
451
|
-
const scalarFieldSet = new Set(tableArtifacts.scalarFields);
|
|
452
|
-
let out = {
|
|
453
|
-
table,
|
|
454
|
-
referencedKey,
|
|
455
|
-
columns: [],
|
|
456
|
-
values: [],
|
|
457
|
-
children: [],
|
|
458
|
-
};
|
|
459
|
-
for (let i = 0; i < data.length; i++) {
|
|
460
|
-
let d = data[i];
|
|
461
|
-
if (Object.keys(d).length === 0) {
|
|
462
|
-
continue;
|
|
463
|
-
}
|
|
464
|
-
d = processCreateData(d, tableArtifacts, dialect, specialCaseUuidColumn);
|
|
465
|
-
out.columns[i] = [];
|
|
466
|
-
out.values[i] = [];
|
|
467
|
-
out.children[i] = [];
|
|
468
|
-
for (let k in d) {
|
|
469
|
-
if (scalarFieldSet.has(k)) {
|
|
470
|
-
out.columns[i].push(k);
|
|
471
|
-
const v = d[k];
|
|
472
|
-
out.values[i].push(v);
|
|
473
|
-
continue;
|
|
474
|
-
}
|
|
475
|
-
const oneToManyRelation = tableArtifacts.relationFields[k];
|
|
476
|
-
if (oneToManyRelation == null ||
|
|
477
|
-
oneToManyRelation.type !== "one-to-many__many-to-one" ||
|
|
478
|
-
oneToManyRelation.kind !== "one-to-many") {
|
|
479
|
-
continue;
|
|
480
|
-
}
|
|
481
|
-
if (!Array.isArray(d[k]?.$create)) {
|
|
482
|
-
throw new Error("Invalid data: " + k);
|
|
483
|
-
}
|
|
484
|
-
out.children[i].push(getCreateTree(d[k].$create, oneToManyRelation.table, oneToManyRelation.relation.referencedKey, specialCaseUuidColumn, dialect, artifacts));
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
return out;
|
|
488
|
-
}
|
|
489
|
-
async function update(input, dbCall, formatQuery, dialect) {
|
|
490
|
-
async function _update() {
|
|
491
|
-
const escapeId = (0, stringifyWhere_1.getEscapeId)(dialect);
|
|
492
|
-
const tableArtifacts = input.artifacts[input.resource];
|
|
493
|
-
const where = (0, getWhere_1.getWhere)(escapeId(input.resource), input.args, dialect);
|
|
494
|
-
if (where == null) {
|
|
495
|
-
throw new Error("Null where");
|
|
496
|
-
}
|
|
497
|
-
const current = await dbCall(formatQuery("SELECT * FROM ?? WHERE " + where, [input.resource])).then((xs) => xs[0]);
|
|
498
|
-
if (current == null) {
|
|
499
|
-
throw new SDKNotFoundError_1.SDKNotFoundError();
|
|
500
|
-
}
|
|
501
|
-
// Shallow clone, as we're going to mutate later
|
|
502
|
-
const data = { ...input.data };
|
|
503
|
-
if (hasMappedFields(input.artifacts, input.resource, data)) {
|
|
504
|
-
await mapMappedFields(tableArtifacts, data, dbCall, formatQuery);
|
|
505
|
-
}
|
|
506
|
-
if (dialect === "mysql" && tableArtifacts.dateTimeFieldsCount > 0) {
|
|
507
|
-
for (let k in tableArtifacts.dateTimeFields) {
|
|
508
|
-
if (data[k] != null) {
|
|
509
|
-
data[k] = (0, getDateTimeStringMySQL_1.getDateTimeStringMySQL)(data[k]);
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
// Delete keys with value of undefined
|
|
514
|
-
for (let k in data) {
|
|
515
|
-
if (data[k] === void 0) {
|
|
516
|
-
delete data[k];
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
// Nothing to update
|
|
520
|
-
if (Object.keys(data).length === 0) {
|
|
521
|
-
return true;
|
|
522
|
-
}
|
|
523
|
-
await dbCall(getUpdateQuery(input.resource, data, where, dialect, formatQuery));
|
|
524
|
-
return true;
|
|
525
|
-
}
|
|
526
|
-
await _update();
|
|
527
|
-
return getData(input, dbCall, formatQuery, dialect);
|
|
528
|
-
}
|
|
529
|
-
function getUpdateQuery(table, data, where, dialect, formatQuery) {
|
|
530
|
-
// Assumes `data` is not empty
|
|
531
|
-
const escapeId = (0, stringifyWhere_1.getEscapeId)(dialect);
|
|
532
|
-
const escape = (0, stringifyWhere_1.getEscape)(dialect);
|
|
533
|
-
let q = "UPDATE ?? ";
|
|
534
|
-
let values = [table];
|
|
535
|
-
let opsStrs = [];
|
|
536
|
-
let dataRegular = {};
|
|
537
|
-
for (let k in data) {
|
|
538
|
-
const v = data[k];
|
|
539
|
-
if (typeof v === "object" && v != null && Object.keys(v).length === 1) {
|
|
540
|
-
const _entries = Object.entries(v)?.[0];
|
|
541
|
-
const op = _entries?.[0];
|
|
542
|
-
const vv = _entries?.[1];
|
|
543
|
-
if (op === "$prepend") {
|
|
544
|
-
opsStrs.push(`${table}.${escapeId(k)} = CASE WHEN ${table}.${escapeId(k)} IS NULL THEN NULL ELSE CONCAT(${escape(vv)}, ${table}.${escapeId(k)}) END`);
|
|
545
|
-
continue;
|
|
546
|
-
}
|
|
547
|
-
if (op === "$append") {
|
|
548
|
-
opsStrs.push(`${table}.${escapeId(k)} = CASE WHEN ${table}.${escapeId(k)} IS NULL THEN NULL ELSE CONCAT(${table}.${escapeId(k)}, ${escape(vv)}) END`);
|
|
549
|
-
continue;
|
|
550
|
-
}
|
|
551
|
-
if (op === "$increment") {
|
|
552
|
-
opsStrs.push(`${table}.${escapeId(k)} = ${table}.${escapeId(k)} + ${escape(vv)}`);
|
|
553
|
-
continue;
|
|
554
|
-
}
|
|
555
|
-
if (op === "$decrement") {
|
|
556
|
-
opsStrs.push(`${table}.${escapeId(k)} = ${table}.${escapeId(k)} - ${escape(vv)}`);
|
|
557
|
-
continue;
|
|
558
|
-
}
|
|
559
|
-
dataRegular[k] = v;
|
|
560
|
-
}
|
|
561
|
-
dataRegular[k] = v;
|
|
562
|
-
}
|
|
563
|
-
if (opsStrs.length > 0) {
|
|
564
|
-
q += `SET ${opsStrs.join(", ")}`;
|
|
565
|
-
}
|
|
566
|
-
if (Object.keys(dataRegular).length > 0) {
|
|
567
|
-
if (opsStrs.length > 0) {
|
|
568
|
-
q += ", ?";
|
|
569
|
-
}
|
|
570
|
-
else {
|
|
571
|
-
q += "SET ?";
|
|
572
|
-
}
|
|
573
|
-
values.push(dataRegular);
|
|
574
|
-
}
|
|
575
|
-
q += ` WHERE ${where}`;
|
|
576
|
-
return formatQuery(q, values);
|
|
577
|
-
}
|
|
578
|
-
async function updateMany(input, dbCall, formatQuery, dialect) {
|
|
579
|
-
async function _updateMany() {
|
|
580
|
-
const escapeId = (0, stringifyWhere_1.getEscapeId)(dialect);
|
|
581
|
-
const tableArtifacts = input.artifacts[input.resource];
|
|
582
|
-
const where = (0, getWhere_1.getWhere)(escapeId(input.resource), input.args, dialect);
|
|
583
|
-
if (where == null) {
|
|
584
|
-
throw new Error("Null where");
|
|
585
|
-
}
|
|
586
|
-
// Shallow clone, as we're going to mutate later
|
|
587
|
-
const data = { ...input.data };
|
|
588
|
-
if (hasMappedFields(input.artifacts, input.resource, data)) {
|
|
589
|
-
await mapMappedFields(tableArtifacts, data, dbCall, formatQuery);
|
|
590
|
-
}
|
|
591
|
-
if (dialect === "mysql" && tableArtifacts.dateTimeFieldsCount > 0) {
|
|
592
|
-
for (let k in tableArtifacts.dateTimeFields) {
|
|
593
|
-
if (data[k] != null) {
|
|
594
|
-
data[k] = (0, getDateTimeStringMySQL_1.getDateTimeStringMySQL)(data[k]);
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
// Delete keys with value of undefined
|
|
599
|
-
for (let k in data) {
|
|
600
|
-
if (data[k] === void 0) {
|
|
601
|
-
delete data[k];
|
|
602
|
-
}
|
|
603
|
-
}
|
|
604
|
-
// Nothing to update
|
|
605
|
-
if (Object.keys(data).length === 0) {
|
|
606
|
-
return [];
|
|
607
|
-
}
|
|
608
|
-
await dbCall(getUpdateQuery(input.resource, data, where, dialect, formatQuery));
|
|
609
|
-
return true;
|
|
610
|
-
}
|
|
611
|
-
await _updateMany();
|
|
612
|
-
return getData(input, dbCall, formatQuery, dialect);
|
|
613
|
-
}
|
|
614
|
-
async function del(input, dbCall, formatQuery, dialect) {
|
|
615
|
-
const _findOne = Object.entries(input.args.$where)[0];
|
|
616
|
-
const findOne = { key: _findOne[0], value: _findOne[1] };
|
|
617
|
-
const current = await dbCall(formatQuery("SELECT * FROM ?? WHERE ?? = ?", [
|
|
618
|
-
input.resource,
|
|
619
|
-
findOne.key,
|
|
620
|
-
findOne.value,
|
|
621
|
-
])).then((xs) => xs[0]);
|
|
622
|
-
if (current == null) {
|
|
623
|
-
throw new SDKNotFoundError_1.SDKNotFoundError();
|
|
624
|
-
}
|
|
625
|
-
await dbCall(formatQuery("DELETE FROM ?? WHERE ?? = ?", [
|
|
626
|
-
input.resource,
|
|
627
|
-
findOne.key,
|
|
628
|
-
findOne.value,
|
|
629
|
-
]));
|
|
630
|
-
return true;
|
|
631
|
-
}
|
|
632
|
-
async function deleteMany(input, dbCall, formatQuery, dialect) {
|
|
633
|
-
const escapeId = (0, stringifyWhere_1.getEscapeId)(dialect);
|
|
634
|
-
const where = (0, getWhere_1.getWhere)(escapeId(input.resource), input.args, dialect);
|
|
635
|
-
if (where == null) {
|
|
636
|
-
throw new Error("Null where");
|
|
637
|
-
}
|
|
638
|
-
const sql = "DELETE FROM ?? WHERE " + where;
|
|
639
|
-
const values = [input.resource];
|
|
640
|
-
await dbCall(formatQuery(sql, values));
|
|
641
|
-
return true;
|
|
642
|
-
}
|
|
65
|
+
exports.getTotalCount = getTotalCount;
|
|
643
66
|
function getScalarFields(table, artifacts) {
|
|
644
67
|
return artifacts[table].scalarFields;
|
|
645
68
|
}
|
|
69
|
+
exports.getScalarFields = getScalarFields;
|
|
646
70
|
function hasMappedFields(artifacts, table, data) {
|
|
647
|
-
const mappedFields = artifacts[table]
|
|
71
|
+
const { mappedFields } = artifacts[table];
|
|
648
72
|
if (mappedFields == null) {
|
|
649
73
|
return false;
|
|
650
74
|
}
|
|
@@ -652,6 +76,7 @@ function hasMappedFields(artifacts, table, data) {
|
|
|
652
76
|
const dataKeys = Object.keys(data);
|
|
653
77
|
return dataKeys.some((k) => keys.has(k));
|
|
654
78
|
}
|
|
79
|
+
exports.hasMappedFields = hasMappedFields;
|
|
655
80
|
async function mapMappedFields(artifactsForTable, data, dbCall, formatQuery) {
|
|
656
81
|
for (let k in data) {
|
|
657
82
|
const v = data[k];
|
|
@@ -681,7 +106,7 @@ async function mapMappedFields(artifactsForTable, data, dbCall, formatQuery) {
|
|
|
681
106
|
w,
|
|
682
107
|
])).then((xs) => xs[0]?.[mappedField.referencedKey]);
|
|
683
108
|
if (result == null) {
|
|
684
|
-
throw new
|
|
109
|
+
throw new error_1.SDKNotFoundError();
|
|
685
110
|
}
|
|
686
111
|
return result;
|
|
687
112
|
}));
|
|
@@ -695,13 +120,14 @@ async function mapMappedFields(artifactsForTable, data, dbCall, formatQuery) {
|
|
|
695
120
|
v,
|
|
696
121
|
])).then((xs) => xs[0]?.[mappedField.referencedKey]);
|
|
697
122
|
if (result == null) {
|
|
698
|
-
throw new
|
|
123
|
+
throw new error_1.SDKNotFoundError();
|
|
699
124
|
}
|
|
700
125
|
data[mappedField.foreignKey] = result;
|
|
701
126
|
delete data[k];
|
|
702
127
|
}
|
|
703
128
|
}
|
|
704
129
|
}
|
|
130
|
+
exports.mapMappedFields = mapMappedFields;
|
|
705
131
|
// 1. Remove additional keys added to data for batches and joins
|
|
706
132
|
// 2. Execute `transform` functions if they exist
|
|
707
133
|
function postProcess(data, fields, shouldRemoveExtraKeys) {
|
|
@@ -715,7 +141,7 @@ function removeExtraKeys(data, fields) {
|
|
|
715
141
|
if (data == null || (Array.isArray(data) && data[0] == null)) {
|
|
716
142
|
return;
|
|
717
143
|
}
|
|
718
|
-
|
|
144
|
+
const fieldKeys = [];
|
|
719
145
|
for (let x of fields) {
|
|
720
146
|
if (typeof x === "string") {
|
|
721
147
|
fieldKeys.push(x);
|
|
@@ -752,159 +178,3 @@ function removeExtraKeys(data, fields) {
|
|
|
752
178
|
}
|
|
753
179
|
}
|
|
754
180
|
}
|
|
755
|
-
function whereNeedsProcessing(where) {
|
|
756
|
-
return JSON.stringify(where).includes("Uuid");
|
|
757
|
-
}
|
|
758
|
-
exports.whereNeedsProcessing = whereNeedsProcessing;
|
|
759
|
-
async function _prepareWhere(artifacts, table, data, dbCall, formatQuery) {
|
|
760
|
-
const mappedFields = artifacts[table].mappedFields;
|
|
761
|
-
let out = {};
|
|
762
|
-
await traverseWhere(data, async (where, ptr, parentPtr, root) => {
|
|
763
|
-
const path = ptr.split("/").slice(1);
|
|
764
|
-
const opIndex = path.length - 1;
|
|
765
|
-
if (ops.includes(path[opIndex])) {
|
|
766
|
-
const newPath = path.slice();
|
|
767
|
-
const index = newPath.length - 2;
|
|
768
|
-
const key = newPath[index];
|
|
769
|
-
const mappedField = mappedFields?.[key];
|
|
770
|
-
if (mappedField != null) {
|
|
771
|
-
newPath[index] = mappedField.foreignKey;
|
|
772
|
-
if (Array.isArray(where)) {
|
|
773
|
-
const newVal = await Promise.all(where.map((v) => dbCall(formatQuery("SELECT ?? FROM ?? WHERE ?? = ?", [
|
|
774
|
-
mappedField.referencedKey,
|
|
775
|
-
mappedField.referencedTable,
|
|
776
|
-
mappedField.name,
|
|
777
|
-
v,
|
|
778
|
-
])).then((xs) => xs[0]?.[mappedField.referencedKey])));
|
|
779
|
-
if (newVal.some((x) => x == null)) {
|
|
780
|
-
const index = newVal.findIndex((x) => x == null);
|
|
781
|
-
if (index === -1) {
|
|
782
|
-
throw new SDKBadWhereError_1.SDKBadWhereError();
|
|
783
|
-
}
|
|
784
|
-
else {
|
|
785
|
-
const v = where[index];
|
|
786
|
-
throw new SDKBadWhereError_1.SDKBadWhereError(getPrepareWhereNotFoundMessage(mappedField, v));
|
|
787
|
-
}
|
|
788
|
-
}
|
|
789
|
-
out = _.set(newPath, newVal, out);
|
|
790
|
-
}
|
|
791
|
-
else {
|
|
792
|
-
const newVal = await dbCall(formatQuery("SELECT ?? FROM ?? WHERE ?? = ?", [
|
|
793
|
-
mappedField.referencedKey,
|
|
794
|
-
mappedField.referencedTable,
|
|
795
|
-
mappedField.name,
|
|
796
|
-
where,
|
|
797
|
-
])).then((xs) => xs[0]?.[mappedField.referencedKey]);
|
|
798
|
-
if (newVal == null) {
|
|
799
|
-
throw new SDKBadWhereError_1.SDKBadWhereError(getPrepareWhereNotFoundMessage(mappedField, where));
|
|
800
|
-
}
|
|
801
|
-
out = _.set(newPath, newVal, out);
|
|
802
|
-
}
|
|
803
|
-
out = _.unset(path.slice(0, path.length - 1), out);
|
|
804
|
-
return;
|
|
805
|
-
}
|
|
806
|
-
}
|
|
807
|
-
else {
|
|
808
|
-
const key = path[path.length - 1];
|
|
809
|
-
const mappedField = mappedFields?.[key];
|
|
810
|
-
if (mappedField != null) {
|
|
811
|
-
const newPath = path
|
|
812
|
-
.slice(0, path.length - 1)
|
|
813
|
-
.concat(mappedField.foreignKey);
|
|
814
|
-
if (Array.isArray(where)) {
|
|
815
|
-
const newVal = await Promise.all(where.map((v) => dbCall(formatQuery("SELECT ?? FROM ?? WHERE ?? = ?", [
|
|
816
|
-
mappedField.referencedKey,
|
|
817
|
-
mappedField.referencedTable,
|
|
818
|
-
mappedField.name,
|
|
819
|
-
v,
|
|
820
|
-
])).then((xs) => xs[0]?.[mappedField.referencedKey])));
|
|
821
|
-
if (newVal.some((x) => x == null)) {
|
|
822
|
-
const index = newVal.findIndex((x) => x == null);
|
|
823
|
-
if (index === -1) {
|
|
824
|
-
throw new SDKBadWhereError_1.SDKBadWhereError();
|
|
825
|
-
}
|
|
826
|
-
else {
|
|
827
|
-
const v = where[index];
|
|
828
|
-
throw new SDKBadWhereError_1.SDKBadWhereError(getPrepareWhereNotFoundMessage(mappedField, v));
|
|
829
|
-
}
|
|
830
|
-
}
|
|
831
|
-
out = _.set(newPath, newVal, out);
|
|
832
|
-
}
|
|
833
|
-
else {
|
|
834
|
-
const newVal = await dbCall(formatQuery("SELECT ?? FROM ?? WHERE ?? = ?", [
|
|
835
|
-
mappedField.referencedKey,
|
|
836
|
-
mappedField.referencedTable,
|
|
837
|
-
mappedField.name,
|
|
838
|
-
where,
|
|
839
|
-
])).then((xs) => xs[0]?.[mappedField.referencedKey]);
|
|
840
|
-
if (newVal == null) {
|
|
841
|
-
throw new SDKBadWhereError_1.SDKBadWhereError(getPrepareWhereNotFoundMessage(mappedField, where));
|
|
842
|
-
}
|
|
843
|
-
out = _.set(newPath, newVal, out);
|
|
844
|
-
}
|
|
845
|
-
out = _.unset(path, out);
|
|
846
|
-
}
|
|
847
|
-
else {
|
|
848
|
-
out = path.length > 0 ? _.set(path, where, out) : where;
|
|
849
|
-
}
|
|
850
|
-
}
|
|
851
|
-
});
|
|
852
|
-
return out;
|
|
853
|
-
}
|
|
854
|
-
exports._prepareWhere = _prepareWhere;
|
|
855
|
-
function getPrepareWhereNotFoundMessage(mappedField, value) {
|
|
856
|
-
return `Not found: unable to map \`${mappedField.referencedTable}\`.\`${mappedField.name}\` to \`${mappedField.referencedTable}\`.\`${mappedField.referencedKey}\` for the value \`${value}\`.`;
|
|
857
|
-
}
|
|
858
|
-
const ops = [
|
|
859
|
-
"$eq",
|
|
860
|
-
"$neq",
|
|
861
|
-
"$gt",
|
|
862
|
-
"$gte",
|
|
863
|
-
"$lt",
|
|
864
|
-
"$lte",
|
|
865
|
-
"$in",
|
|
866
|
-
"$nin",
|
|
867
|
-
"$like",
|
|
868
|
-
"$nlike",
|
|
869
|
-
"$btwn",
|
|
870
|
-
"$nbtwn",
|
|
871
|
-
];
|
|
872
|
-
async function traverseWhere(where, cb) {
|
|
873
|
-
return await _traverseWhere(cb, where, "", where);
|
|
874
|
-
}
|
|
875
|
-
const _traverseWhere = async function (cb, where, ptr, root, parentPtr) {
|
|
876
|
-
await cb(where, ptr, parentPtr, root);
|
|
877
|
-
if (where && typeof where == "object" && !Array.isArray(where)) {
|
|
878
|
-
for (let key in where) {
|
|
879
|
-
const sch = where[key];
|
|
880
|
-
if (key === "$and" || key == "$or") {
|
|
881
|
-
await Promise.all(sch.map(async (s, i) => {
|
|
882
|
-
await _traverseWhere(cb, s, ptr + "/" + key + "/" + i, root, ptr
|
|
883
|
-
// key,
|
|
884
|
-
// where,
|
|
885
|
-
// parentPtr,
|
|
886
|
-
// i
|
|
887
|
-
);
|
|
888
|
-
}));
|
|
889
|
-
}
|
|
890
|
-
else if (typeof sch === "object") {
|
|
891
|
-
for (let prop in sch) {
|
|
892
|
-
await _traverseWhere(cb, sch[prop], ptr + "/" + key + "/" + escapePtr(prop), root, ptr
|
|
893
|
-
// key,
|
|
894
|
-
// where,
|
|
895
|
-
// prop
|
|
896
|
-
);
|
|
897
|
-
}
|
|
898
|
-
}
|
|
899
|
-
else {
|
|
900
|
-
await _traverseWhere(cb, sch, ptr + "/" + key, root, ptr
|
|
901
|
-
// key,
|
|
902
|
-
// where
|
|
903
|
-
);
|
|
904
|
-
}
|
|
905
|
-
}
|
|
906
|
-
}
|
|
907
|
-
};
|
|
908
|
-
function escapePtr(str) {
|
|
909
|
-
return str.replace(/~/g, "~0").replace(/\//g, "~1");
|
|
910
|
-
}
|