@nymphjs/driver-sqlite3 1.0.0-beta.77 → 1.0.0-beta.78

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/CHANGELOG.md CHANGED
@@ -3,6 +3,25 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [1.0.0-beta.78](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.77...v1.0.0-beta.78) (2024-09-27)
7
+
8
+ ### Bug Fixes
9
+
10
+ - don't use transactions for import, to avoid issues when creating tables ([2f8f715](https://github.com/sciactive/nymphjs/commit/2f8f71545de6ca2f3235a9f3a3c91107f58129bf))
11
+
12
+ ### Features
13
+
14
+ - add json column to sqlite and store json values there ([55a8a84](https://github.com/sciactive/nymphjs/commit/55a8a840fb7d23561d60fda602c410b79f76b0da))
15
+ - add method to detect if migration is needed ([274f7c3](https://github.com/sciactive/nymphjs/commit/274f7c39aa4e0d251a38c01593775e1270fc9621))
16
+ - move comparison fields to data table and remove comparison table ([3d7fe7e](https://github.com/sciactive/nymphjs/commit/3d7fe7e614327ecf8903ee7143e559549793e8fc))
17
+
18
+ ### BREAKING CHANGES
19
+
20
+ - This is a breaking change, and requires export and
21
+ import of the database if you are using the SQLite driver.
22
+ - This is a breaking change, and requires export and
23
+ import of the database.
24
+
6
25
  # [1.0.0-beta.77](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.76...v1.0.0-beta.77) (2024-09-26)
7
26
 
8
27
  ### Bug Fixes
@@ -101,5 +101,6 @@ export default class SQLite3Driver extends NymphDriver {
101
101
  setUID(name: string, curUid: number): Promise<boolean>;
102
102
  internalTransaction(name: string): Promise<void>;
103
103
  startTransaction(name: string): Promise<import("@nymphjs/nymph").Nymph>;
104
+ needsMigration(): Promise<boolean>;
104
105
  }
105
106
  export {};
@@ -194,21 +194,16 @@ class SQLite3Driver extends nymph_1.NymphDriver {
194
194
  this.queryRun(`CREATE INDEX IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}entities_${etype}_id_mdate`)} ON ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} ("mdate");`);
195
195
  this.queryRun(`CREATE INDEX IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}entities_${etype}_id_tags`)} ON ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} ("tags");`);
196
196
  // Create the data table.
197
- this.queryRun(`CREATE TABLE IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("guid" CHARACTER(24) NOT NULL REFERENCES ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} ("guid") ON DELETE CASCADE, "name" TEXT NOT NULL, "value" TEXT NOT NULL, PRIMARY KEY("guid", "name"));`);
197
+ this.queryRun(`CREATE TABLE IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("guid" CHARACTER(24) NOT NULL REFERENCES ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} ("guid") ON DELETE CASCADE, "name" TEXT NOT NULL, "value" CHARACTER(1) NOT NULL, "json" BLOB, "string" TEXT, "number" REAL, "truthy" INTEGER, PRIMARY KEY("guid", "name"));`);
198
198
  this.queryRun(`CREATE INDEX IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}data_${etype}_id_guid`)} ON ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("guid");`);
199
- this.queryRun(`CREATE INDEX IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}data_${etype}_id_name`)} ON ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("name");`);
200
- this.queryRun(`CREATE INDEX IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}data_${etype}_id_name_value`)} ON ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("name", "value");`);
201
- this.queryRun(`CREATE INDEX IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}data_${etype}_id_value`)} ON ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("value");`);
199
+ this.queryRun(`CREATE INDEX IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}data_${etype}_id_guid_name`)} ON ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("guid", "name");`);
202
200
  this.queryRun(`CREATE INDEX IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}data_${etype}_id_guid__name_user`)} ON ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("guid") WHERE "name" = \'user\';`);
203
201
  this.queryRun(`CREATE INDEX IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}data_${etype}_id_guid__name_group`)} ON ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("guid") WHERE "name" = \'group\';`);
