@technicity/data-service-generator 0.14.1 → 0.14.2

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 (34) hide show
  1. package/dist/generation/generate.d.ts +1 -0
  2. package/dist/generation/generate.js +916 -309
  3. package/dist/runtime/Cache.js +6 -3
  4. package/dist/runtime/IRuntime.d.ts +46 -17
  5. package/dist/runtime/RuntimeMSSQL.d.ts +7 -1
  6. package/dist/runtime/RuntimeMSSQL.js +4 -4
  7. package/dist/runtime/RuntimeMySQL.d.ts +3 -1
  8. package/dist/runtime/RuntimeMySQL.js +33 -7
  9. package/dist/runtime/RuntimeSQLite.d.ts +38 -0
  10. package/dist/runtime/RuntimeSQLite.js +135 -0
  11. package/dist/runtime/lib/MSSQL.d.ts +2 -1
  12. package/dist/runtime/lib/MSSQL.js +36 -8
  13. package/dist/runtime/lib/MySQL.d.ts +1 -1
  14. package/dist/runtime/lib/MySQL.js +15 -2
  15. package/dist/runtime/lib/addNullFallbacks.test.d.ts +1 -0
  16. package/dist/runtime/lib/addNullFallbacks.test.js +206 -0
  17. package/dist/runtime/lib/getSqlAst.js +158 -121
  18. package/dist/runtime/lib/shared.d.ts +1 -2
  19. package/dist/runtime/lib/shared.js +180 -71
  20. package/dist/runtime/lib/stringifyWhere.js +39 -12
  21. package/dist/runtime/lib/stringifyWhere.test.d.ts +1 -0
  22. package/dist/runtime/lib/stringifyWhere.test.js +236 -0
  23. package/dist/runtime/lib/typeCastMSSQL.js +24 -1
  24. package/dist/traverseFieldArgs.d.ts +2 -2
  25. package/dist/traverseFieldArgs.js +8 -3
  26. package/dist/traverseFieldArgs.test.d.ts +1 -0
  27. package/dist/traverseFieldArgs.test.js +56 -0
  28. package/package.json +1 -1
  29. package/dist/ksql.d.ts +0 -15
  30. package/dist/ksql.js +0 -55
  31. package/dist/runtime/RuntimeKSQL.d.ts +0 -19
  32. package/dist/runtime/RuntimeKSQL.js +0 -446
  33. package/dist/runtime/lib/runTransforms.d.ts +0 -2
  34. package/dist/runtime/lib/runTransforms.js +0 -36
@@ -1,4 +1,30 @@
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
+ };
2
28
  Object.defineProperty(exports, "__esModule", { value: true });
3
29
  exports._prepareWhere = exports.whereNeedsProcessing = exports.postProcess = exports.MiddlewareHandler = exports.resolve = void 0;
4
30
  // @ts-ignore
@@ -6,17 +32,16 @@ exports._prepareWhere = exports.whereNeedsProcessing = exports.postProcess = exp
6
32
  // @ts-ignore
7
33
  // import arrToConnection from "join-monster/dist/array-to-connection";
8
34
  // @ts-ignore
9
- const batch_planner_1 = require("join-monster/dist/batch-planner");
35
+ const batch_planner_1 = __importDefault(require("join-monster/dist/batch-planner"));
10
36
  // @ts-ignore
11
37
  const util_1 = require("join-monster/dist/util");
12
38
  const async_hooks_1 = require("async_hooks");
13
- const _ = require("lodash/fp");
39
+ const _ = __importStar(require("lodash/fp"));
14
40
  const uuid_1 = require("uuid");
15
41
  const getSqlAst_1 = require("./getSqlAst");
16
42
  const getWhere_1 = require("./getWhere");
17
43
  const getDateTimeStringMySQL_1 = require("./getDateTimeStringMySQL");
18
44
  const cursor_1 = require("./cursor");
19
- const runTransforms_1 = require("./runTransforms");
20
45
  const addNullFallbacks_1 = require("./addNullFallbacks");
21
46
  const SDKNotFoundError_1 = require("./SDKNotFoundError");
22
47
  const SDKBadWhereError_1 = require("./SDKBadWhereError");
