@technicity/data-service-generator 0.2.8 → 0.4.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/CustomError.d.ts +3 -0
- package/dist/SDKNotFoundError.d.ts +4 -0
- package/dist/addNullFallbacks.d.ts +1 -0
- package/dist/addNullFallbacks.js +3 -0
- package/dist/cursor.d.ts +2 -0
- package/dist/generate.d.ts +20 -0
- package/dist/generate.js +100 -111
- package/dist/getDateTimeStringMySQL.d.ts +1 -0
- package/dist/getDuplicates.d.ts +1 -0
- package/dist/getOrderBy.d.ts +5 -0
- package/dist/getOrderBy.js +54 -0
- package/dist/getSqlAst.d.ts +2 -0
- package/dist/getSqlAst.js +4 -34
- package/dist/getWhere.d.ts +2 -0
- package/dist/getWhere.js +1 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -1
- package/dist/isNotNullOrUndefined.d.ts +1 -0
- package/dist/ksql.d.ts +9 -0
- package/dist/ksql.js +53 -0
- package/dist/mysql.d.ts +2 -0
- package/dist/mysql.js +0 -3
- package/dist/runTransforms.d.ts +2 -0
- package/dist/runtime/IRuntime.d.ts +47 -0
- package/dist/{IDialect.js → runtime/IRuntime.js} +0 -0
- package/dist/runtime/RuntimeKSQL.d.ts +19 -0
- package/dist/runtime/RuntimeKSQL.js +128 -0
- package/dist/runtime/RuntimeMSSQL.d.ts +21 -0
- package/dist/runtime/RuntimeMSSQL.js +88 -0
- package/dist/runtime/RuntimeMySQL.d.ts +20 -0
- package/dist/runtime/RuntimeMySQL.js +117 -0
- package/dist/runtime/__MSSQL.d.ts +7 -0
- package/dist/runtime/__MSSQL.js +46 -0
- package/dist/runtime/__shared.d.ts +32 -0
- package/dist/{runtime.js → runtime/__shared.js} +213 -230
- package/dist/runtime/__typeCastMSSQL.d.ts +3 -0
- package/dist/runtime/__typeCastMSSQL.js +40 -0
- package/dist/stringifyWhere.d.ts +18 -0
- package/dist/stringifyWhere.js +41 -50
- package/dist/types.d.ts +142 -0
- package/dist/types.js +2 -0
- package/package.json +1 -1
- package/dist/driverOpts.js +0 -54
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports._prepareWhere = exports.whereNeedsProcessing = exports.
|
|
3
|
+
exports._prepareWhere = exports.whereNeedsProcessing = exports.deleteList = exports.del = exports.patchList = exports.patch = exports.post = exports.resolve = void 0;
|
|
4
4
|
// @ts-ignore
|
|
5
5
|
// import * as queryAST from "join-monster/dist/query-ast-to-sql-ast";
|
|
6
6
|
// @ts-ignore
|
|
@@ -9,27 +9,22 @@ exports._prepareWhere = exports.whereNeedsProcessing = exports.init = exports.de
|
|
|
9
9
|
const batch_planner_1 = require("join-monster/dist/batch-planner");
|
|
10
10
|
// @ts-ignore
|
|
11
11
|
const util_1 = require("join-monster/dist/util");
|
|
12
|
-
const SqlString = require("sqlstring");
|
|
13
|
-
// @ts-ignore
|
|
14
|
-
const TSqlString = require("tsqlstring");
|
|
15
12
|
const _ = require("lodash/fp");
|
|
16
|
-
const getSqlAst_1 = require("
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
const driverOpts_1 = require("./driverOpts");
|
|
27
|
-
async function resolve(input, options) {
|
|
13
|
+
const getSqlAst_1 = require("../getSqlAst");
|
|
14
|
+
const getWhere_1 = require("../getWhere");
|
|
15
|
+
const getDateTimeStringMySQL_1 = require("../getDateTimeStringMySQL");
|
|
16
|
+
const cursor_1 = require("../cursor");
|
|
17
|
+
const runTransforms_1 = require("../runTransforms");
|
|
18
|
+
const addNullFallbacks_1 = require("../addNullFallbacks");
|
|
19
|
+
const SDKNotFoundError_1 = require("../SDKNotFoundError");
|
|
20
|
+
const stringifyWhere_1 = require("../stringifyWhere");
|
|
21
|
+
const getOrderBy_1 = require("../getOrderBy");
|
|
22
|
+
async function resolve(input, dbCall, formatQuery, options) {
|
|
28
23
|
const context = {};
|
|
29
|
-
const
|
|
30
|
-
const
|
|
24
|
+
const kind = input.kind;
|
|
25
|
+
const primaryKey = input.artifacts[input.table].primaryKey;
|
|
31
26
|
let rowWithCursorId = undefined;
|
|
32
|
-
if (
|
|
27
|
+
if (kind === "listPaginated") {
|
|
33
28
|
if (input.args?.$paginate == null) {
|
|
34
29
|
throw new Error("$paginate required but not supplied");
|
|
35
30
|
}
|
|
@@ -38,21 +33,22 @@ async function resolve(input, options) {
|
|
|
38
33
|
const cursor = input.args.$paginate.after != null
|
|
39
34
|
? input.args.$paginate.after
|
|
40
35
|
: input.args.$paginate.before;
|
|
41
|
-
rowWithCursorId = await dbCall(formatQuery("SELECT * FROM ?? WHERE
|
|
36
|
+
rowWithCursorId = await dbCall(formatQuery("SELECT * FROM ?? WHERE ?? = ?", [
|
|
42
37
|
input.table,
|
|
38
|
+
primaryKey,
|
|
43
39
|
cursor_1.decodeCursor(cursor),
|
|
44
40
|
])).then((xs) => xs[0]);
|
|
45
41
|
}
|
|
46
42
|
}
|
|
47
43
|
// we need to read the query AST and build a new "SQL AST" from which the SQL and
|
|
48
44
|
// const sqlAST = queryAST.queryASTToSqlAST(resolveInfo, options, context);
|
|
49
|
-
const kind = input.kind;
|
|
50
|
-
delete input.kind;
|
|
51
45
|
const fields = input.fields ?? getScalarFields(input.table, input.artifacts);
|
|
46
|
+
const orderByListPaginatedRootResult = kind === "listPaginated" ? getOrderBy_1.getOrderBy(input.args, primaryKey) : undefined;
|
|
52
47
|
const sqlAST = getSqlAst_1.getSqlAst({
|
|
53
48
|
...input,
|
|
54
49
|
fields,
|
|
55
50
|
getWhere: getWhere_1.getWhere,
|
|
51
|
+
orderBy: orderByListPaginatedRootResult?.orderBy,
|
|
56
52
|
rowWithCursorId,
|
|
57
53
|
dialect: options.dialect,
|
|
58
54
|
});
|
|
@@ -117,9 +113,9 @@ async function resolve(input, options) {
|
|
|
117
113
|
// return true;
|
|
118
114
|
// });
|
|
119
115
|
if (kind === "listPaginated") {
|
|
120
|
-
data = await wrapListPaginated(data, input.args, (xs) => {
|
|
116
|
+
data = await wrapListPaginated(data, input.args, orderByListPaginatedRootResult.flip, (xs) => {
|
|
121
117
|
postProcess(xs, fields, shouldRemoveExtraKeys);
|
|
122
|
-
},
|
|
118
|
+
}, sqlAST, dbCall, input.artifacts[input.table].primaryKey, context, options);
|
|
123
119
|
}
|
|
124
120
|
return data;
|
|
125
121
|
}
|
|
@@ -132,8 +128,7 @@ exports.resolve = resolve;
|
|
|
132
128
|
// Not recursive at the moment; only supported at the root.
|
|
133
129
|
// TODO - remove if support for pagination for mysql dialect
|
|
134
130
|
// is added (I doubt it)
|
|
135
|
-
async function wrapListPaginated(data, args, cb,
|
|
136
|
-
const flip = typeof args.$paginate.first === "number" ? false : true;
|
|
131
|
+
async function wrapListPaginated(data, args, flip, cb, sqlAST, dbCall, primaryKey, context, options) {
|
|
137
132
|
const limit = flip ? args.$paginate.last : args.$paginate.first;
|
|
138
133
|
const hasMoreResults = data.length === limit + 1;
|
|
139
134
|
if (hasMoreResults) {
|
|
@@ -145,37 +140,26 @@ async function wrapListPaginated(data, args, cb, sql, dbCall, dialect, primaryKe
|
|
|
145
140
|
}
|
|
146
141
|
const hasPreviousPage = flip ? hasMoreResults : false;
|
|
147
142
|
const hasNextPage = flip ? false : hasMoreResults;
|
|
148
|
-
const
|
|
149
|
-
const
|
|
150
|
-
const startCursor =
|
|
151
|
-
const endCursor =
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
// Remove ORDER BY to avoid the error:
|
|
169
|
-
// Column "X" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.
|
|
170
|
-
let tmp = sql.split("FROM ");
|
|
171
|
-
tmp.shift();
|
|
172
|
-
let tmp2 = tmp.join("").split("ORDER BY ");
|
|
173
|
-
tmp2.pop();
|
|
174
|
-
countSql = "SELECT " + countField + " FROM " + tmp2.join("");
|
|
175
|
-
totalCount = await dbCall(countSql).then((xs) =>
|
|
176
|
-
// Intentionally "" instead of countField
|
|
177
|
-
xs[0][""]);
|
|
178
|
-
}
|
|
143
|
+
const startKey = data?.[0]?.[primaryKey];
|
|
144
|
+
const endKey = data?.[data.length - 1]?.[primaryKey];
|
|
145
|
+
const startCursor = startKey == null ? null : cursor_1.encodeCursor(startKey);
|
|
146
|
+
const endCursor = endKey == null ? null : cursor_1.encodeCursor(endKey);
|
|
147
|
+
// Replace field selection with `COUNT(*)`
|
|
148
|
+
const fieldNameTotalCount = "totalCount";
|
|
149
|
+
const sqlASTTotalCount = { ...sqlAST };
|
|
150
|
+
sqlASTTotalCount.children = [
|
|
151
|
+
{
|
|
152
|
+
type: "expression",
|
|
153
|
+
sqlExpr: () => "COUNT(*)",
|
|
154
|
+
fieldName: fieldNameTotalCount,
|
|
155
|
+
as: fieldNameTotalCount,
|
|
156
|
+
},
|
|
157
|
+
];
|
|
158
|
+
// Remove ORDER BY to avoid the error:
|
|
159
|
+
// Column "X" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.
|
|
160
|
+
delete sqlASTTotalCount.orderBy;
|
|
161
|
+
const { sql: sqlTotalCount } = await util_1.compileSqlAST(sqlASTTotalCount, context, options);
|
|
162
|
+
const totalCount = await dbCall(sqlTotalCount).then((xs) => xs[0][fieldNameTotalCount]);
|
|
179
163
|
cb(data);
|
|
180
164
|
return {
|
|
181
165
|
paginationInfo: {
|
|
@@ -189,128 +173,198 @@ async function wrapListPaginated(data, args, cb, sql, dbCall, dialect, primaryKe
|
|
|
189
173
|
};
|
|
190
174
|
}
|
|
191
175
|
async function post(input) {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
176
|
+
async function _post() {
|
|
177
|
+
const { dbCall, formatQuery } = input;
|
|
178
|
+
// Shallow clone, as we're going to mutate later
|
|
179
|
+
const data = { ...input.data };
|
|
180
|
+
const tableArtifacts = input.artifacts[input.table];
|
|
181
|
+
if (hasMappedFields(input.artifacts, input.table, data)) {
|
|
182
|
+
await mapMappedFields(tableArtifacts, data, dbCall, formatQuery);
|
|
183
|
+
}
|
|
184
|
+
if (input.dialect === "mysql" && tableArtifacts.dateTimeFieldsCount > 0) {
|
|
185
|
+
for (let k in tableArtifacts.dateTimeFields) {
|
|
186
|
+
if (data[k] != null) {
|
|
187
|
+
data[k] = getDateTimeStringMySQL_1.getDateTimeStringMySQL(data[k]);
|
|
188
|
+
}
|
|
204
189
|
}
|
|
205
190
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
191
|
+
// Delete keys with value of undefined
|
|
192
|
+
for (let k in data) {
|
|
193
|
+
if (data[k] === void 0) {
|
|
194
|
+
delete data[k];
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (input.dialect === "mysql") {
|
|
198
|
+
const inserted = await dbCall(formatQuery("INSERT INTO ?? SET ?", [input.table, data]));
|
|
199
|
+
return inserted.insertId;
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
const columns = Object.keys(data);
|
|
203
|
+
const values = Object.values(data);
|
|
204
|
+
const inserted = await dbCall(formatQuery(`INSERT INTO ?? (${columns
|
|
205
|
+
.map(() => "??")
|
|
206
|
+
.join(",")}) VALUES (${values
|
|
207
|
+
.map(() => "?")
|
|
208
|
+
.join(",")}) SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]`, [input.table, ...columns, ...values]));
|
|
209
|
+
return inserted[0]["SCOPE_IDENTITY"];
|
|
211
210
|
}
|
|
212
211
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
.join(",")}) VALUES (${values
|
|
223
|
-
.map(() => "?")
|
|
224
|
-
.join(",")}) SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]`, [input.table, ...columns, ...values]));
|
|
225
|
-
return inserted[0]["SCOPE_IDENTITY"];
|
|
226
|
-
}
|
|
212
|
+
const id = await _post();
|
|
213
|
+
return resolve({
|
|
214
|
+
table: input.table,
|
|
215
|
+
fieldName: input.fieldName,
|
|
216
|
+
args: { $where: { id } },
|
|
217
|
+
fields: input.fields,
|
|
218
|
+
grabMany: false,
|
|
219
|
+
artifacts: input.artifacts,
|
|
220
|
+
}, input.dbCall, input.formatQuery, { dialect: input.dialect });
|
|
227
221
|
}
|
|
228
222
|
exports.post = post;
|
|
229
223
|
async function patch(input) {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
data[k] = getDateTimeStringMySQL_1.getDateTimeStringMySQL(data[k]);
|
|
224
|
+
async function _patch() {
|
|
225
|
+
const { dbCall, formatQuery } = input;
|
|
226
|
+
const tableArtifacts = input.artifacts[input.table];
|
|
227
|
+
const where = getWhere_1.getWhere(input.table, input.args, input.dialect);
|
|
228
|
+
if (where == null) {
|
|
229
|
+
throw new Error("Null where");
|
|
230
|
+
}
|
|
231
|
+
const current = await dbCall(formatQuery("SELECT * FROM ?? WHERE " + where, [input.table])).then((xs) => xs[0]);
|
|
232
|
+
if (current == null) {
|
|
233
|
+
throw new SDKNotFoundError_1.SDKNotFoundError();
|
|
234
|
+
}
|
|
235
|
+
// Shallow clone, as we're going to mutate later
|
|
236
|
+
const data = { ...input.data };
|
|
237
|
+
if (hasMappedFields(input.artifacts, input.table, data)) {
|
|
238
|
+
await mapMappedFields(tableArtifacts, data, dbCall, formatQuery);
|
|
239
|
+
}
|
|
240
|
+
if (input.dialect === "mysql" && tableArtifacts.dateTimeFieldsCount > 0) {
|
|
241
|
+
for (let k in tableArtifacts.dateTimeFields) {
|
|
242
|
+
if (data[k] != null) {
|
|
243
|
+
data[k] = getDateTimeStringMySQL_1.getDateTimeStringMySQL(data[k]);
|
|
244
|
+
}
|
|
252
245
|
}
|
|
253
246
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
247
|
+
// Delete keys with value of undefined
|
|
248
|
+
for (let k in data) {
|
|
249
|
+
if (data[k] === void 0) {
|
|
250
|
+
delete data[k];
|
|
251
|
+
}
|
|
259
252
|
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
253
|
+
// Nothing to update
|
|
254
|
+
if (Object.keys(data).length === 0) {
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
await dbCall(getUpdateQuery(input.table, data, where, input.dialect, input.formatQuery));
|
|
263
258
|
return true;
|
|
264
259
|
}
|
|
265
|
-
await
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
260
|
+
await _patch();
|
|
261
|
+
return resolve({
|
|
262
|
+
table: input.table,
|
|
263
|
+
fieldName: input.fieldName,
|
|
264
|
+
args: input.args,
|
|
265
|
+
fields: input.fields,
|
|
266
|
+
grabMany: false,
|
|
267
|
+
artifacts: input.artifacts,
|
|
268
|
+
}, input.dbCall, input.formatQuery, { dialect: input.dialect });
|
|
272
269
|
}
|
|
273
270
|
exports.patch = patch;
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
const
|
|
277
|
-
const
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
271
|
+
function getUpdateQuery(table, data, where, dialect, formatQuery) {
|
|
272
|
+
// Assumes `data` is not empty
|
|
273
|
+
const escapeId = stringifyWhere_1.getEscapeId(dialect);
|
|
274
|
+
const escape = stringifyWhere_1.getEscape(dialect);
|
|
275
|
+
let q = "UPDATE ?? ";
|
|
276
|
+
let values = [table];
|
|
277
|
+
let opsStrs = [];
|
|
278
|
+
let dataRegular = {};
|
|
279
|
+
for (let k in data) {
|
|
280
|
+
const v = data[k];
|
|
281
|
+
if (typeof v === "object" && v != null && Object.keys(v).length === 1) {
|
|
282
|
+
const _entries = Object.entries(v)?.[0];
|
|
283
|
+
const op = _entries?.[0];
|
|
284
|
+
const vv = _entries?.[1];
|
|
285
|
+
if (op === "$prepend") {
|
|
286
|
+
opsStrs.push(`${table}.${escapeId(k)} = CASE WHEN ${table}.${escapeId(k)} IS NULL THEN NULL ELSE CONCAT(${escape(vv)}, ${table}.${escapeId(k)}) END`);
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
if (op === "$append") {
|
|
290
|
+
opsStrs.push(`${table}.${escapeId(k)} = CASE WHEN ${table}.${escapeId(k)} IS NULL THEN NULL ELSE CONCAT(${table}.${escapeId(k)}, ${escape(vv)}) END`);
|
|
291
|
+
continue;
|
|
291
292
|
}
|
|
293
|
+
if (op === "$increment") {
|
|
294
|
+
opsStrs.push(`${table}.${escapeId(k)} = ${table}.${escapeId(k)} + ${escape(vv)}`);
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
if (op === "$decrement") {
|
|
298
|
+
opsStrs.push(`${table}.${escapeId(k)} = ${table}.${escapeId(k)} - ${escape(vv)}`);
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
dataRegular[k] = v;
|
|
292
302
|
}
|
|
303
|
+
dataRegular[k] = v;
|
|
293
304
|
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
305
|
+
if (opsStrs.length > 0) {
|
|
306
|
+
q += `SET ${opsStrs.join(", ")}`;
|
|
307
|
+
}
|
|
308
|
+
if (Object.keys(dataRegular).length > 0) {
|
|
309
|
+
if (opsStrs.length > 0) {
|
|
310
|
+
q += ", ?";
|
|
298
311
|
}
|
|
312
|
+
else {
|
|
313
|
+
q += "SET ?";
|
|
314
|
+
}
|
|
315
|
+
values.push(dataRegular);
|
|
299
316
|
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
317
|
+
q += ` WHERE ${where}`;
|
|
318
|
+
return formatQuery(q, values);
|
|
319
|
+
}
|
|
320
|
+
async function patchList(input) {
|
|
321
|
+
async function _patchList() {
|
|
322
|
+
const { dbCall, formatQuery } = input;
|
|
323
|
+
const tableArtifacts = input.artifacts[input.table];
|
|
324
|
+
const where = getWhere_1.getWhere(input.table, input.args, input.dialect);
|
|
325
|
+
if (where == null) {
|
|
326
|
+
throw new Error("Null where");
|
|
327
|
+
}
|
|
328
|
+
// Shallow clone, as we're going to mutate later
|
|
329
|
+
const data = { ...input.data };
|
|
330
|
+
if (hasMappedFields(input.artifacts, input.table, data)) {
|
|
331
|
+
await mapMappedFields(tableArtifacts, data, dbCall, formatQuery);
|
|
332
|
+
}
|
|
333
|
+
if (input.dialect === "mysql" && tableArtifacts.dateTimeFieldsCount > 0) {
|
|
334
|
+
for (let k in tableArtifacts.dateTimeFields) {
|
|
335
|
+
if (data[k] != null) {
|
|
336
|
+
data[k] = getDateTimeStringMySQL_1.getDateTimeStringMySQL(data[k]);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
// Delete keys with value of undefined
|
|
341
|
+
for (let k in data) {
|
|
342
|
+
if (data[k] === void 0) {
|
|
343
|
+
delete data[k];
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
// Nothing to update
|
|
347
|
+
if (Object.keys(data).length === 0) {
|
|
348
|
+
return [];
|
|
349
|
+
}
|
|
350
|
+
await dbCall(getUpdateQuery(input.table, data, where, input.dialect, input.formatQuery));
|
|
351
|
+
return true;
|
|
303
352
|
}
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
353
|
+
await _patchList();
|
|
354
|
+
return resolve({
|
|
355
|
+
table: input.table,
|
|
356
|
+
fieldName: input.fieldName,
|
|
357
|
+
args: input.args,
|
|
358
|
+
fields: input.fields,
|
|
359
|
+
artifacts: input.artifacts,
|
|
360
|
+
grabMany: true,
|
|
361
|
+
kind: "list",
|
|
362
|
+
}, input.dbCall, input.formatQuery, { dialect: input.dialect });
|
|
308
363
|
}
|
|
309
364
|
exports.patchList = patchList;
|
|
310
365
|
async function del(input) {
|
|
311
|
-
const dbCall =
|
|
312
|
-
const
|
|
313
|
-
const _findOne = Object.entries(input.findOne)[0];
|
|
366
|
+
const { dbCall, formatQuery } = input;
|
|
367
|
+
const _findOne = Object.entries(input.args.$where)[0];
|
|
314
368
|
const findOne = { key: _findOne[0], value: _findOne[1] };
|
|
315
369
|
const current = await dbCall(formatQuery("SELECT * FROM ?? WHERE ?? = ?", [
|
|
316
370
|
input.table,
|
|
@@ -329,10 +383,8 @@ async function del(input) {
|
|
|
329
383
|
}
|
|
330
384
|
exports.del = del;
|
|
331
385
|
async function deleteList(input) {
|
|
332
|
-
const dbCall =
|
|
333
|
-
const
|
|
334
|
-
const tableArtifacts = input.artifacts[input.table];
|
|
335
|
-
const where = getWhere_1.getWhere(input.table, { $where: input.where }, tableArtifacts.primaryKey, input.dialect);
|
|
386
|
+
const { dbCall, formatQuery } = input;
|
|
387
|
+
const where = getWhere_1.getWhere(input.table, input.args, input.dialect);
|
|
336
388
|
if (where == null) {
|
|
337
389
|
throw new Error("Null where");
|
|
338
390
|
}
|
|
@@ -342,73 +394,6 @@ async function deleteList(input) {
|
|
|
342
394
|
return true;
|
|
343
395
|
}
|
|
344
396
|
exports.deleteList = deleteList;
|
|
345
|
-
let pool;
|
|
346
|
-
let poolConnect;
|
|
347
|
-
let typeCastMSSQL;
|
|
348
|
-
function init(dialect, opts) {
|
|
349
|
-
let { supplementDriverOpts, ...driverOpts } = opts;
|
|
350
|
-
if (dialect === "mysql") {
|
|
351
|
-
if (supplementDriverOpts) {
|
|
352
|
-
driverOpts = { ...driverOpts_1.driverOptsMySQL, ...driverOpts };
|
|
353
|
-
}
|
|
354
|
-
else {
|
|
355
|
-
driverOpts = {
|
|
356
|
-
typeCast: (field, next) => {
|
|
357
|
-
if (field.type == "TINY" && field.length == 1) {
|
|
358
|
-
return field.string() == "1";
|
|
359
|
-
}
|
|
360
|
-
if (field.type === "VAR_STRING" || field.type === "STRING") {
|
|
361
|
-
return field.string();
|
|
362
|
-
}
|
|
363
|
-
return next();
|
|
364
|
-
},
|
|
365
|
-
...driverOpts,
|
|
366
|
-
};
|
|
367
|
-
}
|
|
368
|
-
mysql_1.init(driverOpts);
|
|
369
|
-
return;
|
|
370
|
-
}
|
|
371
|
-
if (dialect === "mssql") {
|
|
372
|
-
// TODO: see https://github.com/tediousjs/node-mssql/pull/1171
|
|
373
|
-
if (supplementDriverOpts) {
|
|
374
|
-
typeCastMSSQL = driverOpts_1.driverOptsMSSQL.typeCast;
|
|
375
|
-
}
|
|
376
|
-
pool = new mssql.ConnectionPool(driverOpts);
|
|
377
|
-
poolConnect = pool.connect();
|
|
378
|
-
return;
|
|
379
|
-
}
|
|
380
|
-
throw new Error("Unsupported dialect: " + dialect);
|
|
381
|
-
}
|
|
382
|
-
exports.init = init;
|
|
383
|
-
function getDbCall(dialect) {
|
|
384
|
-
if (dialect === "mysql") {
|
|
385
|
-
return mysql_1.query;
|
|
386
|
-
}
|
|
387
|
-
if (dialect === "mssql") {
|
|
388
|
-
return runMSSQLQuery;
|
|
389
|
-
}
|
|
390
|
-
throw new Error("Unsupported dialect: " + dialect);
|
|
391
|
-
}
|
|
392
|
-
async function runMSSQLQuery(query) {
|
|
393
|
-
await poolConnect;
|
|
394
|
-
const request = pool.request();
|
|
395
|
-
const result = await request.query(query);
|
|
396
|
-
// TODO: see https://github.com/tediousjs/node-mssql/pull/1171
|
|
397
|
-
const meta = result.recordset?.columns;
|
|
398
|
-
if (meta != null && typeof typeCastMSSQL === "function") {
|
|
399
|
-
return typeCastMSSQL(result, meta);
|
|
400
|
-
}
|
|
401
|
-
return result.recordset;
|
|
402
|
-
}
|
|
403
|
-
function getFormatQuery(dialect) {
|
|
404
|
-
if (dialect === "mysql") {
|
|
405
|
-
return SqlString.format.bind(SqlString);
|
|
406
|
-
}
|
|
407
|
-
if (dialect === "mssql") {
|
|
408
|
-
return TSqlString.format.bind(TSqlString);
|
|
409
|
-
}
|
|
410
|
-
throw new Error("Unsupported dialect: " + dialect);
|
|
411
|
-
}
|
|
412
397
|
function getScalarFields(table, artifacts) {
|
|
413
398
|
return artifacts[table].scalarFields;
|
|
414
399
|
}
|
|
@@ -524,9 +509,7 @@ function whereNeedsProcessing(where) {
|
|
|
524
509
|
return JSON.stringify(where).includes("Uuid");
|
|
525
510
|
}
|
|
526
511
|
exports.whereNeedsProcessing = whereNeedsProcessing;
|
|
527
|
-
async function _prepareWhere(artifacts, table, data,
|
|
528
|
-
const dbCall = getDbCall(dialect);
|
|
529
|
-
const formatQuery = getFormatQuery(dialect);
|
|
512
|
+
async function _prepareWhere(artifacts, table, data, dbCall, formatQuery) {
|
|
530
513
|
const mappedFields = artifacts[table].mappedFields;
|
|
531
514
|
let out = {};
|
|
532
515
|
await traverseWhere(data, async (where, ptr, parentPtr, root) => {
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.typeCastMSSQL = void 0;
|
|
4
|
+
const mssql = require("mssql");
|
|
5
|
+
// TODO: see https://github.com/tediousjs/node-mssql/pull/1171
|
|
6
|
+
function typeCastMSSQL(customTypeCast) {
|
|
7
|
+
return function (result, meta) {
|
|
8
|
+
let typeCasts = {};
|
|
9
|
+
for (let k in meta) {
|
|
10
|
+
const v = meta[k];
|
|
11
|
+
if (typeof customTypeCast === "function") {
|
|
12
|
+
const tc = customTypeCast(v, mssql);
|
|
13
|
+
if (tc) {
|
|
14
|
+
typeCasts[v.name] = tc;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
// ids
|
|
18
|
+
if (v.identity === true || v.type === mssql.BigInt) {
|
|
19
|
+
typeCasts[v.name] = (x) => (typeof x === "string" ? parseInt(x) : x);
|
|
20
|
+
}
|
|
21
|
+
// uuids
|
|
22
|
+
if (v.type === mssql.UniqueIdentifier) {
|
|
23
|
+
typeCasts[v.name] = (x) => typeof x === "string" ? x.toLocaleLowerCase() : x;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const data = result.recordset;
|
|
27
|
+
if (Object.keys(typeCasts).length === 0) {
|
|
28
|
+
return data;
|
|
29
|
+
}
|
|
30
|
+
return data.map((x) => {
|
|
31
|
+
for (let k of Object.keys(x)) {
|
|
32
|
+
if (typeCasts[k]) {
|
|
33
|
+
x[k] = typeCasts[k](x[k]);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return x;
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
exports.typeCastMSSQL = typeCastMSSQL;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { IOrderBy, IDialect } from "./types";
|
|
2
|
+
declare type IWhere = {
|
|
3
|
+
[k: string]: any;
|
|
4
|
+
};
|
|
5
|
+
declare type IArgs = {
|
|
6
|
+
[k: string]: any;
|
|
7
|
+
};
|
|
8
|
+
export declare function stringifyWhere(input: {
|
|
9
|
+
where: IWhere;
|
|
10
|
+
table: string;
|
|
11
|
+
dialect: IDialect;
|
|
12
|
+
args: IArgs;
|
|
13
|
+
orderBy?: IOrderBy | undefined;
|
|
14
|
+
rowWithCursorId?: any;
|
|
15
|
+
}): string;
|
|
16
|
+
export declare function getEscapeId(dialect: IDialect): any;
|
|
17
|
+
export declare function getEscape(dialect: IDialect): any;
|
|
18
|
+
export {};
|