@technicity/data-service-generator 0.4.4 → 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.
Files changed (45) hide show
  1. package/dist/{generate.d.ts → generation/generate.d.ts} +1 -1
  2. package/dist/{generate.js → generation/generate.js} +82 -63
  3. package/dist/index.d.ts +2 -2
  4. package/dist/index.js +2 -2
  5. package/dist/{CustomError.d.ts → lib/CustomError.d.ts} +0 -0
  6. package/dist/{CustomError.js → lib/CustomError.js} +0 -0
  7. package/dist/{getDuplicates.d.ts → lib/getDuplicates.d.ts} +0 -0
  8. package/dist/{getDuplicates.js → lib/getDuplicates.js} +0 -0
  9. package/dist/{isNotNullOrUndefined.d.ts → lib/isNotNullOrUndefined.d.ts} +0 -0
  10. package/dist/{isNotNullOrUndefined.js → lib/isNotNullOrUndefined.js} +0 -0
  11. package/dist/runtime/IRuntime.d.ts +156 -43
  12. package/dist/runtime/RuntimeKSQL.d.ts +11 -11
  13. package/dist/runtime/RuntimeKSQL.js +29 -24
  14. package/dist/runtime/RuntimeMSSQL.d.ts +6 -11
  15. package/dist/runtime/RuntimeMSSQL.js +16 -53
  16. package/dist/runtime/RuntimeMySQL.d.ts +5 -10
  17. package/dist/runtime/RuntimeMySQL.js +19 -49
  18. package/dist/runtime/{__MSSQL.d.ts → lib/MSSQL.d.ts} +0 -0
  19. package/dist/runtime/{__MSSQL.js → lib/MSSQL.js} +0 -0
  20. package/dist/{SDKNotFoundError.d.ts → runtime/lib/SDKNotFoundError.d.ts} +1 -1
  21. package/dist/{SDKNotFoundError.js → runtime/lib/SDKNotFoundError.js} +1 -1
  22. package/dist/{addNullFallbacks.d.ts → runtime/lib/addNullFallbacks.d.ts} +0 -0
  23. package/dist/{addNullFallbacks.js → runtime/lib/addNullFallbacks.js} +0 -0
  24. package/dist/{cursor.d.ts → runtime/lib/cursor.d.ts} +0 -0
  25. package/dist/{cursor.js → runtime/lib/cursor.js} +0 -0
  26. package/dist/{getDateTimeStringMySQL.d.ts → runtime/lib/getDateTimeStringMySQL.d.ts} +0 -0
  27. package/dist/{getDateTimeStringMySQL.js → runtime/lib/getDateTimeStringMySQL.js} +0 -0
  28. package/dist/{getOrderBy.d.ts → runtime/lib/getOrderBy.d.ts} +1 -1
  29. package/dist/{getOrderBy.js → runtime/lib/getOrderBy.js} +3 -7
  30. package/dist/{getSqlAst.d.ts → runtime/lib/getSqlAst.d.ts} +1 -1
  31. package/dist/{getSqlAst.js → runtime/lib/getSqlAst.js} +6 -2
  32. package/dist/{getWhere.d.ts → runtime/lib/getWhere.d.ts} +1 -1
  33. package/dist/{getWhere.js → runtime/lib/getWhere.js} +3 -1
  34. package/dist/{runTransforms.d.ts → runtime/lib/runTransforms.d.ts} +1 -1
  35. package/dist/{runTransforms.js → runtime/lib/runTransforms.js} +0 -0
  36. package/dist/runtime/lib/shared.d.ts +12 -0
  37. package/dist/runtime/{__shared.js → lib/shared.js} +167 -104
  38. package/dist/{stringifyWhere.d.ts → runtime/lib/stringifyWhere.d.ts} +1 -1
  39. package/dist/{stringifyWhere.js → runtime/lib/stringifyWhere.js} +0 -0
  40. package/dist/runtime/{__typeCastMSSQL.d.ts → lib/typeCastMSSQL.d.ts} +0 -0
  41. package/dist/runtime/{__typeCastMSSQL.js → lib/typeCastMSSQL.js} +0 -0
  42. package/package.json +2 -1
  43. package/dist/runtime/__shared.d.ts +0 -32
  44. package/dist/types.d.ts +0 -142
  45. 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.deleteList = exports.del = exports.patchList = exports.patch = exports.post = exports.resolve = void 0;
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("../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) {
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 kind = input.kind;
25
- const primaryKey = input.artifacts[input.table].primaryKey;
89
+ const action = input.action;
90
+ const primaryKey = input.artifacts[input.resource].primaryKey;
26
91
  let rowWithMatchingCursor = undefined;
27
- if (kind === "listPaginated") {
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.table,
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.table, input.artifacts);
46
- const orderByListPaginatedRootResult = kind === "listPaginated" ? getOrderBy_1.getOrderBy(input.args, primaryKey) : undefined;
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: options.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 (kind === "list" || kind === "listPaginated") {
136
+ if (action === "findMany" || action === "findManyPaginated") {
62
137
  let limit = undefined;
63
- if (kind === "listPaginated") {
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 (options.dialect === "mysql") {
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 (kind !== "listPaginated") {
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,27 +187,48 @@ async function resolve(input, dbCall, formatQuery, options) {
112
187
  // }
113
188
  // return true;
114
189
  // });
115
- if (kind === "listPaginated") {
190
+ if (action === "findManyPaginated") {
191
+ const argsTotalCount = input.args && _.cloneDeep(input.args);
192
+ if (argsTotalCount != null) {
193
+ if (argsTotalCount.$paginate != null) {
194
+ // We don't want the where clause to include cursor-related stuff
195
+ delete argsTotalCount.$paginate.after;
196
+ delete argsTotalCount.$paginate.before;
197
+ }
198
+ }
199
+ const sqlASTTotalCount = getSqlAst_1.getSqlAst({
200
+ ...input,
201
+ table: input.resource,
202
+ fieldName: "data",
203
+ args: argsTotalCount,
204
+ // Because we're going to manually set children anyway
205
+ fields: [],
206
+ getWhere: getWhere_1.getWhere,
207
+ // We don't want the where clause to include cursor-related stuff
208
+ rowWithMatchingCursor: null,
209
+ dialect,
210
+ grabMany: true,
211
+ });
212
+ // Because orderBy doesn't matter for total count.
213
+ // getOrderBy adds an element if paginating, so deleting args.$orderBy
214
+ // isn't sufficient.
215
+ delete sqlASTTotalCount.orderBy;
116
216
  data = await wrapListPaginated(data, input.args, orderByListPaginatedRootResult.flip, (xs) => {
117
217
  postProcess(xs, fields, shouldRemoveExtraKeys);
118
- }, sqlAST, dbCall, input.artifacts[input.table].primaryKey, context, options);
218
+ }, sqlASTTotalCount, dbCall, input.artifacts[input.resource].primaryKey, context, options);
119
219
  }
120
220
  return data;
121
221
  }
122
- if (data == null && kind == null) {
222
+ if (data == null && !grabMany) {
123
223
  throw new SDKNotFoundError_1.SDKNotFoundError();
124
224
  }
125
225
  return data;
126
226
  }
127
- exports.resolve = resolve;
128
227
  // Not recursive at the moment; only supported at the root.
129
228
  // TODO - remove if support for pagination for mysql dialect
130
229
  // is added (I doubt it)
131
- async function wrapListPaginated(data, args, flip, cb, sqlAST, dbCall, primaryKey, context, options) {
132
- const isPaginatingBackward = args.$paginate.last != null;
133
- const limit = isPaginatingBackward
134
- ? args.$paginate.last
135
- : args.$paginate.first;
230
+ async function wrapListPaginated(data, args, flip, cb, sqlASTTotalCount, dbCall, primaryKey, context, options) {
231
+ const limit = flip ? args.$paginate.last : args.$paginate.first;
136
232
  const hasMoreResults = data.length === limit + 1;
137
233
  if (hasMoreResults) {
138
234
  // because we peeked
@@ -141,15 +237,14 @@ async function wrapListPaginated(data, args, flip, cb, sqlAST, dbCall, primaryKe
141
237
  if (flip) {
142
238
  data = data.reverse();
143
239
  }
144
- const hasPreviousPage = isPaginatingBackward ? hasMoreResults : false;
145
- const hasNextPage = isPaginatingBackward ? false : hasMoreResults;
240
+ const hasPreviousPage = flip ? hasMoreResults : false;
241
+ const hasNextPage = flip ? false : hasMoreResults;
146
242
  const startKey = data?.[0]?.[primaryKey];
147
243
  const endKey = data?.[data.length - 1]?.[primaryKey];
148
244
  const startCursor = startKey == null ? null : cursor_1.encodeCursor(startKey);
149
245
  const endCursor = endKey == null ? null : cursor_1.encodeCursor(endKey);
150
246
  // Replace field selection with `COUNT(*)`
151
247
  const fieldNameTotalCount = "totalCount";
152
- const sqlASTTotalCount = { ...sqlAST };
153
248
  sqlASTTotalCount.children = [
154
249
  {
155
250
  type: "expression",
@@ -158,9 +253,6 @@ async function wrapListPaginated(data, args, flip, cb, sqlAST, dbCall, primaryKe
158
253
  as: fieldNameTotalCount,
159
254
  },
160
255
  ];
161
- // Remove ORDER BY to avoid the error:
162
- // Column "X" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.
163
- delete sqlASTTotalCount.orderBy;
164
256
  const { sql: sqlTotalCount } = await util_1.compileSqlAST(sqlASTTotalCount, context, options);
165
257
  const totalCount = await dbCall(sqlTotalCount).then((xs) => xs[0][fieldNameTotalCount]);
166
258
  cb(data);
@@ -175,16 +267,15 @@ async function wrapListPaginated(data, args, flip, cb, sqlAST, dbCall, primaryKe
175
267
  results: data,
176
268
  };
177
269
  }
178
- async function post(input) {
179
- async function _post() {
180
- const { dbCall, formatQuery } = input;
270
+ async function create(input, dbCall, formatQuery, dialect) {
271
+ async function _create() {
181
272
  // Shallow clone, as we're going to mutate later
182
273
  const data = { ...input.data };
183
- const tableArtifacts = input.artifacts[input.table];
184
- if (hasMappedFields(input.artifacts, input.table, data)) {
274
+ const tableArtifacts = input.artifacts[input.resource];
275
+ if (hasMappedFields(input.artifacts, input.resource, data)) {
185
276
  await mapMappedFields(tableArtifacts, data, dbCall, formatQuery);
186
277
  }
187
- if (input.dialect === "mysql" && tableArtifacts.dateTimeFieldsCount > 0) {
278
+ if (dialect === "mysql" && tableArtifacts.dateTimeFieldsCount > 0) {
188
279
  for (let k in tableArtifacts.dateTimeFields) {
189
280
  if (data[k] != null) {
190
281
  data[k] = getDateTimeStringMySQL_1.getDateTimeStringMySQL(data[k]);
@@ -197,8 +288,8 @@ async function post(input) {
197
288
  delete data[k];
198
289
  }
199
290
  }
200
- if (input.dialect === "mysql") {
201
- const inserted = await dbCall(formatQuery("INSERT INTO ?? SET ?", [input.table, data]));
291
+ if (dialect === "mysql") {
292
+ const inserted = await dbCall(formatQuery("INSERT INTO ?? SET ?", [input.resource, data]));
202
293
  return inserted.insertId;
203
294
  }
204
295
  else {
@@ -208,39 +299,31 @@ async function post(input) {
208
299
  .map(() => "??")
209
300
  .join(",")}) VALUES (${values
210
301
  .map(() => "?")
211
- .join(",")}) SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]`, [input.table, ...columns, ...values]));
302
+ .join(",")}) SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]`, [input.resource, ...columns, ...values]));
212
303
  return inserted[0]["SCOPE_IDENTITY"];
213
304
  }
214
305
  }
215
- const id = await _post();
216
- return resolve({
217
- table: input.table,
218
- fieldName: input.fieldName,
219
- args: { $where: { id } },
220
- fields: input.fields,
221
- grabMany: false,
222
- artifacts: input.artifacts,
223
- }, input.dbCall, input.formatQuery, { dialect: input.dialect });
306
+ const id = await _create();
307
+ return getData({ ...input, args: { $where: { id } } }, dbCall, formatQuery, dialect);
224
308
  }
225
- exports.post = post;
226
- async function patch(input) {
227
- async function _patch() {
228
- const { dbCall, formatQuery } = input;
229
- const tableArtifacts = input.artifacts[input.table];
230
- const where = getWhere_1.getWhere(input.table, input.args, input.dialect);
309
+ async function update(input, dbCall, formatQuery, dialect) {
310
+ async function _update() {
311
+ const escapeId = stringifyWhere_1.getEscapeId(dialect);
312
+ const tableArtifacts = input.artifacts[input.resource];
313
+ const where = getWhere_1.getWhere(escapeId(input.resource), input.args, dialect);
231
314
  if (where == null) {
232
315
  throw new Error("Null where");
233
316
  }
234
- const current = await dbCall(formatQuery("SELECT * FROM ?? WHERE " + where, [input.table])).then((xs) => xs[0]);
317
+ const current = await dbCall(formatQuery("SELECT * FROM ?? WHERE " + where, [input.resource])).then((xs) => xs[0]);
235
318
  if (current == null) {
236
319
  throw new SDKNotFoundError_1.SDKNotFoundError();
237
320
  }
238
321
  // Shallow clone, as we're going to mutate later
239
322
  const data = { ...input.data };
240
- if (hasMappedFields(input.artifacts, input.table, data)) {
323
+ if (hasMappedFields(input.artifacts, input.resource, data)) {
241
324
  await mapMappedFields(tableArtifacts, data, dbCall, formatQuery);
242
325
  }
243
- if (input.dialect === "mysql" && tableArtifacts.dateTimeFieldsCount > 0) {
326
+ if (dialect === "mysql" && tableArtifacts.dateTimeFieldsCount > 0) {
244
327
  for (let k in tableArtifacts.dateTimeFields) {
245
328
  if (data[k] != null) {
246
329
  data[k] = getDateTimeStringMySQL_1.getDateTimeStringMySQL(data[k]);
@@ -257,20 +340,12 @@ async function patch(input) {
257
340
  if (Object.keys(data).length === 0) {
258
341
  return true;
259
342
  }
260
- await dbCall(getUpdateQuery(input.table, data, where, input.dialect, input.formatQuery));
343
+ await dbCall(getUpdateQuery(input.resource, data, where, dialect, formatQuery));
261
344
  return true;
262
345
  }
263
- await _patch();
264
- return resolve({
265
- table: input.table,
266
- fieldName: input.fieldName,
267
- args: input.args,
268
- fields: input.fields,
269
- grabMany: false,
270
- artifacts: input.artifacts,
271
- }, input.dbCall, input.formatQuery, { dialect: input.dialect });
346
+ await _update();
347
+ return getData(input, dbCall, formatQuery, dialect);
272
348
  }
273
- exports.patch = patch;
274
349
  function getUpdateQuery(table, data, where, dialect, formatQuery) {
275
350
  // Assumes `data` is not empty
276
351
  const escapeId = stringifyWhere_1.getEscapeId(dialect);
@@ -320,20 +395,20 @@ function getUpdateQuery(table, data, where, dialect, formatQuery) {
320
395
  q += ` WHERE ${where}`;
321
396
  return formatQuery(q, values);
322
397
  }
323
- async function patchList(input) {
324
- async function _patchList() {
325
- const { dbCall, formatQuery } = input;
326
- const tableArtifacts = input.artifacts[input.table];
327
- const where = getWhere_1.getWhere(input.table, input.args, input.dialect);
398
+ async function updateMany(input, dbCall, formatQuery, dialect) {
399
+ async function _updateMany() {
400
+ const escapeId = stringifyWhere_1.getEscapeId(dialect);
401
+ const tableArtifacts = input.artifacts[input.resource];
402
+ const where = getWhere_1.getWhere(escapeId(input.resource), input.args, dialect);
328
403
  if (where == null) {
329
404
  throw new Error("Null where");
330
405
  }
331
406
  // Shallow clone, as we're going to mutate later
332
407
  const data = { ...input.data };
333
- if (hasMappedFields(input.artifacts, input.table, data)) {
408
+ if (hasMappedFields(input.artifacts, input.resource, data)) {
334
409
  await mapMappedFields(tableArtifacts, data, dbCall, formatQuery);
335
410
  }
336
- if (input.dialect === "mysql" && tableArtifacts.dateTimeFieldsCount > 0) {
411
+ if (dialect === "mysql" && tableArtifacts.dateTimeFieldsCount > 0) {
337
412
  for (let k in tableArtifacts.dateTimeFields) {
338
413
  if (data[k] != null) {
339
414
  data[k] = getDateTimeStringMySQL_1.getDateTimeStringMySQL(data[k]);
@@ -350,27 +425,17 @@ async function patchList(input) {
350
425
  if (Object.keys(data).length === 0) {
351
426
  return [];
352
427
  }
353
- await dbCall(getUpdateQuery(input.table, data, where, input.dialect, input.formatQuery));
428
+ await dbCall(getUpdateQuery(input.resource, data, where, dialect, formatQuery));
354
429
  return true;
355
430
  }
356
- await _patchList();
357
- return resolve({
358
- table: input.table,
359
- fieldName: input.fieldName,
360
- args: input.args,
361
- fields: input.fields,
362
- artifacts: input.artifacts,
363
- grabMany: true,
364
- kind: "list",
365
- }, input.dbCall, input.formatQuery, { dialect: input.dialect });
431
+ await _updateMany();
432
+ return getData(input, dbCall, formatQuery, dialect);
366
433
  }
367
- exports.patchList = patchList;
368
- async function del(input) {
369
- const { dbCall, formatQuery } = input;
434
+ async function del(input, dbCall, formatQuery, dialect) {
370
435
  const _findOne = Object.entries(input.args.$where)[0];
371
436
  const findOne = { key: _findOne[0], value: _findOne[1] };
372
437
  const current = await dbCall(formatQuery("SELECT * FROM ?? WHERE ?? = ?", [
373
- input.table,
438
+ input.resource,
374
439
  findOne.key,
375
440
  findOne.value,
376
441
  ])).then((xs) => xs[0]);
@@ -378,25 +443,23 @@ async function del(input) {
378
443
  throw new SDKNotFoundError_1.SDKNotFoundError();
379
444
  }
380
445
  await dbCall(formatQuery("DELETE FROM ?? WHERE ?? = ?", [
381
- input.table,
446
+ input.resource,
382
447
  findOne.key,
383
448
  findOne.value,
384
449
  ]));
385
450
  return true;
386
451
  }
387
- exports.del = del;
388
- async function deleteList(input) {
389
- const { dbCall, formatQuery } = input;
390
- const where = getWhere_1.getWhere(input.table, input.args, input.dialect);
452
+ async function deleteMany(input, dbCall, formatQuery, dialect) {
453
+ const escapeId = stringifyWhere_1.getEscapeId(dialect);
454
+ const where = getWhere_1.getWhere(escapeId(input.resource), input.args, dialect);
391
455
  if (where == null) {
392
456
  throw new Error("Null where");
393
457
  }
394
458
  const sql = "DELETE FROM ?? WHERE " + where;
395
- const values = [input.table];
459
+ const values = [input.resource];
396
460
  await dbCall(formatQuery(sql, values));
397
461
  return true;
398
462
  }
399
- exports.deleteList = deleteList;
400
463
  function getScalarFields(table, artifacts) {
401
464
  return artifacts[table].scalarFields;
402
465
  }
@@ -1,4 +1,4 @@
1
- import type { IOrderBy, IDialect } from "./types";
1
+ import type { IOrderBy, IDialect } from "../IRuntime";
2
2
  declare type IWhere = {
3
3
  [k: string]: any;
4
4
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@technicity/data-service-generator",
3
- "version": "0.4.4",
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
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });