@nymphjs/driver-mysql 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,28 @@
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 mysql and store json values there ([542d762](https://github.com/sciactive/nymphjs/commit/542d762c6f6f41bfbbe775c859e1d9df0990042b))
15
+ - add json column to sqlite and store json values there ([55a8a84](https://github.com/sciactive/nymphjs/commit/55a8a840fb7d23561d60fda602c410b79f76b0da))
16
+ - add method to detect if migration is needed ([274f7c3](https://github.com/sciactive/nymphjs/commit/274f7c39aa4e0d251a38c01593775e1270fc9621))
17
+ - move comparison fields to data table and remove comparison table ([3d7fe7e](https://github.com/sciactive/nymphjs/commit/3d7fe7e614327ecf8903ee7143e559549793e8fc))
18
+
19
+ ### BREAKING CHANGES
20
+
21
+ - This is a breaking change, and requires export and
22
+ import of the database if you are using the SQLite driver.
23
+ - This is a breaking change, and requires export and
24
+ import of the database if you are using the MySQL driver.
25
+ - This is a breaking change, and requires export and
26
+ import of the database.
27
+
6
28
  # [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
29
 
8
30
  ### Bug Fixes
@@ -104,5 +104,6 @@ export default class MySQLDriver extends NymphDriver {
104
104
  setUID(name: string, curUid: number): Promise<boolean>;
105
105
  protected internalTransaction(name: string): Promise<MySQLDriverTransaction>;
106
106
  startTransaction(name: string): Promise<import("@nymphjs/nymph").Nymph>;
107
+ needsMigration(): Promise<boolean>;
107
108
  }
108
109
  export {};
@@ -133,12 +133,10 @@ class MySQLDriver extends nymph_1.NymphDriver {
133
133
  async createTables(etype = null) {
134
134
  await this.queryRun('SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";');
135
135
  let foreignKeyDataTableGuid = '';
136
- let foreignKeyDataComparisonsTableGuid = '';
137
136
  let foreignKeyReferencesTableGuid = '';
138
137
  let foreignKeyUniquesTableGuid = '';
139
138
  if (this.config.foreignKeys) {
140
139
  foreignKeyDataTableGuid = ` REFERENCES ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)}(\`guid\`) ON DELETE CASCADE`;
141
- foreignKeyDataComparisonsTableGuid = ` REFERENCES ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)}(\`guid\`) ON DELETE CASCADE`;
142
140
  foreignKeyReferencesTableGuid = ` REFERENCES ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)}(\`guid\`) ON DELETE CASCADE`;
143
141
  foreignKeyUniquesTableGuid = ` REFERENCES ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)}(\`guid\`) ON DELETE CASCADE`;
144
142
  }
@@ -160,24 +158,15 @@ class MySQLDriver extends nymph_1.NymphDriver {
160
158
  await this.queryRun(`CREATE TABLE IF NOT EXISTS ${MySQLDriver.escape(`${this.prefix}data_${etype}`)} (
161
159
  \`guid\` BINARY(12) NOT NULL${foreignKeyDataTableGuid},
162
160
  \`name\` TEXT NOT NULL,
163
- \`value\` LONGTEXT NOT NULL,
164
- PRIMARY KEY (\`guid\`,\`name\`(255)),
165
- INDEX \`id_guid\` USING HASH (\`guid\`),
166
- INDEX \`id_guid_name\` USING HASH (\`guid\`, \`name\`(255)),
167
- INDEX \`id_guid_name_value\` USING BTREE (\`guid\`, \`name\`(255), \`value\`(510)),
168
- INDEX \`id_name_value\` USING BTREE (\`name\`(255), \`value\`(512))
169
- ) ENGINE ${this.config.engine}
170
- CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;`);
171
- // Create the data comparisons table.
172
- await this.queryRun(`CREATE TABLE IF NOT EXISTS ${MySQLDriver.escape(`${this.prefix}comparisons_${etype}`)} (
173
- \`guid\` BINARY(12) NOT NULL${foreignKeyDataComparisonsTableGuid},
174
- \`name\` TEXT NOT NULL,
175
- \`truthy\` BOOLEAN,
161
+ \`value\` CHAR(1) NOT NULL,
162
+ \`json\` JSON,
176
163
  \`string\` LONGTEXT,
177
164
  \`number\` DOUBLE,
165
+ \`truthy\` BOOLEAN,
178
166
  PRIMARY KEY (\`guid\`, \`name\`(255)),
179
167
  INDEX \`id_guid\` USING HASH (\`guid\`),
180
168
  INDEX \`id_guid_name\` USING HASH (\`guid\`, \`name\`(255)),
169
+ INDEX \`id_name\` USING BTREE (\`name\`(255)),
181
170
  INDEX \`id_name_truthy\` USING HASH (\`name\`(255), \`truthy\`),
182
171
  INDEX \`id_name_string\` USING BTREE (\`name\`(255), \`string\`(512)),
183
172
  INDEX \`id_name_number\` USING BTREE (\`name\`(255), \`number\`)
@@ -378,12 +367,6 @@ class MySQLDriver extends nymph_1.NymphDriver {
378
367
  guid,
379
368
  },
380
369
  });
381
- await this.queryRun(`DELETE FROM ${MySQLDriver.escape(`${this.prefix}comparisons_${etype}`)} WHERE \`guid\`=UNHEX(@guid);`, {
382
- etypes: [etype],
383
- params: {
384
- guid,
385
- },
386
- });
387
370
  await this.queryRun(`DELETE FROM ${MySQLDriver.escape(`${this.prefix}references_${etype}`)} WHERE \`guid\`=UNHEX(@guid);`, {
388
371
  etypes: [etype],
389
372
  params: {
@@ -464,20 +447,21 @@ class MySQLDriver extends nymph_1.NymphDriver {
464
447
  return;
465
448
  }
466
449
  // Get the etypes.
467
- const tables = await this.queryIter('SHOW TABLES;');
450
+ const tables = await this.queryIter('SELECT `table_name` AS `table_name` FROM `information_schema`.`tables` WHERE `table_schema`=@db AND `table_name` LIKE @prefix;', {
451
+ params: {
452
+ db: this.config.database,
453
+ prefix: this.prefix + 'entities_' + '%',
454
+ },
455
+ });
468
456
  const etypes = [];
469
- for (const tableRow of tables) {
470
- const table = tableRow[Object.keys(tableRow)[0]];
471
- if (table.startsWith(this.prefix + 'entities_')) {
472
- etypes.push(table.substr((this.prefix + 'entities_').length));
473
- }
457
+ for (const table of tables) {
458
+ etypes.push(table.table_name.substr((this.prefix + 'entities_').length));
474
459
  }
475
460
  for (const etype of etypes) {
476
461
  // Export entities.
477
- const dataIterator = (await this.queryIter(`SELECT LOWER(HEX(e.\`guid\`)) AS \`guid\`, e.\`tags\`, e.\`cdate\`, e.\`mdate\`, d.\`name\` AS \`dname\`, d.\`value\` AS \`dvalue\`, c.\`string\`, c.\`number\`
462
+ const dataIterator = (await this.queryIter(`SELECT LOWER(HEX(e.\`guid\`)) AS \`guid\`, e.\`tags\`, e.\`cdate\`, e.\`mdate\`, d.\`name\`, d.\`value\`, d.\`json\`, d.\`string\`, d.\`number\`
478
463
  FROM ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)} e
479
464
  LEFT JOIN ${MySQLDriver.escape(`${this.prefix}data_${etype}`)} d ON e.\`guid\`=d.\`guid\`
480
- INNER JOIN ${MySQLDriver.escape(`${this.prefix}comparisons_${etype}`)} c ON d.\`guid\`=c.\`guid\` AND d.\`name\`=c.\`name\`
481
465
  ORDER BY e.\`guid\`;`))[Symbol.iterator]();
482
466
  let datum = dataIterator.next();
483
467
  while (!datum.done) {
@@ -493,16 +477,18 @@ class MySQLDriver extends nymph_1.NymphDriver {
493
477
  currentEntityExport.push(`{${guid}}<${etype}>[${tags}]`);
494
478
  currentEntityExport.push(`\tcdate=${JSON.stringify(cdate)}`);
495
479
  currentEntityExport.push(`\tmdate=${JSON.stringify(mdate)}`);
496
- if (datum.value.dname != null) {
480
+ if (datum.value.name != null) {
497
481
  // This do will keep going and adding the data until the
498
482
  // next entity is reached. $row will end on the next entity.
499
483
  do {
500
- const value = datum.value.dvalue === 'N'
484
+ const value = datum.value.value === 'N'
501
485
  ? JSON.stringify(datum.value.number)
502
- : datum.value.dvalue === 'S'
486
+ : datum.value.value === 'S'
503
487
  ? JSON.stringify(datum.value.string)
504
- : datum.value.dvalue;
505
- currentEntityExport.push(`\t${datum.value.dname}=${value}`);
488
+ : datum.value.value === 'J'
489
+ ? JSON.stringify(datum.value.json)
490
+ : datum.value.value;
491
+ currentEntityExport.push(`\t${datum.value.name}=${value}`);
506
492
  datum = dataIterator.next();
507
493
  } while (!datum.done && datum.value.guid === guid);
508
494
  }
@@ -533,7 +519,6 @@ class MySQLDriver extends nymph_1.NymphDriver {
533
519
  }
534
520
  const eTable = `e${tableSuffix}`;
535
521
  const dTable = `d${tableSuffix}`;
536
- const cTable = `c${tableSuffix}`;
537
522
  const fTable = `f${tableSuffix}`;
538
523
  const ieTable = `ie${tableSuffix}`;
539
524
  const countTable = `count${tableSuffix}`;
@@ -643,7 +628,7 @@ class MySQLDriver extends nymph_1.NymphDriver {
643
628
  curQuery +=
644
629
  ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
645
630
  'EXISTS (SELECT `guid` FROM ' +
646
- MySQLDriver.escape(this.prefix + 'comparisons_' + etype) +
631
+ MySQLDriver.escape(this.prefix + 'data_' + etype) +
647
632
  ' WHERE `guid`=' +
648
633
  ieTable +
649
634
  '.`guid` AND `name`=@' +
@@ -694,7 +679,7 @@ class MySQLDriver extends nymph_1.NymphDriver {
694
679
  curQuery +=
695
680
  ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
696
681
  'EXISTS (SELECT `guid` FROM ' +
697
- MySQLDriver.escape(this.prefix + 'comparisons_' + etype) +
682
+ MySQLDriver.escape(this.prefix + 'data_' + etype) +
698
683
  ' WHERE `guid`=' +
699
684
  ieTable +
700
685
  '.`guid` AND `name`=@' +
@@ -714,7 +699,7 @@ class MySQLDriver extends nymph_1.NymphDriver {
714
699
  curQuery +=
715
700
  ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
716
701
  'EXISTS (SELECT `guid` FROM ' +
717
- MySQLDriver.escape(this.prefix + 'comparisons_' + etype) +
702
+ MySQLDriver.escape(this.prefix + 'data_' + etype) +
718
703
  ' WHERE `guid`=' +
719
704
  ieTable +
720
705
  '.`guid` AND `name`=@' +
@@ -747,7 +732,7 @@ class MySQLDriver extends nymph_1.NymphDriver {
747
732
  ieTable +
748
733
  '.`guid` AND `name`=@' +
749
734
  name +
750
- ' AND `value`=@' +
735
+ ' AND `json`=@' +
751
736
  value +
752
737
  ')';
753
738
  params[name] = curValue[0];
@@ -791,54 +776,26 @@ class MySQLDriver extends nymph_1.NymphDriver {
791
776
  curQuery += typeIsOr ? ' OR ' : ' AND ';
792
777
  }
793
778
  let svalue;
794
- let stringValue;
795
779
  if (curValue[1] instanceof Object &&
796
780
  typeof curValue[1].toReference === 'function') {
797
781
  svalue = JSON.stringify(curValue[1].toReference());
798
- stringValue = `${curValue[1].toReference()}`;
799
782
  }
800
783
  else {
801
784
  svalue = JSON.stringify(curValue[1]);
802
- stringValue = `${curValue[1]}`;
803
785
  }
804
786
  const name = `param${++count.i}`;
805
787
  const value = `param${++count.i}`;
806
- if (typeof curValue[1] === 'string') {
807
- const stringParam = `param${++count.i}`;
808
- curQuery +=
809
- ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
810
- '(EXISTS (SELECT `guid` FROM ' +
811
- MySQLDriver.escape(this.prefix + 'data_' + etype) +
812
- ' WHERE `guid`=' +
813
- ieTable +
814
- '.`guid` AND `name`=@' +
815
- name +
816
- ' AND INSTR(`value`, @' +
817
- value +
818
- ')) OR EXISTS (SELECT `guid` FROM ' +
819
- MySQLDriver.escape(this.prefix + 'comparisons_' + etype) +
820
- ' WHERE `guid`=' +
821
- ieTable +
822
- '.`guid` AND `name`=@' +
823
- name +
824
- ' AND `string`=@' +
825
- stringParam +
826
- '))';
827
- params[stringParam] = stringValue;
828
- }
829
- else {
830
- curQuery +=
831
- ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
832
- 'EXISTS (SELECT `guid` FROM ' +
833
- MySQLDriver.escape(this.prefix + 'data_' + etype) +
834
- ' WHERE `guid`=' +
835
- ieTable +
836
- '.`guid` AND `name`=@' +
837
- name +
838
- ' AND INSTR(`value`, @' +
839
- value +
840
- '))';
841
- }
788
+ curQuery +=
789
+ ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
790
+ 'EXISTS (SELECT `guid` FROM ' +
791
+ MySQLDriver.escape(this.prefix + 'data_' + etype) +
792
+ ' WHERE `guid`=' +
793
+ ieTable +
794
+ '.`guid` AND `name`=@' +
795
+ name +
796
+ ' AND JSON_CONTAINS(`json`, @' +
797
+ value +
798
+ '))';
842
799
  params[name] = curValue[0];
843
800
  params[value] = svalue;
844
801
  }
@@ -884,7 +841,7 @@ class MySQLDriver extends nymph_1.NymphDriver {
884
841
  curQuery +=
885
842
  ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
886
843
  'EXISTS (SELECT `guid` FROM ' +
887
- MySQLDriver.escape(this.prefix + 'comparisons_' + etype) +
844
+ MySQLDriver.escape(this.prefix + 'data_' + etype) +
888
845
  ' WHERE `guid`=' +
889
846
  ieTable +
890
847
  '.`guid` AND `name`=@' +
@@ -937,7 +894,7 @@ class MySQLDriver extends nymph_1.NymphDriver {
937
894
  curQuery +=
938
895
  ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
939
896
  'EXISTS (SELECT `guid` FROM ' +
940
- MySQLDriver.escape(this.prefix + 'comparisons_' + etype) +
897
+ MySQLDriver.escape(this.prefix + 'data_' + etype) +
941
898
  ' WHERE `guid`=' +
942
899
  ieTable +
943
900
  '.`guid` AND `name`=@' +
@@ -990,7 +947,7 @@ class MySQLDriver extends nymph_1.NymphDriver {
990
947
  curQuery +=
991
948
  ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
992
949
  'EXISTS (SELECT `guid` FROM ' +
993
- MySQLDriver.escape(this.prefix + 'comparisons_' + etype) +
950
+ MySQLDriver.escape(this.prefix + 'data_' + etype) +
994
951
  ' WHERE `guid`=' +
995
952
  ieTable +
996
953
  '.`guid` AND `name`=@' +
@@ -1043,7 +1000,7 @@ class MySQLDriver extends nymph_1.NymphDriver {
1043
1000
  curQuery +=
1044
1001
  ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
1045
1002
  'EXISTS (SELECT `guid` FROM ' +
1046
- MySQLDriver.escape(this.prefix + 'comparisons_' + etype) +
1003
+ MySQLDriver.escape(this.prefix + 'data_' + etype) +
1047
1004
  ' WHERE `guid`=' +
1048
1005
  ieTable +
1049
1006
  '.`guid` AND `name`=@' +
@@ -1096,7 +1053,7 @@ class MySQLDriver extends nymph_1.NymphDriver {
1096
1053
  curQuery +=
1097
1054
  ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
1098
1055
  'EXISTS (SELECT `guid` FROM ' +
1099
- MySQLDriver.escape(this.prefix + 'comparisons_' + etype) +
1056
+ MySQLDriver.escape(this.prefix + 'data_' + etype) +
1100
1057
  ' WHERE `guid`=' +
1101
1058
  ieTable +
1102
1059
  '.`guid` AND `name`=@' +
@@ -1151,7 +1108,7 @@ class MySQLDriver extends nymph_1.NymphDriver {
1151
1108
  curQuery +=
1152
1109
  ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
1153
1110
  'EXISTS (SELECT `guid` FROM ' +
1154
- MySQLDriver.escape(this.prefix + 'comparisons_' + etype) +
1111
+ MySQLDriver.escape(this.prefix + 'data_' + etype) +
1155
1112
  ' WHERE `guid`=' +
1156
1113
  ieTable +
1157
1114
  '.`guid` AND `name`=@' +
@@ -1206,7 +1163,7 @@ class MySQLDriver extends nymph_1.NymphDriver {
1206
1163
  curQuery +=
1207
1164
  ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
1208
1165
  'EXISTS (SELECT `guid` FROM ' +
1209
- MySQLDriver.escape(this.prefix + 'comparisons_' + etype) +
1166
+ MySQLDriver.escape(this.prefix + 'data_' + etype) +
1210
1167
  ' WHERE `guid`=' +
1211
1168
  ieTable +
1212
1169
  '.`guid` AND `name`=@' +
@@ -1261,7 +1218,7 @@ class MySQLDriver extends nymph_1.NymphDriver {
1261
1218
  curQuery +=
1262
1219
  ((0, nymph_1.xor)(typeIsNot, clauseNot) ? 'NOT ' : '') +
1263
1220
  'EXISTS (SELECT `guid` FROM ' +
1264
- MySQLDriver.escape(this.prefix + 'comparisons_' + etype) +
1221
+ MySQLDriver.escape(this.prefix + 'data_' + etype) +
1265
1222
  ' WHERE `guid`=' +
1266
1223
  ieTable +
1267
1224
  '.`guid` AND `name`=@' +
@@ -1370,13 +1327,13 @@ class MySQLDriver extends nymph_1.NymphDriver {
1370
1327
  const name = `param${++count.i}`;
1371
1328
  sortJoin = `LEFT JOIN (
1372
1329
  SELECT \`guid\`, \`string\`, \`number\`
1373
- FROM ${MySQLDriver.escape(this.prefix + 'comparisons_' + etype)}
1330
+ FROM ${MySQLDriver.escape(this.prefix + 'data_' + etype)}
1374
1331
  WHERE \`name\`=@${name}
1375
1332
  ORDER BY \`number\`${order}, \`string\`${order}
1376
1333
  ) ${sTable} ON ${eTable}.\`guid\`=${sTable}.\`guid\``;
1377
1334
  sortJoinInner = `LEFT JOIN (
1378
1335
  SELECT \`guid\`, \`string\`, \`number\`
1379
- FROM ${MySQLDriver.escape(this.prefix + 'comparisons_' + etype)}
1336
+ FROM ${MySQLDriver.escape(this.prefix + 'data_' + etype)}
1380
1337
  WHERE \`name\`=@${name}
1381
1338
  ORDER BY \`number\`${order}, \`string\`${order}
1382
1339
  ) ${sTable} ON ${ieTable}.\`guid\`=${sTable}.\`guid\``;
@@ -1435,11 +1392,11 @@ class MySQLDriver extends nymph_1.NymphDriver {
1435
1392
  ${eTable}.\`mdate\`,
1436
1393
  ${dTable}.\`name\`,
1437
1394
  ${dTable}.\`value\`,
1438
- ${cTable}.\`string\`,
1439
- ${cTable}.\`number\`
1395
+ ${dTable}.\`json\`,
1396
+ ${dTable}.\`string\`,
1397
+ ${dTable}.\`number\`
1440
1398
  FROM ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)} ${eTable}
1441
1399
  LEFT JOIN ${MySQLDriver.escape(`${this.prefix}data_${etype}`)} ${dTable} ON ${eTable}.\`guid\`=${dTable}.\`guid\`
1442
- INNER JOIN ${MySQLDriver.escape(`${this.prefix}comparisons_${etype}`)} ${cTable} ON ${dTable}.\`guid\`=${cTable}.\`guid\` AND ${dTable}.\`name\`=${cTable}.\`name\`
1443
1400
  ${sortJoin}
1444
1401
  INNER JOIN (
1445
1402
  SELECT ${ieTable}.\`guid\`
@@ -1499,11 +1456,11 @@ class MySQLDriver extends nymph_1.NymphDriver {
1499
1456
  ${eTable}.\`mdate\`,
1500
1457
  ${dTable}.\`name\`,
1501
1458
  ${dTable}.\`value\`,
1502
- ${cTable}.\`string\`,
1503
- ${cTable}.\`number\`
1459
+ ${dTable}.\`json\`,
1460
+ ${dTable}.\`string\`,
1461
+ ${dTable}.\`number\`
1504
1462
  FROM ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)} ${eTable}
1505
1463
  LEFT JOIN ${MySQLDriver.escape(`${this.prefix}data_${etype}`)} ${dTable} ON ${eTable}.\`guid\`=${dTable}.\`guid\`
1506
- INNER JOIN ${MySQLDriver.escape(`${this.prefix}comparisons_${etype}`)} ${cTable} ON ${dTable}.\`guid\`=${cTable}.\`guid\` AND ${dTable}.\`name\`=${cTable}.\`name\`
1507
1464
  ${sortJoin}
1508
1465
  INNER JOIN (
1509
1466
  SELECT ${ieTable}.\`guid\`
@@ -1522,11 +1479,11 @@ class MySQLDriver extends nymph_1.NymphDriver {
1522
1479
  ${eTable}.\`mdate\`,
1523
1480
  ${dTable}.\`name\`,
1524
1481
  ${dTable}.\`value\`,
1525
- ${cTable}.\`string\`,
1526
- ${cTable}.\`number\`
1482
+ ${dTable}.\`json\`,
1483
+ ${dTable}.\`string\`,
1484
+ ${dTable}.\`number\`
1527
1485
  FROM ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)} ${eTable}
1528
1486
  LEFT JOIN ${MySQLDriver.escape(`${this.prefix}data_${etype}`)} ${dTable} ON ${eTable}.\`guid\`=${dTable}.\`guid\`
1529
- INNER JOIN ${MySQLDriver.escape(`${this.prefix}comparisons_${etype}`)} ${cTable} ON ${dTable}.\`guid\`=${cTable}.\`guid\` AND ${dTable}.\`name\`=${cTable}.\`name\`
1530
1487
  ${sortJoin}
1531
1488
  ${guidSelector ? `WHERE ${eTable}.\`guid\`=${guidSelector}` : ''}
1532
1489
  ORDER BY ${sortBy}, ${eTable}.\`guid\``;
@@ -1571,7 +1528,9 @@ class MySQLDriver extends nymph_1.NymphDriver {
1571
1528
  ? JSON.stringify(row.number)
1572
1529
  : row.value === 'S'
1573
1530
  ? JSON.stringify(row.string)
1574
- : row.value,
1531
+ : row.value === 'J'
1532
+ ? JSON.stringify(row.json)
1533
+ : row.value,
1575
1534
  }));
1576
1535
  const result = await resultPromise;
1577
1536
  const value = process();
@@ -1593,42 +1552,42 @@ class MySQLDriver extends nymph_1.NymphDriver {
1593
1552
  }
1594
1553
  async importEntity({ guid, cdate, mdate, tags, sdata, etype, }) {
1595
1554
  try {
1596
- await this.internalTransaction(`nymph-import-entity-${guid}`);
1597
- await this.queryRun(`REPLACE INTO ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)} (\`guid\`, \`tags\`, \`cdate\`, \`mdate\`) VALUES (UNHEX(@guid), @tags, @cdate, @mdate);`, {
1555
+ let promises = [];
1556
+ promises.push(this.queryRun(`DELETE FROM ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)} WHERE \`guid\`=UNHEX(@guid);`, {
1598
1557
  etypes: [etype],
1599
1558
  params: {
1600
1559
  guid,
1601
- tags: ' ' + tags.join(' ') + ' ',
1602
- cdate: isNaN(cdate) ? null : cdate,
1603
- mdate: isNaN(mdate) ? null : mdate,
1604
1560
  },
1605
- });
1606
- const promises = [];
1561
+ }));
1607
1562
  promises.push(this.queryRun(`DELETE FROM ${MySQLDriver.escape(`${this.prefix}data_${etype}`)} WHERE \`guid\`=UNHEX(@guid);`, {
1608
1563
  etypes: [etype],
1609
1564
  params: {
1610
1565
  guid,
1611
1566
  },
1612
1567
  }));
1613
- promises.push(this.queryRun(`DELETE FROM ${MySQLDriver.escape(`${this.prefix}comparisons_${etype}`)} WHERE \`guid\`=UNHEX(@guid);`, {
1568
+ promises.push(this.queryRun(`DELETE FROM ${MySQLDriver.escape(`${this.prefix}references_${etype}`)} WHERE \`guid\`=UNHEX(@guid);`, {
1614
1569
  etypes: [etype],
1615
1570
  params: {
1616
1571
  guid,
1617
1572
  },
1618
1573
  }));
1619
- promises.push(this.queryRun(`DELETE FROM ${MySQLDriver.escape(`${this.prefix}references_${etype}`)} WHERE \`guid\`=UNHEX(@guid);`, {
1574
+ promises.push(this.queryRun(`DELETE FROM ${MySQLDriver.escape(`${this.prefix}uniques_${etype}`)} WHERE \`guid\`=UNHEX(@guid);`, {
1620
1575
  etypes: [etype],
1621
1576
  params: {
1622
1577
  guid,
1623
1578
  },
1624
1579
  }));
1625
- promises.push(this.queryRun(`DELETE FROM ${MySQLDriver.escape(`${this.prefix}uniques_${etype}`)} WHERE \`guid\`=UNHEX(@guid);`, {
1580
+ await Promise.all(promises);
1581
+ promises = [];
1582
+ await this.queryRun(`INSERT INTO ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)} (\`guid\`, \`tags\`, \`cdate\`, \`mdate\`) VALUES (UNHEX(@guid), @tags, @cdate, @mdate);`, {
1626
1583
  etypes: [etype],
1627
1584
  params: {
1628
1585
  guid,
1586
+ tags: ' ' + tags.join(' ') + ' ',
1587
+ cdate: isNaN(cdate) ? null : cdate,
1588
+ mdate: isNaN(mdate) ? null : mdate,
1629
1589
  },
1630
- }));
1631
- await Promise.all(promises);
1590
+ });
1632
1591
  for (const name in sdata) {
1633
1592
  const value = sdata[name];
1634
1593
  const uvalue = JSON.parse(value);
@@ -1639,24 +1598,18 @@ class MySQLDriver extends nymph_1.NymphDriver {
1639
1598
  ? 'N'
1640
1599
  : typeof uvalue === 'string'
1641
1600
  ? 'S'
1642
- : value;
1643
- const promises = [];
1644
- promises.push(this.queryRun(`INSERT INTO ${MySQLDriver.escape(`${this.prefix}data_${etype}`)} (\`guid\`, \`name\`, \`value\`) VALUES (UNHEX(@guid), @name, @storageValue);`, {
1601
+ : 'J';
1602
+ const jsonValue = storageValue === 'J' ? value : null;
1603
+ promises.push(this.queryRun(`INSERT INTO ${MySQLDriver.escape(`${this.prefix}data_${etype}`)} (\`guid\`, \`name\`, \`value\`, \`json\`, \`string\`, \`number\`, \`truthy\`) VALUES (UNHEX(@guid), @name, @storageValue, @jsonValue, @string, @number, @truthy);`, {
1645
1604
  etypes: [etype],
1646
1605
  params: {
1647
1606
  guid,
1648
1607
  name,
1649
1608
  storageValue,
1650
- },
1651
- }));
1652
- promises.push(this.queryRun(`INSERT INTO ${MySQLDriver.escape(`${this.prefix}comparisons_${etype}`)} (\`guid\`, \`name\`, \`truthy\`, \`string\`, \`number\`) VALUES (UNHEX(@guid), @name, @truthy, @string, @number);`, {
1653
- etypes: [etype],
1654
- params: {
1655
- guid,
1656
- name,
1657
- truthy: !!uvalue,
1658
- string: `${uvalue}`,
1609
+ jsonValue,
1610
+ string: storageValue === 'J' ? null : `${uvalue}`,
1659
1611
  number: isNaN(Number(uvalue)) ? null : Number(uvalue),
1612
+ truthy: !!uvalue,
1660
1613
  },
1661
1614
  }));
1662
1615
  const references = this.findReferences(value);
@@ -1689,11 +1642,9 @@ class MySQLDriver extends nymph_1.NymphDriver {
1689
1642
  }));
1690
1643
  }
1691
1644
  await Promise.all(promises);
1692
- await this.commit(`nymph-import-entity-${guid}`);
1693
1645
  }
1694
1646
  catch (e) {
1695
1647
  this.nymph.config.debugError('mysql', `Import entity error: "${e}"`);
1696
- await this.rollback(`nymph-import-entity-${guid}`);
1697
1648
  throw e;
1698
1649
  }
1699
1650
  }
@@ -1791,24 +1742,19 @@ class MySQLDriver extends nymph_1.NymphDriver {
1791
1742
  ? 'N'
1792
1743
  : typeof value === 'string'
1793
1744
  ? 'S'
1794
- : svalue;
1745
+ : 'J';
1746
+ const jsonValue = storageValue === 'J' ? svalue : null;
1795
1747
  const promises = [];
1796
- promises.push(this.queryRun(`INSERT INTO ${MySQLDriver.escape(`${this.prefix}data_${etype}`)} (\`guid\`, \`name\`, \`value\`) VALUES (UNHEX(@guid), @name, @storageValue);`, {
1748
+ promises.push(this.queryRun(`INSERT INTO ${MySQLDriver.escape(`${this.prefix}data_${etype}`)} (\`guid\`, \`name\`, \`value\`, \`json\`, \`string\`, \`number\`, \`truthy\`) VALUES (UNHEX(@guid), @name, @storageValue, @jsonValue, @string, @number, @truthy);`, {
1797
1749
  etypes: [etype],
1798
1750
  params: {
1799
1751
  guid,
1800
1752
  name,
1801
1753
  storageValue,
1802
- },
1803
- }));
1804
- promises.push(this.queryRun(`INSERT INTO ${MySQLDriver.escape(`${this.prefix}comparisons_${etype}`)} (\`guid\`, \`name\`, \`truthy\`, \`string\`, \`number\`) VALUES (UNHEX(@guid), @name, @truthy, @string, @number);`, {
1805
- etypes: [etype],
1806
- params: {
1807
- guid,
1808
- name,
1809
- truthy: !!value,
1810
- string: `${value}`,
1754
+ jsonValue,
1755
+ string: storageValue === 'J' ? null : `${value}`,
1811
1756
  number: isNaN(Number(value)) ? null : Number(value),
1757
+ truthy: !!value,
1812
1758
  },
1813
1759
  }));
1814
1760
  const references = this.findReferences(svalue);
@@ -1884,12 +1830,6 @@ class MySQLDriver extends nymph_1.NymphDriver {
1884
1830
  guid,
1885
1831
  },
1886
1832
  }));
1887
- promises.push(this.queryRun(`SELECT 1 FROM ${MySQLDriver.escape(`${this.prefix}comparisons_${etype}`)} WHERE \`guid\`=UNHEX(@guid) GROUP BY 1 FOR UPDATE;`, {
1888
- etypes: [etype],
1889
- params: {
1890
- guid,
1891
- },
1892
- }));
1893
1833
  promises.push(this.queryRun(`SELECT 1 FROM ${MySQLDriver.escape(`${this.prefix}references_${etype}`)} WHERE \`guid\`=UNHEX(@guid) GROUP BY 1 FOR UPDATE;`, {
1894
1834
  etypes: [etype],
1895
1835
  params: {
@@ -1905,7 +1845,7 @@ class MySQLDriver extends nymph_1.NymphDriver {
1905
1845
  await Promise.all(promises);
1906
1846
  }
1907
1847
  if (this.config.tableLocking) {
1908
- await this.queryRun(`LOCK TABLES ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)} WRITE, ${MySQLDriver.escape(`${this.prefix}data_${etype}`)} WRITE, ${MySQLDriver.escape(`${this.prefix}comparisons_${etype}`)} WRITE, ${MySQLDriver.escape(`${this.prefix}references_${etype}`)} WRITE, ${MySQLDriver.escape(`${this.prefix}uniques_${etype}`)} WRITE;`);
1848
+ await this.queryRun(`LOCK TABLES ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)} WRITE, ${MySQLDriver.escape(`${this.prefix}data_${etype}`)} WRITE, ${MySQLDriver.escape(`${this.prefix}references_${etype}`)} WRITE, ${MySQLDriver.escape(`${this.prefix}uniques_${etype}`)} WRITE;`);
1909
1849
  }
1910
1850
  const info = await this.queryRun(`UPDATE ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)} SET \`tags\`=@tags, \`mdate\`=@mdate WHERE \`guid\`=UNHEX(@guid) AND \`mdate\` <= @emdate;`, {
1911
1851
  etypes: [etype],
@@ -1925,12 +1865,6 @@ class MySQLDriver extends nymph_1.NymphDriver {
1925
1865
  guid,
1926
1866
  },
1927
1867
  }));
1928
- promises.push(this.queryRun(`DELETE FROM ${MySQLDriver.escape(`${this.prefix}comparisons_${etype}`)} WHERE \`guid\`=UNHEX(@guid);`, {
1929
- etypes: [etype],
1930
- params: {
1931
- guid,
1932
- },
1933
- }));
1934
1868
  promises.push(this.queryRun(`DELETE FROM ${MySQLDriver.escape(`${this.prefix}references_${etype}`)} WHERE \`guid\`=UNHEX(@guid);`, {
1935
1869
  etypes: [etype],
1936
1870
  params: {
@@ -2019,6 +1953,24 @@ class MySQLDriver extends nymph_1.NymphDriver {
2019
1953
  nymph.driver.transaction = transaction;
2020
1954
  return nymph;
2021
1955
  }
1956
+ async needsMigration() {
1957
+ const table = await this.queryGet('SELECT `table_name` AS `table_name` FROM `information_schema`.`tables` WHERE `table_schema`=@db AND `table_name` LIKE @prefix LIMIT 1;', {
1958
+ params: {
1959
+ db: this.config.database,
1960
+ prefix: this.prefix + 'data_' + '%',
1961
+ },
1962
+ });
1963
+ if (table?.table_name) {
1964
+ const result = await this.queryGet("SELECT 1 AS `exists` FROM `information_schema`.`columns` WHERE `table_schema`=@db AND `table_name`=@table AND `column_name`='json';", {
1965
+ params: {
1966
+ db: this.config.database,
1967
+ table: table.table_name,
1968
+ },
1969
+ });
1970
+ return !result?.exists;
1971
+ }
1972
+ return false;
1973
+ }
2022
1974
  }
2023
1975
  exports.default = MySQLDriver;
2024
1976
  //# sourceMappingURL=MySQLDriver.js.map