204
- // Create the comparisons table.
205
- this.queryRun(`CREATE TABLE IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} ("guid" CHARACTER(24) NOT NULL REFERENCES ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} ("guid") ON DELETE CASCADE, "name" TEXT NOT NULL, "truthy" INTEGER, "string" TEXT, "number" REAL, PRIMARY KEY("guid", "name"));`);
206
- this.queryRun(`CREATE INDEX IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}_id_guid`)} ON ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} ("guid");`);
207
- this.queryRun(`CREATE INDEX IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}_id_name`)} ON ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} ("name");`);
208
- this.queryRun(`CREATE INDEX IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}_id_name__truthy`)} ON ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} ("name") WHERE "truthy" = 1;`);
209
- this.queryRun(`CREATE INDEX IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}_id_name__falsy`)} ON ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} ("name") WHERE "truthy" <> 1;`);
210
- this.queryRun(`CREATE INDEX IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}_id_name_string`)} ON ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} ("name", "string");`);
211
- this.queryRun(`CREATE INDEX IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}_id_name_number`)} ON ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} ("name", "number");`);
202
+ this.queryRun(`CREATE INDEX IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}data_${etype}_id_name`)} ON ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("name");`);
203
+ this.queryRun(`CREATE INDEX IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}data_${etype}_id_name__truthy`)} ON ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("name") WHERE "truthy" = 1;`);
204
+ this.queryRun(`CREATE INDEX IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}data_${etype}_id_name__falsy`)} ON ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("name") WHERE "truthy" <> 1;`);
205
+ this.queryRun(`CREATE INDEX IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}data_${etype}_id_name_string`)} ON ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("name", "string");`);
206
+ this.queryRun(`CREATE INDEX IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}data_${etype}_id_name_number`)} ON ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("name", "number");`);
212
207
  // Create the references table.
213
208
  this.queryRun(`CREATE TABLE IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}references_${etype}`)} ("guid" CHARACTER(24) NOT NULL REFERENCES ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} ("guid") ON DELETE CASCADE, "name" TEXT NOT NULL, "reference" CHARACTER(24) NOT NULL, PRIMARY KEY("guid", "name", "reference"));`);
