@technicity/data-service-generator 0.4.7 → 0.5.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/{generate.d.ts → generation/generate.d.ts} +1 -1
- package/dist/{generate.js → generation/generate.js} +82 -63
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/{CustomError.d.ts → lib/CustomError.d.ts} +0 -0
- package/dist/{CustomError.js → lib/CustomError.js} +0 -0
- package/dist/{getDuplicates.d.ts → lib/getDuplicates.d.ts} +0 -0
- package/dist/{getDuplicates.js → lib/getDuplicates.js} +0 -0
- package/dist/{isNotNullOrUndefined.d.ts → lib/isNotNullOrUndefined.d.ts} +0 -0
- package/dist/{isNotNullOrUndefined.js → lib/isNotNullOrUndefined.js} +0 -0
- package/dist/runtime/IRuntime.d.ts +156 -43
- package/dist/runtime/RuntimeKSQL.d.ts +11 -11
- package/dist/runtime/RuntimeKSQL.js +28 -23
- package/dist/runtime/RuntimeMSSQL.d.ts +6 -11
- package/dist/runtime/RuntimeMSSQL.js +16 -53
- package/dist/runtime/RuntimeMySQL.d.ts +5 -10
- package/dist/runtime/RuntimeMySQL.js +19 -49
- package/dist/runtime/{__MSSQL.d.ts → lib/MSSQL.d.ts} +0 -0
- package/dist/runtime/{__MSSQL.js → lib/MSSQL.js} +0 -0
- package/dist/{SDKNotFoundError.d.ts → runtime/lib/SDKNotFoundError.d.ts} +1 -1
- package/dist/{SDKNotFoundError.js → runtime/lib/SDKNotFoundError.js} +1 -1
- package/dist/{addNullFallbacks.d.ts → runtime/lib/addNullFallbacks.d.ts} +0 -0
- package/dist/{addNullFallbacks.js → runtime/lib/addNullFallbacks.js} +0 -0
- package/dist/{cursor.d.ts → runtime/lib/cursor.d.ts} +0 -0
- package/dist/{cursor.js → runtime/lib/cursor.js} +0 -0
- package/dist/{getDateTimeStringMySQL.d.ts → runtime/lib/getDateTimeStringMySQL.d.ts} +0 -0
- package/dist/{getDateTimeStringMySQL.js → runtime/lib/getDateTimeStringMySQL.js} +0 -0
- package/dist/{getOrderBy.d.ts → runtime/lib/getOrderBy.d.ts} +1 -1
- package/dist/{getOrderBy.js → runtime/lib/getOrderBy.js} +0 -0
- package/dist/{getSqlAst.d.ts → runtime/lib/getSqlAst.d.ts} +1 -1
- package/dist/{getSqlAst.js → runtime/lib/getSqlAst.js} +0 -0
- package/dist/{getWhere.d.ts → runtime/lib/getWhere.d.ts} +1 -1
- package/dist/{getWhere.js → runtime/lib/getWhere.js} +0 -0
- package/dist/{runTransforms.d.ts → runtime/lib/runTransforms.d.ts} +1 -1
- package/dist/{runTransforms.js → runtime/lib/runTransforms.js} +0 -0
- package/dist/runtime/lib/shared.d.ts +12 -0
- package/dist/runtime/{__shared.js → lib/shared.js} +139 -94
- package/dist/{stringifyWhere.d.ts → runtime/lib/stringifyWhere.d.ts} +1 -1
- package/dist/{stringifyWhere.js → runtime/lib/stringifyWhere.js} +0 -0
- package/dist/runtime/{__typeCastMSSQL.d.ts → lib/typeCastMSSQL.d.ts} +0 -0
- package/dist/runtime/{__typeCastMSSQL.js → lib/typeCastMSSQL.js} +0 -0
- package/package.json +2 -1
- package/dist/runtime/__shared.d.ts +0 -32
- package/dist/types.d.ts +0 -142
- package/dist/types.js +0 -2
|
@@ -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.MiddlewareHandler = 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,22 +9,87 @@ exports._prepareWhere = exports.whereNeedsProcessing = exports.deleteList = expo
|
|
|
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 async_hooks_1 = require("async_hooks");
|
|
12
13
|
const _ = require("lodash/fp");
|
|
13
|
-
const getSqlAst_1 = require("
|
|
14
|
-
const getWhere_1 = require("
|
|
15
|
-
const getDateTimeStringMySQL_1 = require("
|
|
16
|
-
const cursor_1 = require("
|
|
17
|
-
const runTransforms_1 = require("
|
|
18
|
-
const addNullFallbacks_1 = require("
|
|
19
|
-
const SDKNotFoundError_1 = require("
|
|
20
|
-
const stringifyWhere_1 = require("
|
|
21
|
-
const getOrderBy_1 = require("
|
|
22
|
-
async function resolve(input, dbCall, formatQuery,
|
|
14
|
+
const getSqlAst_1 = require("./getSqlAst");
|
|
15
|
+
const getWhere_1 = require("./getWhere");
|
|
16
|
+
const getDateTimeStringMySQL_1 = require("./getDateTimeStringMySQL");
|
|
17
|
+
const cursor_1 = require("./cursor");
|
|
18
|
+
const runTransforms_1 = require("./runTransforms");
|
|
19
|
+
const addNullFallbacks_1 = require("./addNullFallbacks");
|
|
20
|
+
const SDKNotFoundError_1 = require("./SDKNotFoundError");
|
|
21
|
+
const stringifyWhere_1 = require("./stringifyWhere");
|
|
22
|
+
const getOrderBy_1 = require("./getOrderBy");
|
|
23
|
+
async function resolve(input, dbCall, formatQuery, dialect, middlewareHandler) {
|
|
24
|
+
// https://github.com/prisma/prisma/blob/822198e5ba21535364d20c86901b8c3778ebf6a3/packages/client/src/runtime/getPrismaClient.ts#L1087
|
|
25
|
+
let index = -1;
|
|
26
|
+
if (middlewareHandler.length() > 0) {
|
|
27
|
+
const resource = new async_hooks_1.AsyncResource("sdk-request");
|
|
28
|
+
const params = input;
|
|
29
|
+
const consumer = (paramsMaybeMutated) => {
|
|
30
|
+
const nextMiddleware = middlewareHandler.get(++index);
|
|
31
|
+
if (nextMiddleware != null) {
|
|
32
|
+
return nextMiddleware(paramsMaybeMutated, consumer);
|
|
33
|
+
}
|
|
34
|
+
const paramsChanged = { ...input, ...params };
|
|
35
|
+
return _resolve(paramsChanged, dbCall, formatQuery, dialect);
|
|
36
|
+
};
|
|
37
|
+
return resource.runInAsyncScope(() => consumer(params));
|
|
38
|
+
}
|
|
39
|
+
return _resolve(input, dbCall, formatQuery, dialect);
|
|
40
|
+
}
|
|
41
|
+
exports.resolve = resolve;
|
|
42
|
+
function _resolve(input, dbCall, formatQuery, dialect) {
|
|
43
|
+
if (input.action === "findUnique") {
|
|
44
|
+
return getData(input, dbCall, formatQuery, dialect);
|
|
45
|
+
}
|
|
46
|
+
if (input.action === "findMany") {
|
|
47
|
+
return getData(input, dbCall, formatQuery, dialect);
|
|
48
|
+
}
|
|
49
|
+
if (input.action === "findManyPaginated") {
|
|
50
|
+
return getData(input, dbCall, formatQuery, dialect);
|
|
51
|
+
}
|
|
52
|
+
if (input.action === "create") {
|
|
53
|
+
return create(input, dbCall, formatQuery, dialect);
|
|
54
|
+
}
|
|
55
|
+
if (input.action === "update") {
|
|
56
|
+
return update(input, dbCall, formatQuery, dialect);
|
|
57
|
+
}
|
|
58
|
+
if (input.action === "updateMany") {
|
|
59
|
+
return updateMany(input, dbCall, formatQuery, dialect);
|
|
60
|
+
}
|
|
61
|
+
if (input.action === "delete") {
|
|
62
|
+
return del(input, dbCall, formatQuery, dialect);
|
|
63
|
+
}
|
|
64
|
+
if (input.action === "deleteMany") {
|
|
65
|
+
return deleteMany(input, dbCall, formatQuery, dialect);
|
|
66
|
+
}
|
|
67
|
+
throw new Error("Invalid action: " + input.action);
|
|
68
|
+
}
|
|
69
|
+
class MiddlewareHandler {
|
|
70
|
+
constructor() {
|
|
71
|
+
this._middlewares = [];
|
|
72
|
+
}
|
|
73
|
+
register(middleware) {
|
|
74
|
+
this._middlewares.push(middleware);
|
|
75
|
+
}
|
|
76
|
+
get(id) {
|
|
77
|
+
return this._middlewares[id];
|
|
78
|
+
}
|
|
79
|
+
has(id) {
|
|
80
|
+
return !!this._middlewares[id];
|
|
81
|
+
}
|
|
82
|
+
length() {
|
|
83
|
+
return this._middlewares.length;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
exports.MiddlewareHandler = MiddlewareHandler;
|
|
87
|
+
async function getData(input, dbCall, formatQuery, dialect) {
|
|
23
88
|
const context = {};
|
|
24
|
-
const
|
|
25
|
-
const primaryKey = input.artifacts[input.
|
|
89
|
+
const action = input.action;
|
|
90
|
+
const primaryKey = input.artifacts[input.resource].primaryKey;
|
|
26
91
|
let rowWithMatchingCursor = undefined;
|
|
27
|
-
if (
|
|
92
|
+
if (action === "findManyPaginated") {
|
|
28
93
|
if (input.args?.$paginate == null) {
|
|
29
94
|
throw new Error("$paginate required but not supplied");
|
|
30
95
|
}
|
|
@@ -34,7 +99,7 @@ async function resolve(input, dbCall, formatQuery, options) {
|
|
|
34
99
|
? input.args.$paginate.after
|
|
35
100
|
: input.args.$paginate.before;
|
|
36
101
|
rowWithMatchingCursor = await dbCall(formatQuery("SELECT * FROM ?? WHERE ?? = ?", [
|
|
37
|
-
input.
|
|
102
|
+
input.resource,
|
|
38
103
|
primaryKey,
|
|
39
104
|
cursor_1.decodeCursor(cursor),
|
|
40
105
|
])).then((xs) => xs[0]);
|
|
@@ -42,25 +107,35 @@ async function resolve(input, dbCall, formatQuery, options) {
|
|
|
42
107
|
}
|
|
43
108
|
// we need to read the query AST and build a new "SQL AST" from which the SQL and
|
|
44
109
|
// const sqlAST = queryAST.queryASTToSqlAST(resolveInfo, options, context);
|
|
45
|
-
const fields = input.fields ?? getScalarFields(input.
|
|
46
|
-
const orderByListPaginatedRootResult =
|
|
110
|
+
const fields = input.fields ?? getScalarFields(input.resource, input.artifacts);
|
|
111
|
+
const orderByListPaginatedRootResult = action === "findManyPaginated"
|
|
112
|
+
? getOrderBy_1.getOrderBy(input.args, primaryKey)
|
|
113
|
+
: undefined;
|
|
114
|
+
const grabMany = action === "findMany" ||
|
|
115
|
+
action === "findManyPaginated" ||
|
|
116
|
+
action === "updateMany" ||
|
|
117
|
+
action === "deleteMany";
|
|
47
118
|
const sqlAST = getSqlAst_1.getSqlAst({
|
|
48
119
|
...input,
|
|
120
|
+
table: input.resource,
|
|
121
|
+
fieldName: "data",
|
|
49
122
|
fields,
|
|
50
123
|
getWhere: getWhere_1.getWhere,
|
|
51
124
|
orderBy: orderByListPaginatedRootResult?.orderBy,
|
|
52
125
|
rowWithMatchingCursor,
|
|
53
|
-
dialect
|
|
126
|
+
dialect,
|
|
127
|
+
grabMany,
|
|
54
128
|
});
|
|
129
|
+
const options = { dialect };
|
|
55
130
|
let { sql, shapeDefinition } = await util_1.compileSqlAST(sqlAST, context, options);
|
|
56
131
|
if (!sql) {
|
|
57
132
|
// return {};
|
|
58
133
|
throw new Error("No SQL");
|
|
59
134
|
}
|
|
60
135
|
// TODO - remove if limit support added for mysql dialect
|
|
61
|
-
if (
|
|
136
|
+
if (action === "findMany" || action === "findManyPaginated") {
|
|
62
137
|
let limit = undefined;
|
|
63
|
-
if (
|
|
138
|
+
if (action === "findManyPaginated") {
|
|
64
139
|
limit = (typeof input?.args?.$paginate?.first === "number"
|
|
65
140
|
? input?.args?.$paginate?.first
|
|
66
141
|
: input?.args?.$paginate?.last);
|
|
@@ -71,7 +146,7 @@ async function resolve(input, dbCall, formatQuery, options) {
|
|
|
71
146
|
limit = input?.args?.$limit;
|
|
72
147
|
}
|
|
73
148
|
if (typeof limit === "number") {
|
|
74
|
-
if (
|
|
149
|
+
if (dialect === "mysql") {
|
|
75
150
|
sql += ` LIMIT ${limit}`;
|
|
76
151
|
}
|
|
77
152
|
else {
|
|
@@ -91,7 +166,7 @@ async function resolve(input, dbCall, formatQuery, options) {
|
|
|
91
166
|
// We only need to remove extra keys if input.fields isn't
|
|
92
167
|
// specified, since otherwise there wouldn't be extra keys
|
|
93
168
|
const shouldRemoveExtraKeys = input.fields != null;
|
|
94
|
-
if (
|
|
169
|
+
if (action !== "findManyPaginated") {
|
|
95
170
|
// Remove additional keys that are added for batches and joins
|
|
96
171
|
// Do later for `listPaginated`, since the `id` is needed for
|
|
97
172
|
// creating the cursor
|
|
@@ -112,7 +187,7 @@ async function resolve(input, dbCall, formatQuery, options) {
|
|
|
112
187
|
// }
|
|
113
188
|
// return true;
|
|
114
189
|
// });
|
|
115
|
-
if (
|
|
190
|
+
if (action === "findManyPaginated") {
|
|
116
191
|
const argsTotalCount = input.args && _.cloneDeep(input.args);
|
|
117
192
|
if (argsTotalCount != null) {
|
|
118
193
|
if (argsTotalCount.$paginate != null) {
|
|
@@ -123,13 +198,16 @@ async function resolve(input, dbCall, formatQuery, options) {
|
|
|
123
198
|
}
|
|
124
199
|
const sqlASTTotalCount = getSqlAst_1.getSqlAst({
|
|
125
200
|
...input,
|
|
201
|
+
table: input.resource,
|
|
202
|
+
fieldName: "data",
|
|
126
203
|
args: argsTotalCount,
|
|
127
204
|
// Because we're going to manually set children anyway
|
|
128
205
|
fields: [],
|
|
129
206
|
getWhere: getWhere_1.getWhere,
|
|
130
207
|
// We don't want the where clause to include cursor-related stuff
|
|
131
208
|
rowWithMatchingCursor: null,
|
|
132
|
-
dialect
|
|
209
|
+
dialect,
|
|
210
|
+
grabMany: true,
|
|
133
211
|
});
|
|
134
212
|
// Because orderBy doesn't matter for total count.
|
|
135
213
|
// getOrderBy adds an element if paginating, so deleting args.$orderBy
|
|
@@ -137,16 +215,15 @@ async function resolve(input, dbCall, formatQuery, options) {
|
|
|
137
215
|
delete sqlASTTotalCount.orderBy;
|
|
138
216
|
data = await wrapListPaginated(data, input.args, orderByListPaginatedRootResult.flip, (xs) => {
|
|
139
217
|
postProcess(xs, fields, shouldRemoveExtraKeys);
|
|
140
|
-
}, sqlASTTotalCount, dbCall, input.artifacts[input.
|
|
218
|
+
}, sqlASTTotalCount, dbCall, input.artifacts[input.resource].primaryKey, context, options);
|
|
141
219
|
}
|
|
142
220
|
return data;
|
|
143
221
|
}
|
|
144
|
-
if (data == null &&
|
|
222
|
+
if (data == null && !grabMany) {
|
|
145
223
|
throw new SDKNotFoundError_1.SDKNotFoundError();
|
|
146
224
|
}
|
|
147
225
|
return data;
|
|
148
226
|
}
|
|
149
|
-
exports.resolve = resolve;
|
|
150
227
|
// Not recursive at the moment; only supported at the root.
|
|
151
228
|
// TODO - remove if support for pagination for mysql dialect
|
|
152
229
|
// is added (I doubt it)
|
|
@@ -190,16 +267,15 @@ async function wrapListPaginated(data, args, flip, cb, sqlASTTotalCount, dbCall,
|
|
|
190
267
|
results: data,
|
|
191
268
|
};
|
|
192
269
|
}
|
|
193
|
-
async function
|
|
194
|
-
async function
|
|
195
|
-
const { dbCall, formatQuery } = input;
|
|
270
|
+
async function create(input, dbCall, formatQuery, dialect) {
|
|
271
|
+
async function _create() {
|
|
196
272
|
// Shallow clone, as we're going to mutate later
|
|
197
273
|
const data = { ...input.data };
|
|
198
|
-
const tableArtifacts = input.artifacts[input.
|
|
199
|
-
if (hasMappedFields(input.artifacts, input.
|
|
274
|
+
const tableArtifacts = input.artifacts[input.resource];
|
|
275
|
+
if (hasMappedFields(input.artifacts, input.resource, data)) {
|
|
200
276
|
await mapMappedFields(tableArtifacts, data, dbCall, formatQuery);
|
|
201
277
|
}
|
|
202
|
-
if (
|
|
278
|
+
if (dialect === "mysql" && tableArtifacts.dateTimeFieldsCount > 0) {
|
|
203
279
|
for (let k in tableArtifacts.dateTimeFields) {
|
|
204
280
|
if (data[k] != null) {
|
|
205
281
|
data[k] = getDateTimeStringMySQL_1.getDateTimeStringMySQL(data[k]);
|
|
@@ -212,8 +288,8 @@ async function post(input) {
|
|
|
212
288
|
delete data[k];
|
|
213
289
|
}
|
|
214
290
|
}
|
|
215
|
-
if (
|
|
216
|
-
const inserted = await dbCall(formatQuery("INSERT INTO ?? SET ?", [input.
|
|
291
|
+
if (dialect === "mysql") {
|
|
292
|
+
const inserted = await dbCall(formatQuery("INSERT INTO ?? SET ?", [input.resource, data]));
|
|
217
293
|
return inserted.insertId;
|
|
218
294
|
}
|
|
219
295
|
else {
|
|
@@ -223,40 +299,31 @@ async function post(input) {
|
|
|
223
299
|
.map(() => "??")
|
|
224
300
|
.join(",")}) VALUES (${values
|
|
225
301
|
.map(() => "?")
|
|
226
|
-
.join(",")}) SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]`, [input.
|
|
302
|
+
.join(",")}) SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]`, [input.resource, ...columns, ...values]));
|
|
227
303
|
return inserted[0]["SCOPE_IDENTITY"];
|
|
228
304
|
}
|
|
229
305
|
}
|
|
230
|
-
const id = await
|
|
231
|
-
return
|
|
232
|
-
table: input.table,
|
|
233
|
-
fieldName: input.fieldName,
|
|
234
|
-
args: { $where: { id } },
|
|
235
|
-
fields: input.fields,
|
|
236
|
-
grabMany: false,
|
|
237
|
-
artifacts: input.artifacts,
|
|
238
|
-
}, input.dbCall, input.formatQuery, { dialect: input.dialect });
|
|
306
|
+
const id = await _create();
|
|
307
|
+
return getData({ ...input, args: { $where: { id } } }, dbCall, formatQuery, dialect);
|
|
239
308
|
}
|
|
240
|
-
|
|
241
|
-
async function
|
|
242
|
-
async function _patch() {
|
|
243
|
-
const { dbCall, formatQuery, dialect } = input;
|
|
309
|
+
async function update(input, dbCall, formatQuery, dialect) {
|
|
310
|
+
async function _update() {
|
|
244
311
|
const escapeId = stringifyWhere_1.getEscapeId(dialect);
|
|
245
|
-
const tableArtifacts = input.artifacts[input.
|
|
246
|
-
const where = getWhere_1.getWhere(escapeId(input.
|
|
312
|
+
const tableArtifacts = input.artifacts[input.resource];
|
|
313
|
+
const where = getWhere_1.getWhere(escapeId(input.resource), input.args, dialect);
|
|
247
314
|
if (where == null) {
|
|
248
315
|
throw new Error("Null where");
|
|
249
316
|
}
|
|
250
|
-
const current = await dbCall(formatQuery("SELECT * FROM ?? WHERE " + where, [input.
|
|
317
|
+
const current = await dbCall(formatQuery("SELECT * FROM ?? WHERE " + where, [input.resource])).then((xs) => xs[0]);
|
|
251
318
|
if (current == null) {
|
|
252
319
|
throw new SDKNotFoundError_1.SDKNotFoundError();
|
|
253
320
|
}
|
|
254
321
|
// Shallow clone, as we're going to mutate later
|
|
255
322
|
const data = { ...input.data };
|
|
256
|
-
if (hasMappedFields(input.artifacts, input.
|
|
323
|
+
if (hasMappedFields(input.artifacts, input.resource, data)) {
|
|
257
324
|
await mapMappedFields(tableArtifacts, data, dbCall, formatQuery);
|
|
258
325
|
}
|
|
259
|
-
if (
|
|
326
|
+
if (dialect === "mysql" && tableArtifacts.dateTimeFieldsCount > 0) {
|
|
260
327
|
for (let k in tableArtifacts.dateTimeFields) {
|
|
261
328
|
if (data[k] != null) {
|
|
262
329
|
data[k] = getDateTimeStringMySQL_1.getDateTimeStringMySQL(data[k]);
|
|
@@ -273,20 +340,12 @@ async function patch(input) {
|
|
|
273
340
|
if (Object.keys(data).length === 0) {
|
|
274
341
|
return true;
|
|
275
342
|
}
|
|
276
|
-
await dbCall(getUpdateQuery(input.
|
|
343
|
+
await dbCall(getUpdateQuery(input.resource, data, where, dialect, formatQuery));
|
|
277
344
|
return true;
|
|
278
345
|
}
|
|
279
|
-
await
|
|
280
|
-
return
|
|
281
|
-
table: input.table,
|
|
282
|
-
fieldName: input.fieldName,
|
|
283
|
-
args: input.args,
|
|
284
|
-
fields: input.fields,
|
|
285
|
-
grabMany: false,
|
|
286
|
-
artifacts: input.artifacts,
|
|
287
|
-
}, input.dbCall, input.formatQuery, { dialect: input.dialect });
|
|
346
|
+
await _update();
|
|
347
|
+
return getData(input, dbCall, formatQuery, dialect);
|
|
288
348
|
}
|
|
289
|
-
exports.patch = patch;
|
|
290
349
|
function getUpdateQuery(table, data, where, dialect, formatQuery) {
|
|
291
350
|
// Assumes `data` is not empty
|
|
292
351
|
const escapeId = stringifyWhere_1.getEscapeId(dialect);
|
|
@@ -336,21 +395,20 @@ function getUpdateQuery(table, data, where, dialect, formatQuery) {
|
|
|
336
395
|
q += ` WHERE ${where}`;
|
|
337
396
|
return formatQuery(q, values);
|
|
338
397
|
}
|
|
339
|
-
async function
|
|
340
|
-
async function
|
|
341
|
-
const { dbCall, formatQuery, dialect } = input;
|
|
398
|
+
async function updateMany(input, dbCall, formatQuery, dialect) {
|
|
399
|
+
async function _updateMany() {
|
|
342
400
|
const escapeId = stringifyWhere_1.getEscapeId(dialect);
|
|
343
|
-
const tableArtifacts = input.artifacts[input.
|
|
344
|
-
const where = getWhere_1.getWhere(escapeId(input.
|
|
401
|
+
const tableArtifacts = input.artifacts[input.resource];
|
|
402
|
+
const where = getWhere_1.getWhere(escapeId(input.resource), input.args, dialect);
|
|
345
403
|
if (where == null) {
|
|
346
404
|
throw new Error("Null where");
|
|
347
405
|
}
|
|
348
406
|
// Shallow clone, as we're going to mutate later
|
|
349
407
|
const data = { ...input.data };
|
|
350
|
-
if (hasMappedFields(input.artifacts, input.
|
|
408
|
+
if (hasMappedFields(input.artifacts, input.resource, data)) {
|
|
351
409
|
await mapMappedFields(tableArtifacts, data, dbCall, formatQuery);
|
|
352
410
|
}
|
|
353
|
-
if (
|
|
411
|
+
if (dialect === "mysql" && tableArtifacts.dateTimeFieldsCount > 0) {
|
|
354
412
|
for (let k in tableArtifacts.dateTimeFields) {
|
|
355
413
|
if (data[k] != null) {
|
|
356
414
|
data[k] = getDateTimeStringMySQL_1.getDateTimeStringMySQL(data[k]);
|
|
@@ -367,27 +425,17 @@ async function patchList(input) {
|
|
|
367
425
|
if (Object.keys(data).length === 0) {
|
|
368
426
|
return [];
|
|
369
427
|
}
|
|
370
|
-
await dbCall(getUpdateQuery(input.
|
|
428
|
+
await dbCall(getUpdateQuery(input.resource, data, where, dialect, formatQuery));
|
|
371
429
|
return true;
|
|
372
430
|
}
|
|
373
|
-
await
|
|
374
|
-
return
|
|
375
|
-
table: input.table,
|
|
376
|
-
fieldName: input.fieldName,
|
|
377
|
-
args: input.args,
|
|
378
|
-
fields: input.fields,
|
|
379
|
-
artifacts: input.artifacts,
|
|
380
|
-
grabMany: true,
|
|
381
|
-
kind: "list",
|
|
382
|
-
}, input.dbCall, input.formatQuery, { dialect: input.dialect });
|
|
431
|
+
await _updateMany();
|
|
432
|
+
return getData(input, dbCall, formatQuery, dialect);
|
|
383
433
|
}
|
|
384
|
-
|
|
385
|
-
async function del(input) {
|
|
386
|
-
const { dbCall, formatQuery } = input;
|
|
434
|
+
async function del(input, dbCall, formatQuery, dialect) {
|
|
387
435
|
const _findOne = Object.entries(input.args.$where)[0];
|
|
388
436
|
const findOne = { key: _findOne[0], value: _findOne[1] };
|
|
389
437
|
const current = await dbCall(formatQuery("SELECT * FROM ?? WHERE ?? = ?", [
|
|
390
|
-
input.
|
|
438
|
+
input.resource,
|
|
391
439
|
findOne.key,
|
|
392
440
|
findOne.value,
|
|
393
441
|
])).then((xs) => xs[0]);
|
|
@@ -395,26 +443,23 @@ async function del(input) {
|
|
|
395
443
|
throw new SDKNotFoundError_1.SDKNotFoundError();
|
|
396
444
|
}
|
|
397
445
|
await dbCall(formatQuery("DELETE FROM ?? WHERE ?? = ?", [
|
|
398
|
-
input.
|
|
446
|
+
input.resource,
|
|
399
447
|
findOne.key,
|
|
400
448
|
findOne.value,
|
|
401
449
|
]));
|
|
402
450
|
return true;
|
|
403
451
|
}
|
|
404
|
-
|
|
405
|
-
async function deleteList(input) {
|
|
406
|
-
const { dbCall, formatQuery, dialect } = input;
|
|
452
|
+
async function deleteMany(input, dbCall, formatQuery, dialect) {
|
|
407
453
|
const escapeId = stringifyWhere_1.getEscapeId(dialect);
|
|
408
|
-
const where = getWhere_1.getWhere(escapeId(input.
|
|
454
|
+
const where = getWhere_1.getWhere(escapeId(input.resource), input.args, dialect);
|
|
409
455
|
if (where == null) {
|
|
410
456
|
throw new Error("Null where");
|
|
411
457
|
}
|
|
412
458
|
const sql = "DELETE FROM ?? WHERE " + where;
|
|
413
|
-
const values = [input.
|
|
459
|
+
const values = [input.resource];
|
|
414
460
|
await dbCall(formatQuery(sql, values));
|
|
415
461
|
return true;
|
|
416
462
|
}
|
|
417
|
-
exports.deleteList = deleteList;
|
|
418
463
|
function getScalarFields(table, artifacts) {
|
|
419
464
|
return artifacts[table].scalarFields;
|
|
420
465
|
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@technicity/data-service-generator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
"@types/sqlstring": "^2.2.1",
|
|
35
35
|
"env-cmd": "^10.1.0",
|
|
36
36
|
"mocha": "9.0.3",
|
|
37
|
+
"sinon": "^11.1.2",
|
|
37
38
|
"typescript": "^4.0.5"
|
|
38
39
|
}
|
|
39
40
|
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import type { IArtifacts, IDialect, TDbCall, TFormatQuery } from "../types";
|
|
2
|
-
import { TResolveInput, TPatchInput, TPatchListInput, TPostInput, TDelInput, TDeleteListInput } from "./IRuntime";
|
|
3
|
-
export declare function resolve(input: TResolveInput, dbCall: TDbCall, formatQuery: TFormatQuery, options: {
|
|
4
|
-
dialect: IDialect;
|
|
5
|
-
}): Promise<any>;
|
|
6
|
-
export declare function post(input: TPostInput & {
|
|
7
|
-
dialect: IDialect;
|
|
8
|
-
dbCall: TDbCall;
|
|
9
|
-
formatQuery: TFormatQuery;
|
|
10
|
-
}): Promise<any>;
|
|
11
|
-
export declare function patch(input: TPatchInput & {
|
|
12
|
-
dialect: IDialect;
|
|
13
|
-
dbCall: TDbCall;
|
|
14
|
-
formatQuery: TFormatQuery;
|
|
15
|
-
}): Promise<any>;
|
|
16
|
-
export declare function patchList(input: TPatchListInput & {
|
|
17
|
-
dialect: IDialect;
|
|
18
|
-
dbCall: TDbCall;
|
|
19
|
-
formatQuery: TFormatQuery;
|
|
20
|
-
}): Promise<any>;
|
|
21
|
-
export declare function del(input: TDelInput & {
|
|
22
|
-
dialect: IDialect;
|
|
23
|
-
dbCall: TDbCall;
|
|
24
|
-
formatQuery: TFormatQuery;
|
|
25
|
-
}): Promise<boolean>;
|
|
26
|
-
export declare function deleteList(input: TDeleteListInput & {
|
|
27
|
-
dialect: IDialect;
|
|
28
|
-
dbCall: TDbCall;
|
|
29
|
-
formatQuery: TFormatQuery;
|
|
30
|
-
}): Promise<boolean>;
|
|
31
|
-
export declare function whereNeedsProcessing(where: any): boolean;
|
|
32
|
-
export declare function _prepareWhere(artifacts: IArtifacts, table: string, data: any, dbCall: TDbCall, formatQuery: TFormatQuery): Promise<{}>;
|
package/dist/types.d.ts
DELETED
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
import { getWhere } from "./getWhere";
|
|
2
|
-
export declare type IDialect = "mysql" | "mssql" | "ksql";
|
|
3
|
-
export declare type TDbCall = (q: string) => Promise<any>;
|
|
4
|
-
export declare type TFormatQuery = (q: string, values: any[]) => string;
|
|
5
|
-
export declare type ISupplementClientOpts = boolean;
|
|
6
|
-
export declare type IOrderBy = Array<{
|
|
7
|
-
column: string;
|
|
8
|
-
direction: any;
|
|
9
|
-
}>;
|
|
10
|
-
export declare type IArgs = {
|
|
11
|
-
[k: string]: any;
|
|
12
|
-
};
|
|
13
|
-
declare type IField = string | {
|
|
14
|
-
name: string;
|
|
15
|
-
as?: string;
|
|
16
|
-
fields: Array<IField>;
|
|
17
|
-
args?: IArgs;
|
|
18
|
-
transform?: (record: any) => any;
|
|
19
|
-
};
|
|
20
|
-
declare type IWhere = (table: string, args: IArgs) => string | undefined;
|
|
21
|
-
declare type IASTChildColumn = {
|
|
22
|
-
type: "column";
|
|
23
|
-
name: string;
|
|
24
|
-
fieldName: string;
|
|
25
|
-
as: string;
|
|
26
|
-
fromOtherTable?: string | undefined;
|
|
27
|
-
};
|
|
28
|
-
declare type IASTChildComposite = {
|
|
29
|
-
type: "composite";
|
|
30
|
-
name: string[];
|
|
31
|
-
fieldName: string;
|
|
32
|
-
as: string;
|
|
33
|
-
fromOtherTable?: string | undefined;
|
|
34
|
-
};
|
|
35
|
-
declare type ISqlJoin = (t1: string, t2: string, args: IArgs) => string;
|
|
36
|
-
declare type ISqlBatch = {
|
|
37
|
-
thisKey: IASTChildColumn;
|
|
38
|
-
parentKey: IASTChildColumn;
|
|
39
|
-
};
|
|
40
|
-
declare type IJunction = {
|
|
41
|
-
sqlTable: string;
|
|
42
|
-
as: string;
|
|
43
|
-
uniqueKey: string | string[];
|
|
44
|
-
where?: IWhere;
|
|
45
|
-
sqlBatch: {
|
|
46
|
-
thisKey: IASTChildColumn;
|
|
47
|
-
parentKey: IASTChildColumn;
|
|
48
|
-
sqlJoin: ISqlJoin;
|
|
49
|
-
};
|
|
50
|
-
} | {
|
|
51
|
-
sqlTable: string;
|
|
52
|
-
as: string;
|
|
53
|
-
where?: IWhere;
|
|
54
|
-
sqlJoins: Array<ISqlJoin>;
|
|
55
|
-
};
|
|
56
|
-
export declare type IGetSQLASTInput = {
|
|
57
|
-
table: string;
|
|
58
|
-
fieldName: string;
|
|
59
|
-
fields: Array<IField>;
|
|
60
|
-
args?: IArgs;
|
|
61
|
-
where?: IWhere;
|
|
62
|
-
sqlJoin?: ISqlJoin;
|
|
63
|
-
sqlBatch?: ISqlBatch;
|
|
64
|
-
junction?: IJunction;
|
|
65
|
-
grabMany: boolean;
|
|
66
|
-
getWhere: typeof getWhere;
|
|
67
|
-
orderBy?: IOrderBy | undefined;
|
|
68
|
-
rowWithMatchingCursor?: any;
|
|
69
|
-
artifacts: IArtifacts;
|
|
70
|
-
dialect: IDialect;
|
|
71
|
-
firstChild?: IASTChildColumn | IASTChildComposite;
|
|
72
|
-
};
|
|
73
|
-
export declare type IRelation = {
|
|
74
|
-
table: string;
|
|
75
|
-
foreignKey: string;
|
|
76
|
-
referencedTable: string;
|
|
77
|
-
referencedKey: string;
|
|
78
|
-
nullable: boolean;
|
|
79
|
-
};
|
|
80
|
-
declare type IOneToManyOrManyToOne = "one-to-many__many-to-one";
|
|
81
|
-
declare type IManyToMany = "many-to-many";
|
|
82
|
-
declare type IRelationKind = "one-to-many" | "many-to-one";
|
|
83
|
-
export declare type IRelationOneToManyOrManyToOne = {
|
|
84
|
-
type: IOneToManyOrManyToOne;
|
|
85
|
-
kind: IRelationKind;
|
|
86
|
-
grabMany: boolean;
|
|
87
|
-
table: string;
|
|
88
|
-
name: string;
|
|
89
|
-
relation: IRelation;
|
|
90
|
-
nullable: boolean;
|
|
91
|
-
};
|
|
92
|
-
export declare type IRelationManyToMany = {
|
|
93
|
-
type: IManyToMany;
|
|
94
|
-
grabMany: boolean;
|
|
95
|
-
table: string;
|
|
96
|
-
junctionTable: string;
|
|
97
|
-
name: string;
|
|
98
|
-
relations: Array<IRelation>;
|
|
99
|
-
};
|
|
100
|
-
export declare type IRelationField = {
|
|
101
|
-
name: string;
|
|
102
|
-
type: IOneToManyOrManyToOne;
|
|
103
|
-
kind: IRelationKind;
|
|
104
|
-
table: string;
|
|
105
|
-
grabMany: boolean;
|
|
106
|
-
relation: IRelation;
|
|
107
|
-
} | {
|
|
108
|
-
name: string;
|
|
109
|
-
type: IManyToMany;
|
|
110
|
-
table: string;
|
|
111
|
-
junctionTable: string;
|
|
112
|
-
grabMany: boolean;
|
|
113
|
-
relations: Array<IRelation>;
|
|
114
|
-
};
|
|
115
|
-
export declare type IMappedField = {
|
|
116
|
-
name: string;
|
|
117
|
-
as: string;
|
|
118
|
-
type: string;
|
|
119
|
-
nullable: boolean;
|
|
120
|
-
table: string;
|
|
121
|
-
foreignKey: string;
|
|
122
|
-
referencedTable: string;
|
|
123
|
-
referencedKey: string;
|
|
124
|
-
};
|
|
125
|
-
export declare type IArtifacts = {
|
|
126
|
-
[k: string]: {
|
|
127
|
-
table: string;
|
|
128
|
-
primaryKey: string;
|
|
129
|
-
scalarFields: Array<string>;
|
|
130
|
-
relationFields: {
|
|
131
|
-
[k: string]: IRelationField;
|
|
132
|
-
};
|
|
133
|
-
mappedFields: {
|
|
134
|
-
[k: string]: IMappedField;
|
|
135
|
-
} | null;
|
|
136
|
-
dateTimeFields: {
|
|
137
|
-
[k: string]: boolean;
|
|
138
|
-
};
|
|
139
|
-
dateTimeFieldsCount: number;
|
|
140
|
-
};
|
|
141
|
-
};
|
|
142
|
-
export {};
|
package/dist/types.js
DELETED