@@ -45,9 +70,9 @@ exports.resolve = resolve;
45
70
  function _resolve(input, dbCall, formatQuery, beginTransaction, dialect, context, cache) {
46
71
  switch (input.action) {
47
72
  case "findMany":
48
- return cache && !input.skipCache ?
49
- getCached(input, dbCall, formatQuery, dialect, cache) :
50
- getData(input, dbCall, formatQuery, dialect);
73
+ return cache && !input.skipCache
74
+ ? getCached(input, dbCall, formatQuery, dialect, cache)
75
+ : getData(input, dbCall, formatQuery, dialect);
51
76
  case "findUnique":
52
77
  case "findManyPaginated":
53
78
  return getData(input, dbCall, formatQuery, dialect);
@@ -116,7 +141,7 @@ async function getData(input, dbCall, formatQuery, dialect) {
116
141
  rowWithMatchingCursor = await dbCall(formatQuery("SELECT * FROM ?? WHERE ?? = ?", [
117
142
  input.resource,
118
143
  primaryKey,
119
- (0, cursor_1.decodeCursor)(cursor),
144
+ (0, cursor_1.decodeCursor)(cursor)
120
145
  ])).then((xs) => xs[0]);
121
146
  if (rowWithMatchingCursor == null) {
122
147
  throw new Error(`Invalid cursor: ${cursor}`);
@@ -134,15 +159,19 @@ async function getData(input, dbCall, formatQuery, dialect) {
134
159
  }
135
160
  // we need to read the query AST and build a new "SQL AST" from which the SQL and
136
161
  // const sqlAST = queryAST.queryASTToSqlAST(resolveInfo, options, context);
137
- const fields = input.fields ?? getScalarFields(input.resource, input.artifacts);
162
+ const fields = input.fields ??
163
+ getScalarFields(input.resource, input.artifacts).reduce((acc, x) => {
164
+ acc[x] = true;
165
+ return acc;
166
+ }, {});
138
167
  const orderByListPaginatedRootResult =
139
168
  // MSSQL's OFFSET and FETCH requires ORDER BY, so we need to provide a fallback
140
169
  dialect === "mssql" && paginationType === "limit-offset"
141
170
  ? {
142
171
  orderBy: (0, getOrderBy_1.getOrderBy)(input.args, primaryKey)?.orderBy ?? [
143
- { column: primaryKey, direction: "asc" },
172
+ { column: primaryKey, direction: "asc" }
144
173
  ],
145
- flip: false,
174
+ flip: false
146
175
  }
147
176
  : action === "findManyPaginated"
148
177
  ? (0, getOrderBy_1.getOrderBy)(input.args, primaryKey)
@@ -160,9 +189,9 @@ async function getData(input, dbCall, formatQuery, dialect) {
160
189
  orderBy: orderByListPaginatedRootResult?.orderBy,
161
190
  rowWithMatchingCursor,
162
191
  dialect,
163
- grabMany,
192
+ grabMany
164
193
  });
165
- const options = { dialect };
194
+ const options = { dialect: dialect === "sqlite" ? "mysql" : dialect };
166
195
  let { sql, shapeDefinition } = await (0, util_1.compileSqlAST)(sqlAST, context, options);
167
196
  if (!sql) {
168
197
  // return {};
@@ -205,6 +234,9 @@ async function getData(input, dbCall, formatQuery, dialect) {
205
234
  // Do later for `listPaginated`, since the `id` is needed for
206
235
  // creating the cursor
207
236
  postProcess(data, fields, shouldRemoveExtraKeys);
237
+ if (dialect === "sqlite") {
238
+ typeCastSqlite(data, fields, input.resource, input.artifacts);
239
+ }
208
240
  }
209
241
  // check for batch data
210
242
  if (Array.isArray(data)) {
@@ -238,12 +270,12 @@ async function getData(input, dbCall, formatQuery, dialect) {
238
270
  fieldName: "data",
239
271
  args: argsTotalCount,
240
272
  // Because we're going to manually set children anyway
241
- fields: [],
273
+ fields: {},
242
274
  getWhere: getWhere_1.getWhere,
243
275
  // We don't want the where clause to include cursor-related stuff
244
276
  rowWithMatchingCursor: null,
245
277
  dialect,
246
- grabMany: true,
278
+ grabMany: true
247
279
  });
248
280
  // Because orderBy doesn't matter for total count.
249
281
  // getOrderBy adds an element if paginating, so deleting args.$orderBy
@@ -253,10 +285,16 @@ async function getData(input, dbCall, formatQuery, dialect) {
253
285
  if (paginationType === "cursor") {
254
286
  data = wrapListPaginationCursor(data, input.args, orderByListPaginatedRootResult.flip, (xs) => {
255
287
  postProcess(xs, fields, shouldRemoveExtraKeys);
288
+ if (dialect === "sqlite") {
289
+ typeCastSqlite(data, fields, input.resource, input.artifacts);
290
+ }
256
291
  }, input.artifacts[input.resource].primaryKey, totalCount);
257
292
  }
258
293
  else {
259
294
  postProcess(data, fields, shouldRemoveExtraKeys);
295
+ if (dialect === "sqlite") {
296
+ typeCastSqlite(data, fields, input.resource, input.artifacts);
297
+ }
260
298
  data = wrapListPaginationLimitOffset(data, totalCount);
261
299
  }
262
300
  }
@@ -305,21 +343,23 @@ async function getCached(input, dbCall, formatQuery, dialect, cache) {
305
343
  function ensureUuidSelect(input) {
306
344
  const { resource, artifacts } = input;
307
345
  const remove = [];
308
- ensure(resource, input);
346
+ ensure(resource, input.fields);
309
347
  function ensure(type, input, path = []) {
310
348
  const { scalarFields, relationFields } = artifacts[type];
311
349
  if (!scalarFields.includes("uuid"))
312
350
  return;
313
- const fields = input.fields || [];
314
- if (!fields.includes("uuid")) {
351
+ const fields = input || {};
352
+ if (!Object.keys(fields).includes("uuid")) {
315
353
  remove.push(path);
316
- fields.unshift("uuid");
354
+ fields["uuid"] = true;
317
355
  }
318
- for (const field of fields)
319
- if (typeof field == "object") {
320
- const { name } = field;
321
- const { table } = relationFields[name];
322
- ensure(table, field, path.concat(name));
356
+ const entries = Object.entries(fields);
357
+ for (const [k, v] of entries)
358
+ if (typeof v === "object") {
359
+ const table = relationFields[k]?.table;
360
+ if (table != null) {
361
+ ensure(table, v.$fields, path.concat(k));
362
+ }
323
363
  }
324
364
  }
325
365
  return remove;
@@ -353,9 +393,9 @@ function wrapListPaginationCursor(data, args, flip, cb, primaryKey, totalCount)
353
393
  hasNextPage,
354
394
  startCursor,
355
395
  endCursor,
356
- totalCount,
396
+ totalCount
357
397
  },
358
- results: data,
398
+ results: data
359
399
  };
360
400
  }
361
401
  async function getTotalCount(sqlASTTotalCount, dbCall, context, options) {
@@ -366,8 +406,8 @@ async function getTotalCount(sqlASTTotalCount, dbCall, context, options) {
366
406
  type: "expression",
367
407
  sqlExpr: () => "COUNT(*)",
368
408
  fieldName: fieldNameTotalCount,
369
- as: fieldNameTotalCount,
370
- },
409
+ as: fieldNameTotalCount
410
+ }
371
411
  ];
372
412
  const { sql: sqlTotalCount } = await (0, util_1.compileSqlAST)(sqlASTTotalCount, context, options);
373
413
  const totalCount = await dbCall(sqlTotalCount).then((xs) => xs[0][fieldNameTotalCount]);
@@ -442,16 +482,22 @@ async function create(input, dbCall, formatQuery, beginTransaction, dialect, con
442
482
  }
443
483
  else {
444
484
  data = processCreateData(data, tableArtifacts, dialect, context?.specialCaseUuidColumn);
445
- if (dialect === "mysql") {
446
- const inserted = await dbCall(formatQuery("INSERT INTO ?? SET ?", [input.resource, data]));
447
- return inserted.insertId;
448
- }
449
- else {
485
+ if (dialect === "mssql") {
450
486
  const columns = Object.keys(data);
451
487
  const values = Object.values(data);
452
488
  const inserted = await dbCall(formatQuery(`INSERT INTO ?? (??) VALUES (?) SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]`, [input.resource, columns, values]));
453
489
  return inserted[0]["SCOPE_IDENTITY"];
454
490
  }
491
+ else {
492
+ const columns = Object.keys(data);
493
+ const values = Object.values(data);
494
+ const inserted = await dbCall(formatQuery(`INSERT INTO ?? (??) VALUES (?)`, [
495
+ input.resource,
496
+ columns,
497
+ values
498
+ ]));
499
+ return inserted.insertId;
500
+ }
455
501
  }
456
502
  }
457
503
  const id = await _create();
@@ -508,7 +554,7 @@ function getCreateTree(data, table, referencedKey, specialCaseUuidColumn, dialec
508
554
  referencedKey,
509
555
  columns: [],
510
556
  values: [],
511
- children: [],
557
+ children: []
512
558
  };
513
559
  for (let i = 0; i < data.length; i++) {
514
560
  let d = data[i];
@@ -597,19 +643,19 @@ function getUpdateQuery(table, data, where, dialect, formatQuery) {
597
643
  const op = _entries?.[0];
598
644
  const vv = _entries?.[1];
599
645
  if (op === "$prepend") {
600
- opsStrs.push(`${table}.${escapeId(k)} = CASE WHEN ${table}.${escapeId(k)} IS NULL THEN NULL ELSE CONCAT(${escape(vv)}, ${table}.${escapeId(k)}) END`);
646
+ opsStrs.push(`${escapeId(k)} = CASE WHEN ${escapeId(k)} IS NULL THEN NULL ELSE ${stringifyConcat(escape(vv), escapeId(k), dialect)} END`);
601
647
  continue;
602
648
  }
603
649
  if (op === "$append") {
604
- opsStrs.push(`${table}.${escapeId(k)} = CASE WHEN ${table}.${escapeId(k)} IS NULL THEN NULL ELSE CONCAT(${table}.${escapeId(k)}, ${escape(vv)}) END`);
650
+ opsStrs.push(`${escapeId(k)} = CASE WHEN ${escapeId(k)} IS NULL THEN NULL ELSE ${stringifyConcat(escapeId(k), escape(vv), dialect)} END`);
605
651
  continue;
606
652
  }
607
653
  if (op === "$increment") {
608
- opsStrs.push(`${table}.${escapeId(k)} = ${table}.${escapeId(k)} + ${escape(vv)}`);
654
+ opsStrs.push(`${escapeId(k)} = ${escapeId(k)} + ${escape(vv)}`);
609
655
  continue;
610
656
  }
611
657
  if (op === "$decrement") {
612
- opsStrs.push(`${table}.${escapeId(k)} = ${table}.${escapeId(k)} - ${escape(vv)}`);
658
+ opsStrs.push(`${escapeId(k)} = ${escapeId(k)} - ${escape(vv)}`);
613
659
  continue;
614
660
  }
615
661
  dataRegular[k] = v;
@@ -619,18 +665,28 @@ function getUpdateQuery(table, data, where, dialect, formatQuery) {
619
665
  if (opsStrs.length > 0) {
620
666
  q += `SET ${opsStrs.join(", ")}`;
621
667
  }
622
- if (Object.keys(dataRegular).length > 0) {
668
+ const columns = Object.keys(dataRegular);
669
+ if (columns.length > 0) {
623
670
  if (opsStrs.length > 0) {
624
- q += ", ?";
671
+ q += ", ";
625
672
  }
626
673
  else {
627
- q += "SET ?";
674
+ q += "SET ";
675
+ }
676
+ q += columns.map((x) => `${escapeId(x)} = ?`).join(", ");
677
+ for (let k in dataRegular) {
678
+ values.push(dataRegular[k]);
628
679
  }
629
- values.push(dataRegular);
630
680
  }
631
681
  q += ` WHERE ${where}`;
632
682
  return formatQuery(q, values);
633
683
  }
684
+ function stringifyConcat(operand1, operand2, dialect) {
685
+ if (dialect !== "sqlite") {
686
+ return `CONCAT(${operand1}, ${operand2})`;
687
+ }
688
+ return `(${operand1} || ${operand2})`;
689
+ }
634
690
  async function updateMany(input, dbCall, formatQuery, dialect, cache) {
635
691
  async function _updateMany() {
636
692
  const escapeId = (0, stringifyWhere_1.getEscapeId)(dialect);
@@ -641,7 +697,9 @@ async function updateMany(input, dbCall, formatQuery, dialect, cache) {
641
697
  }
642
698
  if (cache)
643
699
  try {
644
- const query = formatQuery(`SELECT uuid FROM ?? WHERE ${where}`, [input.resource]);
700
+ const query = formatQuery(`SELECT uuid FROM ?? WHERE ${where}`, [
701
+ input.resource
702
+ ]);
645
703
  const matches = await dbCall(query);
646
704
  const uuids = matches.map((x) => x.uuid);
647
705
  cache.purge(...uuids);
@@ -681,7 +739,7 @@ async function deleteOne(input, dbCall, formatQuery, dialect, cache) {
681
739
  const current = await dbCall(formatQuery("SELECT * FROM ?? WHERE ?? = ?", [
682
740
  input.resource,
683
741
  findOne.key,
684
- findOne.value,
742
+ findOne.value
685
743
  ])).then((xs) => xs[0]);
686
744
  if (current == null) {
687
745
  throw new SDKNotFoundError_1.SDKNotFoundError();
@@ -691,7 +749,7 @@ async function deleteOne(input, dbCall, formatQuery, dialect, cache) {
691
749
  await dbCall(formatQuery("DELETE FROM ?? WHERE ?? = ?", [
692
750
  input.resource,
693
751
  findOne.key,
694
- findOne.value,
752
+ findOne.value
695
753
  ]));
696
754
  return true;
697
755
  }
@@ -703,7 +761,9 @@ async function deleteMany(input, dbCall, formatQuery, dialect, cache) {
703
761
  }
704
762
  if (cache)
705
763
  try {
706
- const query = formatQuery(`SELECT uuid FROM ?? WHERE ${where}`, [input.resource]);
764
+ const query = formatQuery(`SELECT uuid FROM ?? WHERE ${where}`, [
765
+ input.resource
766
+ ]);
707
767
  const matches = await dbCall(query);
708
768
  const uuids = matches.map((x) => x.uuid);
709
769
  cache.purge(...uuids);
@@ -752,7 +812,7 @@ async function mapMappedFields(artifactsForTable, data, dbCall, formatQuery) {
752
812
  mappedField.referencedKey,
753
813
  mappedField.referencedTable,
754
814
  mappedField.name,
755
- w,
815
+ w
756
816
  ])).then((xs) => xs[0]?.[mappedField.referencedKey]);
757
817
  if (result == null) {
758
818
  throw new SDKNotFoundError_1.SDKNotFoundError();
@@ -766,7 +826,7 @@ async function mapMappedFields(artifactsForTable, data, dbCall, formatQuery) {
766
826
  mappedField.referencedKey,
767
827
  mappedField.referencedTable,
768
828
  mappedField.name,
769
- v,
829
+ v
770
830
  ])).then((xs) => xs[0]?.[mappedField.referencedKey]);
771
831
  if (result == null) {
772
832
  throw new SDKNotFoundError_1.SDKNotFoundError();
@@ -777,30 +837,18 @@ async function mapMappedFields(artifactsForTable, data, dbCall, formatQuery) {
777
837
  }
778
838
  }
779
839
  // 1. Remove additional keys added to data for batches and joins
780
- // 2. Execute `transform` functions if they exist
781
840
  function postProcess(data, fields, shouldRemoveExtraKeys) {
782
841
  if (shouldRemoveExtraKeys) {
783
842
  removeExtraKeys(data, fields);
784
843
  }
785
- (0, runTransforms_1.runTransforms)(data, fields);
786
844
  }
787
845
  exports.postProcess = postProcess;
788
846
  function removeExtraKeys(data, fields) {
789
847
  if (data == null || (Array.isArray(data) && data[0] == null)) {
790
848
  return;
791
849
  }
792
- let fieldKeys = [];
793
- for (let x of fields) {
794
- if (typeof x === "string") {
795
- fieldKeys.push(x);
796
- }
797
- else {
798
- fieldKeys.push(x.as ?? x.name);
799
- }
800
- }
801
- const dataKeys = Array.isArray(data)
802
- ? Object.keys(data[0])
803
- : Object.keys(data);
850
+ let fieldKeys = Object.keys(fields);
851
+ const dataKeys = Array.isArray(data) ? Object.keys(data[0]) : Object.keys(data);
804
852
  const extraDataKeys = _.difference(dataKeys, fieldKeys);
805
853
  for (let k of extraDataKeys) {
806
854
  if (Array.isArray(data)) {
@@ -812,16 +860,77 @@ function removeExtraKeys(data, fields) {
812
860
  delete data[k];
813
861
  }
814
862
  }
815
- for (let x of fields) {
816
- if (typeof x === "object") {
817
- const k = typeof x === "string" ? x : x.as ?? x.name;
863
+ const entries = Object.entries(fields);
864
+ for (let [k, v] of entries) {
865
+ if (typeof v === "object" && v.$fields != null) {
866
+ if (Array.isArray(data)) {
867
+ for (let d of data) {
868
+ removeExtraKeys(d[k], v.$fields);
869
+ }
870
+ }
871
+ else {
872
+ removeExtraKeys(data[k], v.$fields);
873
+ }
874
+ }
875
+ }
876
+ }
877
+ const getTypeCastMap = _.memoize(function getTypeCastMap(artifacts) {
878
+ const typeCastMap = new Map();
879
+ for (let table in artifacts) {
880
+ let booleanColumns = new Set();
881
+ for (let field of artifacts[table].fields) {
882
+ if (field.kind === "scalar" && field.type === "boolean") {
883
+ booleanColumns.add(field.name);
884
+ }
885
+ }
886
+ typeCastMap.set(table, { boolean: booleanColumns });
887
+ }
888
+ return typeCastMap;
889
+ });
890
+ function typeCastSqlite(data, fields, table, artifacts) {
891
+ if (data == null || (Array.isArray(data) && data[0] == null)) {
892
+ return;
893
+ }
894
+ const typeCastMap = getTypeCastMap(artifacts);
895
+ const booleanColumns = typeCastMap.get(table)?.boolean;
896
+ if (booleanColumns == null) {
897
+ throw new Error(`Failed to resolve typeCastMap for table \`${table}\``);
898
+ }
899
+ if (booleanColumns.size > 0) {
900
+ const keys = Object.keys(fields);
901
+ for (let k of keys) {
902
+ if (booleanColumns.has(k)) {
903
+ if (Array.isArray(data)) {
904
+ for (let d of data) {
905
+ d[k] = !!d[k];
906
+ }
907
+ }
908
+ else {
909
+ data[k] = !!data[k];
910
+ }
911
+ }
912
+ }
913
+ }
914
+ const tableArtifacts = artifacts[table];
915
+ const entries = Object.entries(fields);
916
+ for (let [k, v] of entries) {
917
+ if (typeof v === "object") {
918
+ const relationFields = tableArtifacts.relationFields[k];
919
+ if (relationFields == null) {
920
+ throw new Error(`Failed to resolve relationFields for field \`${k}\``);
921
+ }
922
+ const fields = v.$fields ??
923
+ tableArtifacts.scalarFields.reduce((acc, x) => {
924
+ acc[x] = true;
925
+ return acc;
926
+ }, {});
818
927
  if (Array.isArray(data)) {
819
928
  for (let d of data) {
820
- removeExtraKeys(d[k], x.fields);
929
+ typeCastSqlite(d[k], fields, relationFields.table, artifacts);
821
930
  }
822
931
  }
823
932
  else {
824
- removeExtraKeys(data[k], x.fields);
933
+ typeCastSqlite(data[k], fields, relationFields.table, artifacts);
825
934
  }
826
935
  }
827
936
  }
@@ -848,7 +957,7 @@ async function _prepareWhere(artifacts, table, data, dbCall, formatQuery) {
848
957
  mappedField.referencedKey,
849
958
  mappedField.referencedTable,
850
959
  mappedField.name,
851
- v,
960
+ v
852
961
  ])).then((xs) => xs[0]?.[mappedField.referencedKey])));
853
962
  if (newVal.some((x) => x == null)) {
854
963
  const index = newVal.findIndex((x) => x == null);
@@ -867,7 +976,7 @@ async function _prepareWhere(artifacts, table, data, dbCall, formatQuery) {
867
976
  mappedField.referencedKey,
868
977
  mappedField.referencedTable,
869
978
  mappedField.name,
870
- where,
979
+ where
871
980
  ])).then((xs) => xs[0]?.[mappedField.referencedKey]);
872
981
  if (newVal == null) {
873
982
  throw new SDKBadWhereError_1.SDKBadWhereError(getPrepareWhereNotFoundMessage(mappedField, where));
@@ -890,7 +999,7 @@ async function _prepareWhere(artifacts, table, data, dbCall, formatQuery) {
890
999
  mappedField.referencedKey,
891
1000
  mappedField.referencedTable,
892
1001
  mappedField.name,
893
- v,
1002
+ v
894
1003
  ])).then((xs) => xs[0]?.[mappedField.referencedKey])));
895
1004
  if (newVal.some((x) => x == null)) {
896
1005
  const index = newVal.findIndex((x) => x == null);
@@ -909,7 +1018,7 @@ async function _prepareWhere(artifacts, table, data, dbCall, formatQuery) {
909
1018
  mappedField.referencedKey,
910
1019
  mappedField.referencedTable,
911
1020
  mappedField.name,
912
- where,
1021
+ where
913
1022
  ])).then((xs) => xs[0]?.[mappedField.referencedKey]);
914
1023
  if (newVal == null) {
915
1024
  throw new SDKBadWhereError_1.SDKBadWhereError(getPrepareWhereNotFoundMessage(mappedField, where));
@@ -941,7 +1050,7 @@ const ops = [
941
1050
  "$like",
942
1051
  "$nlike",
943
1052
  "$btwn",
944
- "$nbtwn",
1053
+ "$nbtwn"
945
1054
  ];
946
1055
  async function traverseWhere(where, cb) {
947
1056
  return await _traverseWhere(cb, where, "", where);
@@ -1,10 +1,35 @@
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
+ };
2
25
  Object.defineProperty(exports, "__esModule", { value: true });
3
26
  exports.getEscape = exports.getEscapeId = exports.stringifyWhere = void 0;
4
- const _ = require("lodash/fp");
5
- const MySqlString = require("sqlstring");
6
- // @ts-ignore
7
- const TSqlString = require("tsqlstring");
27
+ const _ = __importStar(require("lodash/fp"));
28
+ const MySqlString = __importStar(require("sqlstring"));
29
+ // @ts-expect-error
30
+ const TSqlString = __importStar(require("tsqlstring"));
31
+ // @ts-expect-error
32
+ const SqliteString = __importStar(require("sqlstring-sqlite"));
8
33
  function stringifyWhere(input) {
9
34
  const { where, table, dialect, args, orderBy, rowWithMatchingCursor } = input;
10
35
  const escapeId = getEscapeId(dialect);
@@ -16,13 +41,11 @@ function stringifyWhere(input) {
16
41
  orderBy,
17
42
  escapeId,
18
43
  escape,
19
- rowWithMatchingCursor,
44
+ rowWithMatchingCursor
20
45
  });
21
46
  if (paginationWhere) {
22
47
  result =
23
- result.length === 0
24
- ? paginationWhere
25
- : result + " AND " + paginationWhere;
48
+ result.length === 0 ? paginationWhere : result + " AND " + paginationWhere;
26
49
  }
27
50
  return result;
28
51
  }
@@ -84,9 +107,7 @@ function _stringifyWhere(where, table, escapeId, escape, result) {
84
107
  if (v === null) {
85
108
  return `${table}.${escapeId(k)} IS NULL`;
86
109
  }
87
- if (typeof v === "string" ||
88
- typeof v === "number" ||
89
- typeof v === "boolean") {
110
+ if (typeof v === "string" || typeof v === "number" || typeof v === "boolean") {
90
111
  return `${table}.${escapeId(k)} = ${printValue(v)}`;
91
112
  }
92
113
  if (_.isPlainObject(v)) {
@@ -96,7 +117,7 @@ function _stringifyWhere(where, table, escapeId, escape, result) {
96
117
  }
97
118
  const operator = keys[0];
98
119
  const operand = v[operator];
99
- if (operator === "$ne") {
120
+ if (operator === "$neq") {
100
121
  return `${table}.${escapeId(k)} ${getOperatorNeq(v)} ${printValue(operand)}`;
101
122
  }
102
123
  if (operator === "$gt") {
@@ -213,6 +234,9 @@ function getEscapeId(dialect) {
213
234
  if (dialect === "mssql") {
214
235
  return TSqlString.escapeId.bind(TSqlString);
215
236
  }
237
+ if (dialect === "sqlite") {
238
+ return SqliteString.escapeId.bind(SqliteString);
239
+ }
216
240
  throw new Error("Unsupported dialect: " + dialect);
217
241
  }
218
242
  exports.getEscapeId = getEscapeId;
@@ -223,6 +247,9 @@ function getEscape(dialect) {
223
247
  if (dialect === "mssql") {
224
248
  return TSqlString.escape.bind(TSqlString);
225
249
  }
250
+ if (dialect === "sqlite") {
251
+ return SqliteString.escape.bind(SqliteString);
252
+ }
226
253
  throw new Error("Unsupported dialect: " + dialect);
227
254
  }
228
255
  exports.getEscape = getEscape;
@@ -0,0 +1 @@
1
+ export {};