214
209
  this.queryRun(`CREATE INDEX IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}references_${etype}_id_guid`)} ON ${SQLite3Driver.escape(`${this.prefix}references_${etype}`)} ("guid");`);
@@ -312,12 +307,6 @@ class SQLite3Driver extends nymph_1.NymphDriver {
312
307
  guid,
313
308
  },
314
309
  });
315
- this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} WHERE "guid"=@guid;`, {
316
- etypes: [etype],
317
- params: {
318
- guid,
319
- },
320
- });
321
310
  this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}references_${etype}`)} WHERE "guid"=@guid;`, {
322
311
  etypes: [etype],
323
312
  params: {
@@ -400,16 +389,18 @@ class SQLite3Driver extends nymph_1.NymphDriver {
400
389
  return;
401
390
  }
402
391
  // Get the etypes.
403
- const tables = this.queryIter("SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name;");
392
+ const tables = this.queryIter("SELECT `name` FROM `sqlite_master` WHERE `type`='table' AND `name` LIKE @prefix;", {
393
+ params: {
394
+ prefix: this.prefix + 'entities_' + '%',
395
+ },
396
+ });
404
397
  const etypes = [];
405
398
  for (const table of tables) {
406
- if (table.name.startsWith(this.prefix + 'entities_')) {
407
- etypes.push(table.name.substr((this.prefix + 'entities_').length));
408
- }
399
+ etypes.push(table.name.substr((this.prefix + 'entities_').length));
409
400
  }
410
401
  for (const etype of etypes) {
411
402
  // Export entities.
412
- const dataIterator = this.queryIter(`SELECT e.*, d."name" AS "dname", d."value" AS "dvalue", c."string", c."number" FROM ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} e LEFT JOIN ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} d USING ("guid") INNER JOIN ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} c USING ("guid", "name") ORDER BY e."guid";`)[Symbol.iterator]();
403
+ const dataIterator = this.queryIter(`SELECT e.*, d."name", d."value", json(d."json") as "json", d."string", d."number" FROM ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} e LEFT JOIN ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} d USING ("guid") ORDER BY e."guid";`)[Symbol.iterator]();
413
404
  let datum = dataIterator.next();
414
405
  while (!datum.done) {
415
406
  const guid = datum.value.guid;
@@ -420,16 +411,18 @@ class SQLite3Driver extends nymph_1.NymphDriver {
420
411
  currentEntityExport.push(`{${guid}}<${etype}>[${tags}]`);
421
412
  currentEntityExport.push(`\tcdate=${JSON.stringify(cdate)}`);
422
413
  currentEntityExport.push(`\tmdate=${JSON.stringify(mdate)}`);
423
- if (datum.value.dname != null) {
414
+ if (datum.value.name != null) {
424
415
  // This do will keep going and adding the data until the
425
416
  // next entity is reached. datum will end on the next entity.
426
417
  do {
427
- const value = datum.value.dvalue === 'N'
418
+ const value = datum.value.value === 'N'
428
419
  ? JSON.stringify(datum.value.number)
429
- : datum.value.dvalue === 'S'
420
+ : datum.value.value === 'S'
430
421
  ? JSON.stringify(datum.value.string)
431
- : datum.value.dvalue;
432
- currentEntityExport.push(`\t${datum.value.dname}=${value}`);
422
+ : datum.value.value === 'J'
423
+ ? datum.value.json
424
+ : datum.value.value;
425
+ currentEntityExport.push(`\t${datum.value.name}=${value}`);
433
426
  datum = dataIterator.next();
434
427
  } while (!datum.done && datum.value.guid === guid);
435
428
  }
@@ -460,7 +453,6 @@ class SQLite3Driver extends nymph_1.NymphDriver {
460
453
  }
461
454
  const eTable = `e${tableSuffix}`;
462
455
  const dTable = `d${tableSuffix}`;
463
- const cTable = `c${tableSuffix}`;
464
456
  const fTable = `f${tableSuffix}`;
465
457
  const ieTable = `ie${tableSuffix}`;
466
458
  const sTable = `s${tableSuffix}`;
@@ -553,7 +545,7 @@ class SQLite3Driver extends nymph_1.NymphDriver {
553
545
  curQuery +=
554
546
  ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
555
547
  'EXISTS (SELECT "guid" FROM ' +
556
- SQLite3Driver.escape(this.prefix + 'comparisons_' + etype) +
548
+ SQLite3Driver.escape(this.prefix + 'data_' + etype) +
557
549
  ' WHERE "guid"=' +
558
550
  ieTable +
559
551
  '."guid" AND "name"=@' +
@@ -600,7 +592,7 @@ class SQLite3Driver extends nymph_1.NymphDriver {
600
592
  curQuery +=
601
593
  ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
602
594
  'EXISTS (SELECT "guid" FROM ' +
603
- SQLite3Driver.escape(this.prefix + 'comparisons_' + etype) +
595
+ SQLite3Driver.escape(this.prefix + 'data_' + etype) +
604
596
  ' WHERE "guid"=' +
605
597
  ieTable +
606
598
  '."guid" AND "name"=@' +
@@ -620,7 +612,7 @@ class SQLite3Driver extends nymph_1.NymphDriver {
620
612
  curQuery +=
621
613
  ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
622
614
  'EXISTS (SELECT "guid" FROM ' +
623
- SQLite3Driver.escape(this.prefix + 'comparisons_' + etype) +
615
+ SQLite3Driver.escape(this.prefix + 'data_' + etype) +
624
616
  ' WHERE "guid"=' +
625
617
  ieTable +
626
618
  '."guid" AND "name"=@' +
@@ -653,9 +645,9 @@ class SQLite3Driver extends nymph_1.NymphDriver {
653
645
  ieTable +
654
646
  '."guid" AND "name"=@' +
655
647
  name +
656
- ' AND "value"=@' +
648
+ ' AND "json"=jsonb(@' +
657
649
  value +
658
- ')';
650
+ '))';
659
651
  params[name] = curValue[0];
660
652
  params[value] = svalue;
661
653
  }
@@ -689,58 +681,35 @@ class SQLite3Driver extends nymph_1.NymphDriver {
689
681
  break;
690
682
  }
691
683
  else {
684
+ const containTableSuffix = (0, guid_1.makeTableSuffix)();
692
685
  if (curQuery) {
693
686
  curQuery += typeIsOr ? ' OR ' : ' AND ';
694
687
  }
695
688
  let svalue;
696
- let stringValue;
697
689
  if (curValue[1] instanceof Object &&
698
690
  typeof curValue[1].toReference === 'function') {
699
691
  svalue = JSON.stringify(curValue[1].toReference());
700
- stringValue = `${curValue[1].toReference()}`;
701
692
  }
702
693
  else {
703
694
  svalue = JSON.stringify(curValue[1]);
704
- stringValue = `${curValue[1]}`;
705
695
  }
706
696
  const name = `param${++count.i}`;
707
697
  const value = `param${++count.i}`;
708
- if (typeof curValue[1] === 'string') {
709
- const stringParam = `param${++count.i}`;
710
- curQuery +=
711
- ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
712
- '(EXISTS (SELECT "guid" FROM ' +
713
- SQLite3Driver.escape(this.prefix + 'data_' + etype) +
714
- ' WHERE "guid"=' +
715
- ieTable +
716
- '."guid" AND "name"=@' +
717
- name +
718
- ' AND instr("value", @' +
719
- value +
720
- ')) OR EXISTS (SELECT "guid" FROM ' +
721
- SQLite3Driver.escape(this.prefix + 'comparisons_' + etype) +
722
- ' WHERE "guid"=' +
723
- ieTable +
724
- '."guid" AND "name"=@' +
725
- name +
726
- ' AND "string"=@' +
727
- stringParam +
728
- '))';
729
- params[stringParam] = stringValue;
730
- }
731
- else {
732
- curQuery +=
733
- ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
734
- 'EXISTS (SELECT "guid" FROM ' +
735
- SQLite3Driver.escape(this.prefix + 'data_' + etype) +
736
- ' WHERE "guid"=' +
737
- ieTable +
738
- '."guid" AND "name"=@' +
739
- name +
740
- ' AND instr("value", @' +
741
- value +
742
- '))';
743
- }
698
+ curQuery +=
699
+ ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
700
+ 'EXISTS (SELECT "guid" FROM ' +
701
+ SQLite3Driver.escape(this.prefix + 'data_' + etype) +
702
+ ' d' +
703
+ containTableSuffix +
704
+ ' WHERE "guid"=' +
705
+ ieTable +
706
+ '."guid" AND "name"=@' +
707
+ name +
708
+ ' AND json(@' +
709
+ value +
710
+ ') IN (SELECT json_quote("value") FROM json_each(d' +
711
+ containTableSuffix +
712
+ '."json")))';
744
713
  params[name] = curValue[0];
745
714
  params[value] = svalue;
746
715
  }
@@ -786,7 +755,7 @@ class SQLite3Driver extends nymph_1.NymphDriver {
786
755
  curQuery +=
787
756
  ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
788
757
  'EXISTS (SELECT "guid" FROM ' +
789
- SQLite3Driver.escape(this.prefix + 'comparisons_' + etype) +
758
+ SQLite3Driver.escape(this.prefix + 'data_' + etype) +
790
759
  ' WHERE "guid"=' +
791
760
  ieTable +
792
761
  '."guid" AND "name"=@' +
@@ -839,7 +808,7 @@ class SQLite3Driver extends nymph_1.NymphDriver {
839
808
  curQuery +=
840
809
  ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
841
810
  'EXISTS (SELECT "guid" FROM ' +
842
- SQLite3Driver.escape(this.prefix + 'comparisons_' + etype) +
811
+ SQLite3Driver.escape(this.prefix + 'data_' + etype) +
843
812
  ' WHERE "guid"=' +
844
813
  ieTable +
845
814
  '."guid" AND "name"=@' +
@@ -892,7 +861,7 @@ class SQLite3Driver extends nymph_1.NymphDriver {
892
861
  curQuery +=
893
862
  ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
894
863
  'EXISTS (SELECT "guid" FROM ' +
895
- SQLite3Driver.escape(this.prefix + 'comparisons_' + etype) +
864
+ SQLite3Driver.escape(this.prefix + 'data_' + etype) +
896
865
  ' WHERE "guid"=' +
897
866
  ieTable +
898
867
  '."guid" AND "name"=@' +
@@ -945,7 +914,7 @@ class SQLite3Driver extends nymph_1.NymphDriver {
945
914
  curQuery +=
946
915
  ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
947
916
  'EXISTS (SELECT "guid" FROM ' +
948
- SQLite3Driver.escape(this.prefix + 'comparisons_' + etype) +
917
+ SQLite3Driver.escape(this.prefix + 'data_' + etype) +
949
918
  ' WHERE "guid"=' +
950
919
  ieTable +
951
920
  '."guid" AND "name"=@' +
@@ -994,7 +963,7 @@ class SQLite3Driver extends nymph_1.NymphDriver {
994
963
  curQuery +=
995
964
  ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
996
965
  'EXISTS (SELECT "guid" FROM ' +
997
- SQLite3Driver.escape(this.prefix + 'comparisons_' + etype) +
966
+ SQLite3Driver.escape(this.prefix + 'data_' + etype) +
998
967
  ' WHERE "guid"=' +
999
968
  ieTable +
1000
969
  '."guid" AND "name"=@' +
@@ -1043,7 +1012,7 @@ class SQLite3Driver extends nymph_1.NymphDriver {
1043
1012
  curQuery +=
1044
1013
  ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
1045
1014
  'EXISTS (SELECT "guid" FROM ' +
1046
- SQLite3Driver.escape(this.prefix + 'comparisons_' + etype) +
1015
+ SQLite3Driver.escape(this.prefix + 'data_' + etype) +
1047
1016
  ' WHERE "guid"=' +
1048
1017
  ieTable +
1049
1018
  '."guid" AND "name"=@' +
@@ -1092,7 +1061,7 @@ class SQLite3Driver extends nymph_1.NymphDriver {
1092
1061
  curQuery +=
1093
1062
  ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
1094
1063
  'EXISTS (SELECT "guid" FROM ' +
1095
- SQLite3Driver.escape(this.prefix + 'comparisons_' + etype) +
1064
+ SQLite3Driver.escape(this.prefix + 'data_' + etype) +
1096
1065
  ' WHERE "guid"=' +
1097
1066
  ieTable +
1098
1067
  '."guid" AND "name"=@' +
@@ -1141,7 +1110,7 @@ class SQLite3Driver extends nymph_1.NymphDriver {
1141
1110
  curQuery +=
1142
1111
  ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
1143
1112
  'EXISTS (SELECT "guid" FROM ' +
1144
- SQLite3Driver.escape(this.prefix + 'comparisons_' + etype) +
1113
+ SQLite3Driver.escape(this.prefix + 'data_' + etype) +
1145
1114
  ' WHERE "guid"=' +
1146
1115
  ieTable +
1147
1116
  '."guid" AND "name"=@' +
@@ -1247,7 +1216,7 @@ class SQLite3Driver extends nymph_1.NymphDriver {
1247
1216
  const name = `param${++count.i}`;
1248
1217
  sortJoin = `LEFT JOIN (
1249
1218
  SELECT "guid", "string", "number"
1250
- FROM ${SQLite3Driver.escape(this.prefix + 'comparisons_' + etype)}
1219
+ FROM ${SQLite3Driver.escape(this.prefix + 'data_' + etype)}
1251
1220
  WHERE "name"=@${name}
1252
1221
  ORDER BY "number"${order}, "string"${order}
1253
1222
  ) ${sTable} USING ("guid")`;
@@ -1303,11 +1272,11 @@ class SQLite3Driver extends nymph_1.NymphDriver {
1303
1272
  ${eTable}."mdate",
1304
1273
  ${dTable}."name",
1305
1274
  ${dTable}."value",
1306
- ${cTable}."string",
1307
- ${cTable}."number"
1275
+ json(${dTable}."json") as "json",
1276
+ ${dTable}."string",
1277
+ ${dTable}."number"
1308
1278
  FROM ${SQLite3Driver.escape(this.prefix + 'entities_' + etype)} ${eTable}
1309
1279
  LEFT JOIN ${SQLite3Driver.escape(this.prefix + 'data_' + etype)} ${dTable} USING ("guid")
1310
- INNER JOIN ${SQLite3Driver.escape(this.prefix + 'comparisons_' + etype)} ${cTable} USING ("guid", "name")
1311
1280
  ${sortJoin}
1312
1281
  INNER JOIN (
1313
1282
  SELECT "guid"
@@ -1364,11 +1333,11 @@ class SQLite3Driver extends nymph_1.NymphDriver {
1364
1333
  ${eTable}."mdate",
1365
1334
  ${dTable}."name",
1366
1335
  ${dTable}."value",
1367
- ${cTable}."string",
1368
- ${cTable}."number"
1336
+ json(${dTable}."json") as "json",
1337
+ ${dTable}."string",
1338
+ ${dTable}."number"
1369
1339
  FROM ${SQLite3Driver.escape(this.prefix + 'entities_' + etype)} ${eTable}
1370
1340
  LEFT JOIN ${SQLite3Driver.escape(this.prefix + 'data_' + etype)} ${dTable} USING ("guid")
1371
- INNER JOIN ${SQLite3Driver.escape(this.prefix + 'comparisons_' + etype)} c USING ("guid", "name")
1372
1341
  ${sortJoin}
1373
1342
  INNER JOIN (
1374
1343
  SELECT "guid"
@@ -1387,11 +1356,11 @@ class SQLite3Driver extends nymph_1.NymphDriver {
1387
1356
  ${eTable}."mdate",
1388
1357
  ${dTable}."name",
1389
1358
  ${dTable}."value",
1390
- ${cTable}."string",
1391
- ${cTable}."number"
1359
+ json(${dTable}."json") as "json",
1360
+ ${dTable}."string",
1361
+ ${dTable}."number"
1392
1362
  FROM ${SQLite3Driver.escape(this.prefix + 'entities_' + etype)} ${eTable}
1393
1363
  LEFT JOIN ${SQLite3Driver.escape(this.prefix + 'data_' + etype)} ${dTable} USING ("guid")
1394
- INNER JOIN ${SQLite3Driver.escape(this.prefix + 'comparisons_' + etype)} ${cTable} USING ("guid", "name")
1395
1364
  ${sortJoin}
1396
1365
  ${guidSelector ? `WHERE ${eTable}."guid"=${guidSelector}` : ''}
1397
1366
  ORDER BY ${sortBy}, ${eTable}."guid"`;
@@ -1434,7 +1403,9 @@ class SQLite3Driver extends nymph_1.NymphDriver {
1434
1403
  ? JSON.stringify(row.number)
1435
1404
  : row.value === 'S'
1436
1405
  ? JSON.stringify(row.string)
1437
- : row.value,
1406
+ : row.value === 'J'
1407
+ ? row.json
1408
+ : row.value,
1438
1409
  }));
1439
1410
  const value = process();
1440
1411
  if (value instanceof Error) {
@@ -1455,7 +1426,6 @@ class SQLite3Driver extends nymph_1.NymphDriver {
1455
1426
  }
1456
1427
  async importEntity({ guid, cdate, mdate, tags, sdata, etype, }) {
1457
1428
  try {
1458
- await this.startTransaction(`nymph-import-entity-${guid}`);
1459
1429
  this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} WHERE "guid"=@guid;`, {
1460
1430
  etypes: [etype],
1461
1431
  params: {
@@ -1468,12 +1438,6 @@ class SQLite3Driver extends nymph_1.NymphDriver {
1468
1438
  guid,
1469
1439
  },
1470
1440
  });
1471
- this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} WHERE "guid"=@guid;`, {
1472
- etypes: [etype],
1473
- params: {
1474
- guid,
1475
- },
1476
- });
1477
1441
  this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}references_${etype}`)} WHERE "guid"=@guid;`, {
1478
1442
  etypes: [etype],
1479
1443
  params: {
@@ -1505,23 +1469,18 @@ class SQLite3Driver extends nymph_1.NymphDriver {
1505
1469
  ? 'N'
1506
1470
  : typeof uvalue === 'string'
1507
1471
  ? 'S'
1508
- : value;
1509
- this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("guid", "name", "value") VALUES (@guid, @name, @storageValue);`, {
1472
+ : 'J';
1473
+ const jsonValue = storageValue === 'J' ? value : null;
1474
+ this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("guid", "name", "value", "json", "string", "number", "truthy") VALUES (@guid, @name, @storageValue, jsonb(@jsonValue), @string, @number, @truthy);`, {
1510
1475
  etypes: [etype],
1511
1476
  params: {
1512
1477
  guid,
1513
1478
  name,
1514
1479
  storageValue,
1515
- },
1516
- });
1517
- this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} ("guid", "name", "truthy", "string", "number") VALUES (@guid, @name, @truthy, @string, @number);`, {
1518
- etypes: [etype],
1519
- params: {
1520
- guid,
1521
- name,
1522
- truthy: uvalue ? 1 : 0,
1523
- string: `${uvalue}`,
1480
+ jsonValue,
1481
+ string: storageValue === 'J' ? null : `${uvalue}`,
1524
1482
  number: Number(uvalue),
1483
+ truthy: uvalue ? 1 : 0,
1525
1484
  },
1526
1485
  });
1527
1486
  const references = this.findReferences(value);
@@ -1556,11 +1515,9 @@ class SQLite3Driver extends nymph_1.NymphDriver {
1556
1515
  throw e;
1557
1516
  }
1558
1517
  }
1559
- await this.commit(`nymph-import-entity-${guid}`);
1560
1518
  }
1561
1519
  catch (e) {
1562
1520
  this.nymph.config.debugError('sqlite3', `Import entity error: "${e}"`);
1563
- await this.rollback(`nymph-import-entity-${guid}`);
1564
1521
  throw e;
1565
1522
  }
1566
1523
  }
@@ -1668,23 +1625,18 @@ class SQLite3Driver extends nymph_1.NymphDriver {
1668
1625
  ? 'N'
1669
1626
  : typeof value === 'string'
1670
1627
  ? 'S'
1671
- : svalue;
1672
- this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("guid", "name", "value") VALUES (@guid, @name, @storageValue);`, {
1628
+ : 'J';
1629
+ const jsonValue = storageValue === 'J' ? svalue : null;
1630
+ this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("guid", "name", "value", "json", "string", "number", "truthy") VALUES (@guid, @name, @storageValue, jsonb(@jsonValue), @string, @number, @truthy);`, {
1673
1631
  etypes: [etype],
1674
1632
  params: {
1675
1633
  guid,
1676
1634
  name,
1677
1635
  storageValue,
1678
- },
1679
- });
1680
- this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} ("guid", "name", "truthy", "string", "number") VALUES (@guid, @name, @truthy, @string, @number);`, {
1681
- etypes: [etype],
1682
- params: {
1683
- guid,
1684
- name,
1685
- truthy: value ? 1 : 0,
1686
- string: `${value}`,
1636
+ jsonValue,
1637
+ string: storageValue === 'J' ? null : `${value}`,
1687
1638
  number: Number(value),
1639
+ truthy: value ? 1 : 0,
1688
1640
  },
1689
1641
  });
1690
1642
  const references = this.findReferences(svalue);
@@ -1762,12 +1714,6 @@ class SQLite3Driver extends nymph_1.NymphDriver {
1762
1714
  guid,
1763
1715
  },
1764
1716
  });
1765
- this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} WHERE "guid"=@guid;`, {
1766
- etypes: [etype],
1767
- params: {
1768
- guid,
1769
- },
1770
- });
1771
1717
  this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}references_${etype}`)} WHERE "guid"=@guid;`, {
1772
1718
  etypes: [etype],
1773
1719
  params: {
@@ -1841,6 +1787,22 @@ class SQLite3Driver extends nymph_1.NymphDriver {
1841
1787
  this.store.transactionsStarted++;
1842
1788
  return this.nymph;
1843
1789
  }
1790
+ async needsMigration() {
1791
+ const table = this.queryGet("SELECT `name` FROM `sqlite_master` WHERE `type`='table' AND `name` LIKE @prefix LIMIT 1;", {
1792
+ params: {
1793
+ prefix: this.prefix + 'data_' + '%',
1794
+ },
1795
+ });
1796
+ if (table?.name) {
1797
+ const result = this.queryGet("SELECT 1 AS `exists` FROM pragma_table_info(@table) WHERE `name`='json';", {
1798
+ params: {
1799
+ table: table.name,
1800
+ },
1801
+ });
1802
+ return !result?.exists;
1803
+ }
1804
+ return false;
1805
+ }
1844
1806
  }
1845
1807
  exports.default = SQLite3Driver;
1846
1808
  //# sourceMappingURL=SQLite3Driver.js.map