@technicity/data-service-generator 0.13.26 → 0.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/generation/generate.d.ts +0 -1
- package/dist/generation/generate.js +269 -787
- package/dist/ksql.js +1 -24
- package/dist/runtime/Cache.js +3 -6
- package/dist/runtime/IRuntime.d.ts +4 -33
- package/dist/runtime/RuntimeKSQL.d.ts +0 -7
- package/dist/runtime/RuntimeKSQL.js +17 -47
- package/dist/runtime/RuntimeMSSQL.d.ts +1 -7
- package/dist/runtime/RuntimeMSSQL.js +4 -4
- package/dist/runtime/RuntimeMySQL.d.ts +1 -3
- package/dist/runtime/RuntimeMySQL.js +7 -33
- package/dist/runtime/lib/MSSQL.d.ts +1 -2
- package/dist/runtime/lib/MSSQL.js +8 -36
- package/dist/runtime/lib/MySQL.d.ts +1 -1
- package/dist/runtime/lib/MySQL.js +2 -15
- package/dist/runtime/lib/getSqlAst.js +21 -46
- package/dist/runtime/lib/shared.js +44 -156
- package/dist/runtime/lib/stringifyWhere.js +12 -39
- package/dist/runtime/lib/typeCastMSSQL.js +1 -24
- package/package.json +4 -1
- package/dist/runtime/RuntimeSQLite.d.ts +0 -38
- package/dist/runtime/RuntimeSQLite.js +0 -135
- package/dist/runtime/lib/addNullFallbacks.test.d.ts +0 -1
- package/dist/runtime/lib/addNullFallbacks.test.js +0 -206
- package/dist/runtime/lib/runTransforms.test.d.ts +0 -1
- package/dist/runtime/lib/runTransforms.test.js +0 -112
- package/dist/runtime/lib/stringifyWhere.test.d.ts +0 -1
- package/dist/runtime/lib/stringifyWhere.test.js +0 -236
- package/dist/traverseFieldArgs.test.d.ts +0 -1
- package/dist/traverseFieldArgs.test.js +0 -72
|
@@ -1,47 +1,21 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
3
|
exports.getSqlAst = void 0;
|
|
30
|
-
const SqlString =
|
|
4
|
+
const SqlString = require("sqlstring");
|
|
31
5
|
// @ts-ignore
|
|
32
|
-
const TSqlString =
|
|
6
|
+
const TSqlString = require("tsqlstring");
|
|
33
7
|
// @ts-ignore
|
|
34
|
-
const alias_namespace_1 =
|
|
35
|
-
const _ =
|
|
8
|
+
const alias_namespace_1 = require("join-monster/dist/alias-namespace");
|
|
9
|
+
const _ = require("lodash/fp");
|
|
36
10
|
const getOrderBy_1 = require("./getOrderBy");
|
|
37
11
|
const namespace = new alias_namespace_1.default(true);
|
|
38
12
|
function getSqlAst(input) {
|
|
39
|
-
const { table, fieldName, fields, args, grabMany, sqlJoin, sqlBatch, junction, getWhere, artifacts, rowWithMatchingCursor, dialect } = input;
|
|
13
|
+
const { table, fieldName, fields, args, grabMany, sqlJoin, sqlBatch, junction, getWhere, artifacts, rowWithMatchingCursor, dialect, } = input;
|
|
40
14
|
const tableArtifacts = artifacts[table];
|
|
41
15
|
const primaryKey = tableArtifacts.primaryKey;
|
|
42
|
-
const format = dialect === "
|
|
43
|
-
?
|
|
44
|
-
:
|
|
16
|
+
const format = dialect === "mysql"
|
|
17
|
+
? SqlString.format.bind(SqlString)
|
|
18
|
+
: TSqlString.format.bind(TSqlString);
|
|
45
19
|
const orderBy = input.orderBy ?? (0, getOrderBy_1.getOrderBy)(args, primaryKey)?.orderBy;
|
|
46
20
|
let where = input.where;
|
|
47
21
|
if (input.where == null) {
|
|
@@ -100,12 +74,12 @@ function getSqlAst(input) {
|
|
|
100
74
|
referencedTableAlias,
|
|
101
75
|
referencedTableAlias,
|
|
102
76
|
mappedField.referencedKey,
|
|
103
|
-
mappedField.foreignKey
|
|
77
|
+
mappedField.foreignKey,
|
|
104
78
|
]);
|
|
105
79
|
},
|
|
106
80
|
name: x,
|
|
107
81
|
fieldName: x,
|
|
108
|
-
as: namespace.generate("column", x)
|
|
82
|
+
as: namespace.generate("column", x),
|
|
109
83
|
};
|
|
110
84
|
}
|
|
111
85
|
// TODO - validate in dev?
|
|
@@ -131,7 +105,7 @@ function getSqlAst(input) {
|
|
|
131
105
|
// Notice the duplicate names in the select. This results in missing rows because the
|
|
132
106
|
// shape definition passed into NestHydrationJS will only have 1 `id`.
|
|
133
107
|
fieldName: String(Date.now()),
|
|
134
|
-
as: String(Date.now())
|
|
108
|
+
as: String(Date.now()),
|
|
135
109
|
},
|
|
136
110
|
junction: {
|
|
137
111
|
sqlTable: relationField.junctionTable,
|
|
@@ -144,7 +118,8 @@ function getSqlAst(input) {
|
|
|
144
118
|
return undefined;
|
|
145
119
|
}
|
|
146
120
|
const argsMapped = _.cloneDeep(args);
|
|
147
|
-
argsMapped.$where =
|
|
121
|
+
argsMapped.$where =
|
|
122
|
+
argsMapped.$where[relationField.junctionTable];
|
|
148
123
|
const whereResult = getWhere(
|
|
149
124
|
// table is escaped already
|
|
150
125
|
table, argsMapped, dialect, orderBy, rowWithMatchingCursor);
|
|
@@ -168,12 +143,12 @@ function getSqlAst(input) {
|
|
|
168
143
|
sqlBatch: {
|
|
169
144
|
thisKey: columnToASTChild(relationField.relations[0].foreignKey, namespace, asJunction),
|
|
170
145
|
parentKey: columnToASTChild(relationField.relations[0].referencedKey, namespace),
|
|
171
|
-
sqlJoin: (junctionTable, t, args) => `${junctionTable}.${relationField.relations[1].foreignKey} = ${t}.${relationField.relations[1].referencedKey}
|
|
172
|
-
}
|
|
146
|
+
sqlJoin: (junctionTable, t, args) => `${junctionTable}.${relationField.relations[1].foreignKey} = ${t}.${relationField.relations[1].referencedKey}`,
|
|
147
|
+
},
|
|
173
148
|
},
|
|
174
149
|
getWhere,
|
|
175
150
|
artifacts,
|
|
176
|
-
dialect
|
|
151
|
+
dialect,
|
|
177
152
|
});
|
|
178
153
|
}
|
|
179
154
|
return getSqlAst({
|
|
@@ -199,13 +174,13 @@ function getSqlAst(input) {
|
|
|
199
174
|
// sub lists are limited, too.
|
|
200
175
|
sqlBatch: {
|
|
201
176
|
thisKey: columnToASTChild(relationField.relation.referencedKey, namespace),
|
|
202
|
-
parentKey: columnToASTChild(relationField.relation.foreignKey, namespace)
|
|
177
|
+
parentKey: columnToASTChild(relationField.relation.foreignKey, namespace),
|
|
203
178
|
},
|
|
204
179
|
getWhere,
|
|
205
180
|
artifacts,
|
|
206
|
-
dialect
|
|
181
|
+
dialect,
|
|
207
182
|
});
|
|
208
|
-
}))
|
|
183
|
+
})),
|
|
209
184
|
};
|
|
210
185
|
}
|
|
211
186
|
exports.getSqlAst = getSqlAst;
|
|
@@ -217,7 +192,7 @@ function keyToASTChild(key, namespace, fromOtherTable) {
|
|
|
217
192
|
name: key,
|
|
218
193
|
fieldName: clumsyName,
|
|
219
194
|
as: namespace.generate("column", clumsyName),
|
|
220
|
-
fromOtherTable
|
|
195
|
+
fromOtherTable,
|
|
221
196
|
};
|
|
222
197
|
}
|
|
223
198
|
return columnToASTChild(key, namespace, fromOtherTable);
|
|
@@ -228,7 +203,7 @@ function columnToASTChild(columnName, namespace, fromOtherTable) {
|
|
|
228
203
|
name: columnName,
|
|
229
204
|
fieldName: columnName,
|
|
230
205
|
as: namespace.generate("column", columnName),
|
|
231
|
-
fromOtherTable
|
|
206
|
+
fromOtherTable,
|
|
232
207
|
};
|
|
233
208
|
}
|
|
234
209
|
function toClumsyName(keyArr) {
|
|
@@ -1,30 +1,4 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
3
|
exports._prepareWhere = exports.whereNeedsProcessing = exports.postProcess = exports.MiddlewareHandler = exports.resolve = void 0;
|
|
30
4
|
// @ts-ignore
|
|
@@ -32,11 +6,11 @@ exports._prepareWhere = exports.whereNeedsProcessing = exports.postProcess = exp
|
|
|
32
6
|
// @ts-ignore
|
|
33
7
|
// import arrToConnection from "join-monster/dist/array-to-connection";
|
|
34
8
|
// @ts-ignore
|
|
35
|
-
const batch_planner_1 =
|
|
9
|
+
const batch_planner_1 = require("join-monster/dist/batch-planner");
|
|
36
10
|
// @ts-ignore
|
|
37
11
|
const util_1 = require("join-monster/dist/util");
|
|
38
12
|
const async_hooks_1 = require("async_hooks");
|
|
39
|
-
const _ =
|
|
13
|
+
const _ = require("lodash/fp");
|
|
40
14
|
const uuid_1 = require("uuid");
|
|
41
15
|
const getSqlAst_1 = require("./getSqlAst");
|
|
42
16
|
const getWhere_1 = require("./getWhere");
|
|
@@ -71,9 +45,9 @@ exports.resolve = resolve;
|
|
|
71
45
|
function _resolve(input, dbCall, formatQuery, beginTransaction, dialect, context, cache) {
|
|
72
46
|
switch (input.action) {
|
|
73
47
|
case "findMany":
|
|
74
|
-
return cache && !input.skipCache
|
|
75
|
-
|
|
76
|
-
|
|
48
|
+
return cache && !input.skipCache ?
|
|
49
|
+
getCached(input, dbCall, formatQuery, dialect, cache) :
|
|
50
|
+
getData(input, dbCall, formatQuery, dialect);
|
|
77
51
|
case "findUnique":
|
|
78
52
|
case "findManyPaginated":
|
|
79
53
|
return getData(input, dbCall, formatQuery, dialect);
|
|
@@ -142,7 +116,7 @@ async function getData(input, dbCall, formatQuery, dialect) {
|
|
|
142
116
|
rowWithMatchingCursor = await dbCall(formatQuery("SELECT * FROM ?? WHERE ?? = ?", [
|
|
143
117
|
input.resource,
|
|
144
118
|
primaryKey,
|
|
145
|
-
(0, cursor_1.decodeCursor)(cursor)
|
|
119
|
+
(0, cursor_1.decodeCursor)(cursor),
|
|
146
120
|
])).then((xs) => xs[0]);
|
|
147
121
|
if (rowWithMatchingCursor == null) {
|
|
148
122
|
throw new Error(`Invalid cursor: ${cursor}`);
|
|
@@ -166,9 +140,9 @@ async function getData(input, dbCall, formatQuery, dialect) {
|
|
|
166
140
|
dialect === "mssql" && paginationType === "limit-offset"
|
|
167
141
|
? {
|
|
168
142
|
orderBy: (0, getOrderBy_1.getOrderBy)(input.args, primaryKey)?.orderBy ?? [
|
|
169
|
-
{ column: primaryKey, direction: "asc" }
|
|
143
|
+
{ column: primaryKey, direction: "asc" },
|
|
170
144
|
],
|
|
171
|
-
flip: false
|
|
145
|
+
flip: false,
|
|
172
146
|
}
|
|
173
147
|
: action === "findManyPaginated"
|
|
174
148
|
? (0, getOrderBy_1.getOrderBy)(input.args, primaryKey)
|
|
@@ -186,9 +160,9 @@ async function getData(input, dbCall, formatQuery, dialect) {
|
|
|
186
160
|
orderBy: orderByListPaginatedRootResult?.orderBy,
|
|
187
161
|
rowWithMatchingCursor,
|
|
188
162
|
dialect,
|
|
189
|
-
grabMany
|
|
163
|
+
grabMany,
|
|
190
164
|
});
|
|
191
|
-
const options = { dialect
|
|
165
|
+
const options = { dialect };
|
|
192
166
|
let { sql, shapeDefinition } = await (0, util_1.compileSqlAST)(sqlAST, context, options);
|
|
193
167
|
if (!sql) {
|
|
194
168
|
// return {};
|
|
@@ -231,9 +205,6 @@ async function getData(input, dbCall, formatQuery, dialect) {
|
|
|
231
205
|
// Do later for `listPaginated`, since the `id` is needed for
|
|
232
206
|
// creating the cursor
|
|
233
207
|
postProcess(data, fields, shouldRemoveExtraKeys);
|
|
234
|
-
if (dialect === "sqlite") {
|
|
235
|
-
typeCastSqlite(data, fields, input.resource, input.artifacts);
|
|
236
|
-
}
|
|
237
208
|
}
|
|
238
209
|
// check for batch data
|
|
239
210
|
if (Array.isArray(data)) {
|
|
@@ -272,7 +243,7 @@ async function getData(input, dbCall, formatQuery, dialect) {
|
|
|
272
243
|
// We don't want the where clause to include cursor-related stuff
|
|
273
244
|
rowWithMatchingCursor: null,
|
|
274
245
|
dialect,
|
|
275
|
-
grabMany: true
|
|
246
|
+
grabMany: true,
|
|
276
247
|
});
|
|
277
248
|
// Because orderBy doesn't matter for total count.
|
|
278
249
|
// getOrderBy adds an element if paginating, so deleting args.$orderBy
|
|
@@ -282,16 +253,10 @@ async function getData(input, dbCall, formatQuery, dialect) {
|
|
|
282
253
|
if (paginationType === "cursor") {
|
|
283
254
|
data = wrapListPaginationCursor(data, input.args, orderByListPaginatedRootResult.flip, (xs) => {
|
|
284
255
|
postProcess(xs, fields, shouldRemoveExtraKeys);
|
|
285
|
-
if (dialect === "sqlite") {
|
|
286
|
-
typeCastSqlite(data, fields, input.resource, input.artifacts);
|
|
287
|
-
}
|
|
288
256
|
}, input.artifacts[input.resource].primaryKey, totalCount);
|
|
289
257
|
}
|
|
290
258
|
else {
|
|
291
259
|
postProcess(data, fields, shouldRemoveExtraKeys);
|
|
292
|
-
if (dialect === "sqlite") {
|
|
293
|
-
typeCastSqlite(data, fields, input.resource, input.artifacts);
|
|
294
|
-
}
|
|
295
260
|
data = wrapListPaginationLimitOffset(data, totalCount);
|
|
296
261
|
}
|
|
297
262
|
}
|
|
@@ -388,9 +353,9 @@ function wrapListPaginationCursor(data, args, flip, cb, primaryKey, totalCount)
|
|
|
388
353
|
hasNextPage,
|
|
389
354
|
startCursor,
|
|
390
355
|
endCursor,
|
|
391
|
-
totalCount
|
|
356
|
+
totalCount,
|
|
392
357
|
},
|
|
393
|
-
results: data
|
|
358
|
+
results: data,
|
|
394
359
|
};
|
|
395
360
|
}
|
|
396
361
|
async function getTotalCount(sqlASTTotalCount, dbCall, context, options) {
|
|
@@ -401,8 +366,8 @@ async function getTotalCount(sqlASTTotalCount, dbCall, context, options) {
|
|
|
401
366
|
type: "expression",
|
|
402
367
|
sqlExpr: () => "COUNT(*)",
|
|
403
368
|
fieldName: fieldNameTotalCount,
|
|
404
|
-
as: fieldNameTotalCount
|
|
405
|
-
}
|
|
369
|
+
as: fieldNameTotalCount,
|
|
370
|
+
},
|
|
406
371
|
];
|
|
407
372
|
const { sql: sqlTotalCount } = await (0, util_1.compileSqlAST)(sqlASTTotalCount, context, options);
|
|
408
373
|
const totalCount = await dbCall(sqlTotalCount).then((xs) => xs[0][fieldNameTotalCount]);
|
|
@@ -477,21 +442,15 @@ async function create(input, dbCall, formatQuery, beginTransaction, dialect, con
|
|
|
477
442
|
}
|
|
478
443
|
else {
|
|
479
444
|
data = processCreateData(data, tableArtifacts, dialect, context?.specialCaseUuidColumn);
|
|
480
|
-
if (dialect === "
|
|
481
|
-
const
|
|
482
|
-
|
|
483
|
-
const inserted = await dbCall(formatQuery(`INSERT INTO ?? (??) VALUES (?) SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]`, [input.resource, columns, values]));
|
|
484
|
-
return inserted[0]["SCOPE_IDENTITY"];
|
|
445
|
+
if (dialect === "mysql") {
|
|
446
|
+
const inserted = await dbCall(formatQuery("INSERT INTO ?? SET ?", [input.resource, data]));
|
|
447
|
+
return inserted.insertId;
|
|
485
448
|
}
|
|
486
449
|
else {
|
|
487
450
|
const columns = Object.keys(data);
|
|
488
451
|
const values = Object.values(data);
|
|
489
|
-
const inserted = await dbCall(formatQuery(`INSERT INTO ?? (??) VALUES (?)`, [
|
|
490
|
-
|
|
491
|
-
columns,
|
|
492
|
-
values
|
|
493
|
-
]));
|
|
494
|
-
return inserted.insertId;
|
|
452
|
+
const inserted = await dbCall(formatQuery(`INSERT INTO ?? (??) VALUES (?) SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]`, [input.resource, columns, values]));
|
|
453
|
+
return inserted[0]["SCOPE_IDENTITY"];
|
|
495
454
|
}
|
|
496
455
|
}
|
|
497
456
|
}
|
|
@@ -549,7 +508,7 @@ function getCreateTree(data, table, referencedKey, specialCaseUuidColumn, dialec
|
|
|
549
508
|
referencedKey,
|
|
550
509
|
columns: [],
|
|
551
510
|
values: [],
|
|
552
|
-
children: []
|
|
511
|
+
children: [],
|
|
553
512
|
};
|
|
554
513
|
for (let i = 0; i < data.length; i++) {
|
|
555
514
|
let d = data[i];
|
|
@@ -638,19 +597,19 @@ function getUpdateQuery(table, data, where, dialect, formatQuery) {
|
|
|
638
597
|
const op = _entries?.[0];
|
|
639
598
|
const vv = _entries?.[1];
|
|
640
599
|
if (op === "$prepend") {
|
|
641
|
-
opsStrs.push(`${escapeId(k)} = CASE WHEN ${escapeId(k)} IS NULL THEN NULL ELSE ${
|
|
600
|
+
opsStrs.push(`${table}.${escapeId(k)} = CASE WHEN ${table}.${escapeId(k)} IS NULL THEN NULL ELSE CONCAT(${escape(vv)}, ${table}.${escapeId(k)}) END`);
|
|
642
601
|
continue;
|
|
643
602
|
}
|
|
644
603
|
if (op === "$append") {
|
|
645
|
-
opsStrs.push(`${escapeId(k)} = CASE WHEN ${escapeId(k)} IS NULL THEN NULL ELSE ${
|
|
604
|
+
opsStrs.push(`${table}.${escapeId(k)} = CASE WHEN ${table}.${escapeId(k)} IS NULL THEN NULL ELSE CONCAT(${table}.${escapeId(k)}, ${escape(vv)}) END`);
|
|
646
605
|
continue;
|
|
647
606
|
}
|
|
648
607
|
if (op === "$increment") {
|
|
649
|
-
opsStrs.push(`${escapeId(k)} = ${escapeId(k)} + ${escape(vv)}`);
|
|
608
|
+
opsStrs.push(`${table}.${escapeId(k)} = ${table}.${escapeId(k)} + ${escape(vv)}`);
|
|
650
609
|
continue;
|
|
651
610
|
}
|
|
652
611
|
if (op === "$decrement") {
|
|
653
|
-
opsStrs.push(`${escapeId(k)} = ${escapeId(k)} - ${escape(vv)}`);
|
|
612
|
+
opsStrs.push(`${table}.${escapeId(k)} = ${table}.${escapeId(k)} - ${escape(vv)}`);
|
|
654
613
|
continue;
|
|
655
614
|
}
|
|
656
615
|
dataRegular[k] = v;
|
|
@@ -660,28 +619,18 @@ function getUpdateQuery(table, data, where, dialect, formatQuery) {
|
|
|
660
619
|
if (opsStrs.length > 0) {
|
|
661
620
|
q += `SET ${opsStrs.join(", ")}`;
|
|
662
621
|
}
|
|
663
|
-
|
|
664
|
-
if (columns.length > 0) {
|
|
622
|
+
if (Object.keys(dataRegular).length > 0) {
|
|
665
623
|
if (opsStrs.length > 0) {
|
|
666
|
-
q += ", ";
|
|
624
|
+
q += ", ?";
|
|
667
625
|
}
|
|
668
626
|
else {
|
|
669
|
-
q += "SET ";
|
|
670
|
-
}
|
|
671
|
-
q += columns.map((x) => `${escapeId(x)} = ?`).join(", ");
|
|
672
|
-
for (let k in dataRegular) {
|
|
673
|
-
values.push(dataRegular[k]);
|
|
627
|
+
q += "SET ?";
|
|
674
628
|
}
|
|
629
|
+
values.push(dataRegular);
|
|
675
630
|
}
|
|
676
631
|
q += ` WHERE ${where}`;
|
|
677
632
|
return formatQuery(q, values);
|
|
678
633
|
}
|
|
679
|
-
function stringifyConcat(operand1, operand2, dialect) {
|
|
680
|
-
if (dialect !== "sqlite") {
|
|
681
|
-
return `CONCAT(${operand1}, ${operand2})`;
|
|
682
|
-
}
|
|
683
|
-
return `(${operand1} || ${operand2})`;
|
|
684
|
-
}
|
|
685
634
|
async function updateMany(input, dbCall, formatQuery, dialect, cache) {
|
|
686
635
|
async function _updateMany() {
|
|
687
636
|
const escapeId = (0, stringifyWhere_1.getEscapeId)(dialect);
|
|
@@ -692,9 +641,7 @@ async function updateMany(input, dbCall, formatQuery, dialect, cache) {
|
|
|
692
641
|
}
|
|
693
642
|
if (cache)
|
|
694
643
|
try {
|
|
695
|
-
const query = formatQuery(`SELECT uuid FROM ?? WHERE ${where}`, [
|
|
696
|
-
input.resource
|
|
697
|
-
]);
|
|
644
|
+
const query = formatQuery(`SELECT uuid FROM ?? WHERE ${where}`, [input.resource]);
|
|
698
645
|
const matches = await dbCall(query);
|
|
699
646
|
const uuids = matches.map((x) => x.uuid);
|
|
700
647
|
cache.purge(...uuids);
|
|
@@ -734,7 +681,7 @@ async function deleteOne(input, dbCall, formatQuery, dialect, cache) {
|
|
|
734
681
|
const current = await dbCall(formatQuery("SELECT * FROM ?? WHERE ?? = ?", [
|
|
735
682
|
input.resource,
|
|
736
683
|
findOne.key,
|
|
737
|
-
findOne.value
|
|
684
|
+
findOne.value,
|
|
738
685
|
])).then((xs) => xs[0]);
|
|
739
686
|
if (current == null) {
|
|
740
687
|
throw new SDKNotFoundError_1.SDKNotFoundError();
|
|
@@ -744,7 +691,7 @@ async function deleteOne(input, dbCall, formatQuery, dialect, cache) {
|
|
|
744
691
|
await dbCall(formatQuery("DELETE FROM ?? WHERE ?? = ?", [
|
|
745
692
|
input.resource,
|
|
746
693
|
findOne.key,
|
|
747
|
-
findOne.value
|
|
694
|
+
findOne.value,
|
|
748
695
|
]));
|
|
749
696
|
return true;
|
|
750
697
|
}
|
|
@@ -756,9 +703,7 @@ async function deleteMany(input, dbCall, formatQuery, dialect, cache) {
|
|
|
756
703
|
}
|
|
757
704
|
if (cache)
|
|
758
705
|
try {
|
|
759
|
-
const query = formatQuery(`SELECT uuid FROM ?? WHERE ${where}`, [
|
|
760
|
-
input.resource
|
|
761
|
-
]);
|
|
706
|
+
const query = formatQuery(`SELECT uuid FROM ?? WHERE ${where}`, [input.resource]);
|
|
762
707
|
const matches = await dbCall(query);
|
|
763
708
|
const uuids = matches.map((x) => x.uuid);
|
|
764
709
|
cache.purge(...uuids);
|
|
@@ -807,7 +752,7 @@ async function mapMappedFields(artifactsForTable, data, dbCall, formatQuery) {
|
|
|
807
752
|
mappedField.referencedKey,
|
|
808
753
|
mappedField.referencedTable,
|
|
809
754
|
mappedField.name,
|
|
810
|
-
w
|
|
755
|
+
w,
|
|
811
756
|
])).then((xs) => xs[0]?.[mappedField.referencedKey]);
|
|
812
757
|
if (result == null) {
|
|
813
758
|
throw new SDKNotFoundError_1.SDKNotFoundError();
|
|
@@ -821,7 +766,7 @@ async function mapMappedFields(artifactsForTable, data, dbCall, formatQuery) {
|
|
|
821
766
|
mappedField.referencedKey,
|
|
822
767
|
mappedField.referencedTable,
|
|
823
768
|
mappedField.name,
|
|
824
|
-
v
|
|
769
|
+
v,
|
|
825
770
|
])).then((xs) => xs[0]?.[mappedField.referencedKey]);
|
|
826
771
|
if (result == null) {
|
|
827
772
|
throw new SDKNotFoundError_1.SDKNotFoundError();
|
|
@@ -853,7 +798,9 @@ function removeExtraKeys(data, fields) {
|
|
|
853
798
|
fieldKeys.push(x.as ?? x.name);
|
|
854
799
|
}
|
|
855
800
|
}
|
|
856
|
-
const dataKeys = Array.isArray(data)
|
|
801
|
+
const dataKeys = Array.isArray(data)
|
|
802
|
+
? Object.keys(data[0])
|
|
803
|
+
: Object.keys(data);
|
|
857
804
|
const extraDataKeys = _.difference(dataKeys, fieldKeys);
|
|
858
805
|
for (let k of extraDataKeys) {
|
|
859
806
|
if (Array.isArray(data)) {
|
|
@@ -867,7 +814,7 @@ function removeExtraKeys(data, fields) {
|
|
|
867
814
|
}
|
|
868
815
|
for (let x of fields) {
|
|
869
816
|
if (typeof x === "object") {
|
|
870
|
-
const k = x.as ?? x.name;
|
|
817
|
+
const k = typeof x === "string" ? x : x.as ?? x.name;
|
|
871
818
|
if (Array.isArray(data)) {
|
|
872
819
|
for (let d of data) {
|
|
873
820
|
removeExtraKeys(d[k], x.fields);
|
|
@@ -879,65 +826,6 @@ function removeExtraKeys(data, fields) {
|
|
|
879
826
|
}
|
|
880
827
|
}
|
|
881
828
|
}
|
|
882
|
-
const getTypeCastMap = _.memoize(function getTypeCastMap(artifacts) {
|
|
883
|
-
const typeCastMap = new Map();
|
|
884
|
-
for (let table in artifacts) {
|
|
885
|
-
let booleanColumns = new Set();
|
|
886
|
-
for (let field of artifacts[table].fields) {
|
|
887
|
-
if (field.kind === "scalar" && field.type === "boolean") {
|
|
888
|
-
booleanColumns.add(field.name);
|
|
889
|
-
}
|
|
890
|
-
}
|
|
891
|
-
typeCastMap.set(table, { boolean: booleanColumns });
|
|
892
|
-
}
|
|
893
|
-
return typeCastMap;
|
|
894
|
-
});
|
|
895
|
-
function typeCastSqlite(data, fields, table, artifacts) {
|
|
896
|
-
if (data == null || (Array.isArray(data) && data[0] == null)) {
|
|
897
|
-
return;
|
|
898
|
-
}
|
|
899
|
-
const typeCastMap = getTypeCastMap(artifacts);
|
|
900
|
-
const booleanColumns = typeCastMap.get(table)?.boolean;
|
|
901
|
-
if (booleanColumns == null) {
|
|
902
|
-
throw new Error(`Failed to resolve typeCastMap for table \`${table}\``);
|
|
903
|
-
}
|
|
904
|
-
if (booleanColumns.size > 0) {
|
|
905
|
-
for (let field of fields) {
|
|
906
|
-
const name = typeof field === "string" ? field : field.name;
|
|
907
|
-
const as = typeof field === "string" ? undefined : field.as;
|
|
908
|
-
if (booleanColumns.has(name)) {
|
|
909
|
-
const k = as ?? name;
|
|
910
|
-
if (Array.isArray(data)) {
|
|
911
|
-
for (let d of data) {
|
|
912
|
-
d[k] = !!d[k];
|
|
913
|
-
}
|
|
914
|
-
}
|
|
915
|
-
else {
|
|
916
|
-
data[k] = !!data[k];
|
|
917
|
-
}
|
|
918
|
-
}
|
|
919
|
-
}
|
|
920
|
-
}
|
|
921
|
-
const tableArtifacts = artifacts[table];
|
|
922
|
-
for (let x of fields) {
|
|
923
|
-
if (typeof x === "object") {
|
|
924
|
-
const name = x.name;
|
|
925
|
-
const relationFields = tableArtifacts.relationFields[name];
|
|
926
|
-
if (relationFields == null) {
|
|
927
|
-
throw new Error(`Failed to resolve relationFields for field \`${name}\``);
|
|
928
|
-
}
|
|
929
|
-
const k = x.as ?? name;
|
|
930
|
-
if (Array.isArray(data)) {
|
|
931
|
-
for (let d of data) {
|
|
932
|
-
typeCastSqlite(d[k], x.fields, relationFields.table, artifacts);
|
|
933
|
-
}
|
|
934
|
-
}
|
|
935
|
-
else {
|
|
936
|
-
typeCastSqlite(data[k], x.fields, relationFields.table, artifacts);
|
|
937
|
-
}
|
|
938
|
-
}
|
|
939
|
-
}
|
|
940
|
-
}
|
|
941
829
|
function whereNeedsProcessing(where) {
|
|
942
830
|
return JSON.stringify(where).includes("Uuid");
|
|
943
831
|
}
|
|
@@ -960,7 +848,7 @@ async function _prepareWhere(artifacts, table, data, dbCall, formatQuery) {
|
|
|
960
848
|
mappedField.referencedKey,
|
|
961
849
|
mappedField.referencedTable,
|
|
962
850
|
mappedField.name,
|
|
963
|
-
v
|
|
851
|
+
v,
|
|
964
852
|
])).then((xs) => xs[0]?.[mappedField.referencedKey])));
|
|
965
853
|
if (newVal.some((x) => x == null)) {
|
|
966
854
|
const index = newVal.findIndex((x) => x == null);
|
|
@@ -979,7 +867,7 @@ async function _prepareWhere(artifacts, table, data, dbCall, formatQuery) {
|
|
|
979
867
|
mappedField.referencedKey,
|
|
980
868
|
mappedField.referencedTable,
|
|
981
869
|
mappedField.name,
|
|
982
|
-
where
|
|
870
|
+
where,
|
|
983
871
|
])).then((xs) => xs[0]?.[mappedField.referencedKey]);
|
|
984
872
|
if (newVal == null) {
|
|
985
873
|
throw new SDKBadWhereError_1.SDKBadWhereError(getPrepareWhereNotFoundMessage(mappedField, where));
|
|
@@ -1002,7 +890,7 @@ async function _prepareWhere(artifacts, table, data, dbCall, formatQuery) {
|
|
|
1002
890
|
mappedField.referencedKey,
|
|
1003
891
|
mappedField.referencedTable,
|
|
1004
892
|
mappedField.name,
|
|
1005
|
-
v
|
|
893
|
+
v,
|
|
1006
894
|
])).then((xs) => xs[0]?.[mappedField.referencedKey])));
|
|
1007
895
|
if (newVal.some((x) => x == null)) {
|
|
1008
896
|
const index = newVal.findIndex((x) => x == null);
|
|
@@ -1021,7 +909,7 @@ async function _prepareWhere(artifacts, table, data, dbCall, formatQuery) {
|
|
|
1021
909
|
mappedField.referencedKey,
|
|
1022
910
|
mappedField.referencedTable,
|
|
1023
911
|
mappedField.name,
|
|
1024
|
-
where
|
|
912
|
+
where,
|
|
1025
913
|
])).then((xs) => xs[0]?.[mappedField.referencedKey]);
|
|
1026
914
|
if (newVal == null) {
|
|
1027
915
|
throw new SDKBadWhereError_1.SDKBadWhereError(getPrepareWhereNotFoundMessage(mappedField, where));
|
|
@@ -1053,7 +941,7 @@ const ops = [
|
|
|
1053
941
|
"$like",
|
|
1054
942
|
"$nlike",
|
|
1055
943
|
"$btwn",
|
|
1056
|
-
"$nbtwn"
|
|
944
|
+
"$nbtwn",
|
|
1057
945
|
];
|
|
1058
946
|
async function traverseWhere(where, cb) {
|
|
1059
947
|
return await _traverseWhere(cb, where, "", where);
|
|
@@ -1,35 +1,10 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
3
|
exports.getEscape = exports.getEscapeId = exports.stringifyWhere = void 0;
|
|
27
|
-
const _ =
|
|
28
|
-
const MySqlString =
|
|
29
|
-
// @ts-
|
|
30
|
-
const TSqlString =
|
|
31
|
-
// @ts-expect-error
|
|
32
|
-
const SqliteString = __importStar(require("sqlstring-sqlite"));
|
|
4
|
+
const _ = require("lodash/fp");
|
|
5
|
+
const MySqlString = require("sqlstring");
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
const TSqlString = require("tsqlstring");
|
|
33
8
|
function stringifyWhere(input) {
|
|
34
9
|
const { where, table, dialect, args, orderBy, rowWithMatchingCursor } = input;
|
|
35
10
|
const escapeId = getEscapeId(dialect);
|
|
@@ -41,11 +16,13 @@ function stringifyWhere(input) {
|
|
|
41
16
|
orderBy,
|
|
42
17
|
escapeId,
|
|
43
18
|
escape,
|
|
44
|
-
rowWithMatchingCursor
|
|
19
|
+
rowWithMatchingCursor,
|
|
45
20
|
});
|
|
46
21
|
if (paginationWhere) {
|
|
47
22
|
result =
|
|
48
|
-
result.length === 0
|
|
23
|
+
result.length === 0
|
|
24
|
+
? paginationWhere
|
|
25
|
+
: result + " AND " + paginationWhere;
|
|
49
26
|
}
|
|
50
27
|
return result;
|
|
51
28
|
}
|
|
@@ -107,7 +84,9 @@ function _stringifyWhere(where, table, escapeId, escape, result) {
|
|
|
107
84
|
if (v === null) {
|
|
108
85
|
return `${table}.${escapeId(k)} IS NULL`;
|
|
109
86
|
}
|
|
110
|
-
if (typeof v === "string" ||
|
|
87
|
+
if (typeof v === "string" ||
|
|
88
|
+
typeof v === "number" ||
|
|
89
|
+
typeof v === "boolean") {
|
|
111
90
|
return `${table}.${escapeId(k)} = ${printValue(v)}`;
|
|
112
91
|
}
|
|
113
92
|
if (_.isPlainObject(v)) {
|
|
@@ -117,7 +96,7 @@ function _stringifyWhere(where, table, escapeId, escape, result) {
|
|
|
117
96
|
}
|
|
118
97
|
const operator = keys[0];
|
|
119
98
|
const operand = v[operator];
|
|
120
|
-
if (operator === "$
|
|
99
|
+
if (operator === "$ne") {
|
|
121
100
|
return `${table}.${escapeId(k)} ${getOperatorNeq(v)} ${printValue(operand)}`;
|
|
122
101
|
}
|
|
123
102
|
if (operator === "$gt") {
|
|
@@ -234,9 +213,6 @@ function getEscapeId(dialect) {
|
|
|
234
213
|
if (dialect === "mssql") {
|
|
235
214
|
return TSqlString.escapeId.bind(TSqlString);
|
|
236
215
|
}
|
|
237
|
-
if (dialect === "sqlite") {
|
|
238
|
-
return SqliteString.escapeId.bind(SqliteString);
|
|
239
|
-
}
|
|
240
216
|
throw new Error("Unsupported dialect: " + dialect);
|
|
241
217
|
}
|
|
242
218
|
exports.getEscapeId = getEscapeId;
|
|
@@ -247,9 +223,6 @@ function getEscape(dialect) {
|
|
|
247
223
|
if (dialect === "mssql") {
|
|
248
224
|
return TSqlString.escape.bind(TSqlString);
|
|
249
225
|
}
|
|
250
|
-
if (dialect === "sqlite") {
|
|
251
|
-
return SqliteString.escape.bind(SqliteString);
|
|
252
|
-
}
|
|
253
226
|
throw new Error("Unsupported dialect: " + dialect);
|
|
254
227
|
}
|
|
255
228
|
exports.getEscape = getEscape;
|