@nymphjs/driver-postgresql 1.0.0-beta.76 → 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.
@@ -84,8 +84,8 @@ export default class PostgreSQLDriver extends NymphDriver {
84
84
 
85
85
  static unescapeNulls(input: string) {
86
86
  return input
87
- .replace(/\uFFFD\uFFFD/g, () => '\uFFFD')
88
- .replace(/-\uFFFD-/g, () => '\x00');
87
+ .replace(/-\uFFFD-/g, () => '\x00')
88
+ .replace(/\uFFFD\uFFFD/g, () => '\uFFFD');
89
89
  }
90
90
 
91
91
  constructor(
@@ -312,6 +312,9 @@ export default class PostgreSQLDriver extends NymphDriver {
312
312
  "name" TEXT NOT NULL,
313
313
  "value" CHARACTER(1) NOT NULL,
314
314
  "json" JSONB,
315
+ "string" TEXT,
316
+ "number" DOUBLE PRECISION,
317
+ "truthy" BOOLEAN,
315
318
  PRIMARY KEY ("guid", "name"),
316
319
  FOREIGN KEY ("guid")
317
320
  REFERENCES ${PostgreSQLDriver.escape(
@@ -342,16 +345,16 @@ export default class PostgreSQLDriver extends NymphDriver {
342
345
  );
343
346
  await this.queryRun(
344
347
  `DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(
345
- `${this.prefix}data_${etype}_id_name`,
348
+ `${this.prefix}data_${etype}_id_guid_name`,
346
349
  )};`,
347
350
  { connection },
348
351
  );
349
352
  await this.queryRun(
350
353
  `CREATE INDEX ${PostgreSQLDriver.escape(
351
- `${this.prefix}data_${etype}_id_name`,
354
+ `${this.prefix}data_${etype}_id_guid_name`,
352
355
  )} ON ${PostgreSQLDriver.escape(
353
356
  `${this.prefix}data_${etype}`,
354
- )} USING btree ("name");`,
357
+ )} USING btree ("guid", "name");`,
355
358
  { connection },
356
359
  );
357
360
  await this.queryRun(
@@ -384,146 +387,100 @@ export default class PostgreSQLDriver extends NymphDriver {
384
387
  );
385
388
  await this.queryRun(
386
389
  `DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(
387
- `${this.prefix}data_${etype}_id_name_value`,
390
+ `${this.prefix}data_${etype}_id_name`,
388
391
  )};`,
389
392
  { connection },
390
393
  );
391
394
  await this.queryRun(
392
395
  `CREATE INDEX ${PostgreSQLDriver.escape(
393
- `${this.prefix}data_${etype}_id_name_value`,
396
+ `${this.prefix}data_${etype}_id_name`,
394
397
  )} ON ${PostgreSQLDriver.escape(
395
398
  `${this.prefix}data_${etype}`,
396
- )} USING btree ("name", "value");`,
399
+ )} USING btree ("name");`,
397
400
  { connection },
398
401
  );
399
402
  await this.queryRun(
400
403
  `DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(
401
- `${this.prefix}data_${etype}_id_json`,
404
+ `${this.prefix}data_${etype}_id_name_string`,
402
405
  )};`,
403
406
  { connection },
404
407
  );
405
408
  await this.queryRun(
406
409
  `CREATE INDEX ${PostgreSQLDriver.escape(
407
- `${this.prefix}data_${etype}_id_json`,
410
+ `${this.prefix}data_${etype}_id_name_string`,
408
411
  )} ON ${PostgreSQLDriver.escape(
409
412
  `${this.prefix}data_${etype}`,
410
- )} USING gin ("json");`,
411
- { connection },
412
- );
413
- // Create the data comparisons table.
414
- await this.queryRun(
415
- `CREATE TABLE IF NOT EXISTS ${PostgreSQLDriver.escape(
416
- `${this.prefix}comparisons_${etype}`,
417
- )} (
418
- "guid" BYTEA NOT NULL,
419
- "name" TEXT NOT NULL,
420
- "truthy" BOOLEAN,
421
- "string" TEXT,
422
- "number" DOUBLE PRECISION,
423
- PRIMARY KEY ("guid", "name"),
424
- FOREIGN KEY ("guid")
425
- REFERENCES ${PostgreSQLDriver.escape(
426
- `${this.prefix}entities_${etype}`,
427
- )} ("guid") MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE
428
- ) WITH ( OIDS=FALSE );`,
429
- { connection },
430
- );
431
- await this.queryRun(
432
- `ALTER TABLE ${PostgreSQLDriver.escape(
433
- `${this.prefix}comparisons_${etype}`,
434
- )} OWNER TO ${PostgreSQLDriver.escape(this.config.user)};`,
435
- { connection },
436
- );
437
- await this.queryRun(
438
- `DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(
439
- `${this.prefix}comparisons_${etype}_id_guid`,
440
- )};`,
441
- { connection },
442
- );
443
- await this.queryRun(
444
- `CREATE INDEX ${PostgreSQLDriver.escape(
445
- `${this.prefix}comparisons_${etype}_id_guid`,
446
- )} ON ${PostgreSQLDriver.escape(
447
- `${this.prefix}comparisons_${etype}`,
448
- )} USING btree ("guid");`,
413
+ )} USING btree ("name", LEFT("string", 512));`,
449
414
  { connection },
450
415
  );
451
416
  await this.queryRun(
452
417
  `DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(
453
- `${this.prefix}comparisons_${etype}_id_name`,
418
+ `${this.prefix}data_${etype}_id_name_number`,
454
419
  )};`,
455
420
  { connection },
456
421
  );
457
422
  await this.queryRun(
458
423
  `CREATE INDEX ${PostgreSQLDriver.escape(
459
- `${this.prefix}comparisons_${etype}_id_name`,
424
+ `${this.prefix}data_${etype}_id_name_number`,
460
425
  )} ON ${PostgreSQLDriver.escape(
461
- `${this.prefix}comparisons_${etype}`,
462
- )} USING btree ("name");`,
426
+ `${this.prefix}data_${etype}`,
427
+ )} USING btree ("name", "number");`,
463
428
  { connection },
464
429
  );
465
430
  await this.queryRun(
466
431
  `DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(
467
- `${this.prefix}comparisons_${etype}_id_name_truthy`,
432
+ `${this.prefix}data_${etype}_id_name_truthy`,
468
433
  )};`,
469
434
  { connection },
470
435
  );
471
436
  await this.queryRun(
472
437
  `CREATE INDEX ${PostgreSQLDriver.escape(
473
- `${this.prefix}comparisons_${etype}_id_name_truthy`,
438
+ `${this.prefix}data_${etype}_id_name_truthy`,
474
439
  )} ON ${PostgreSQLDriver.escape(
475
- `${this.prefix}comparisons_${etype}`,
440
+ `${this.prefix}data_${etype}`,
476
441
  )} USING btree ("name") WHERE "truthy" = TRUE;`,
477
442
  { connection },
478
443
  );
479
444
  await this.queryRun(
480
445
  `DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(
481
- `${this.prefix}comparisons_${etype}_id_name_falsy`,
446
+ `${this.prefix}data_${etype}_id_name_falsy`,
482
447
  )};`,
483
448
  { connection },
484
449
  );
485
450
  await this.queryRun(
486
451
  `CREATE INDEX ${PostgreSQLDriver.escape(
487
- `${this.prefix}comparisons_${etype}_id_name_falsy`,
452
+ `${this.prefix}data_${etype}_id_name_falsy`,
488
453
  )} ON ${PostgreSQLDriver.escape(
489
- `${this.prefix}comparisons_${etype}`,
454
+ `${this.prefix}data_${etype}`,
490
455
  )} USING btree ("name") WHERE "truthy" <> TRUE;`,
491
456
  { connection },
492
457
  );
493
458
  await this.queryRun(
494
459
  `DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(
495
- `${this.prefix}comparisons_${etype}_id_name_string`,
460
+ `${this.prefix}data_${etype}_id_string`,
496
461
  )};`,
497
462
  { connection },
498
463
  );
499
464
  await this.queryRun(
500
465
  `CREATE INDEX ${PostgreSQLDriver.escape(
501
- `${this.prefix}comparisons_${etype}_id_name_string`,
502
- )} ON ${PostgreSQLDriver.escape(
503
- `${this.prefix}comparisons_${etype}`,
504
- )} USING btree ("name", LEFT("string", 512));`,
505
- { connection },
506
- );
507
- await this.queryRun(
508
- `CREATE INDEX ${PostgreSQLDriver.escape(
509
- `${this.prefix}comparisons_${etype}_id_stringgin`,
466
+ `${this.prefix}data_${etype}_id_string`,
510
467
  )} ON ${PostgreSQLDriver.escape(
511
- `${this.prefix}comparisons_${etype}`,
468
+ `${this.prefix}data_${etype}`,
512
469
  )} USING gin ("string" gin_trgm_ops);`,
513
470
  { connection },
514
471
  );
515
472
  await this.queryRun(
516
473
  `DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(
517
- `${this.prefix}comparisons_${etype}_id_name_number`,
474
+ `${this.prefix}data_${etype}_id_json`,
518
475
  )};`,
519
476
  { connection },
520
477
  );
521
478
  await this.queryRun(
522
479
  `CREATE INDEX ${PostgreSQLDriver.escape(
523
- `${this.prefix}comparisons_${etype}_id_name_number`,
480
+ `${this.prefix}data_${etype}_id_json`,
524
481
  )} ON ${PostgreSQLDriver.escape(
525
- `${this.prefix}comparisons_${etype}`,
526
- )} USING btree ("name", "number");`,
482
+ `${this.prefix}data_${etype}`,
483
+ )} USING gin ("json");`,
527
484
  { connection },
528
485
  );
529
486
  // Create the references table.
@@ -861,17 +818,6 @@ export default class PostgreSQLDriver extends NymphDriver {
861
818
  },
862
819
  },
863
820
  );
864
- await this.queryRun(
865
- `DELETE FROM ${PostgreSQLDriver.escape(
866
- `${this.prefix}comparisons_${etype}`,
867
- )} WHERE "guid"=decode(@guid, 'hex');`,
868
- {
869
- etypes: [etype],
870
- params: {
871
- guid,
872
- },
873
- },
874
- );
875
821
  await this.queryRun(
876
822
  `DELETE FROM ${PostgreSQLDriver.escape(
877
823
  `${this.prefix}references_${etype}`,
@@ -988,28 +934,28 @@ export default class PostgreSQLDriver extends NymphDriver {
988
934
 
989
935
  // Get the etypes.
990
936
  const tables = await this.queryIter(
991
- 'SELECT relname FROM pg_stat_user_tables ORDER BY relname;',
937
+ 'SELECT "table_name" AS "table_name" FROM "information_schema"."tables" WHERE "table_catalog"=@db AND "table_schema"=\'public\' AND "table_name" LIKE @prefix;',
938
+ {
939
+ params: {
940
+ db: this.config.database,
941
+ prefix: this.prefix + 'entities_' + '%',
942
+ },
943
+ },
992
944
  );
993
945
  const etypes = [];
994
- for (const tableRow of tables) {
995
- const table = tableRow.relname;
996
- if (table.startsWith(this.prefix + 'entities_')) {
997
- etypes.push(table.substr((this.prefix + 'entities_').length));
998
- }
946
+ for (const table of tables) {
947
+ etypes.push(table.table_name.substr((this.prefix + 'entities_').length));
999
948
  }
1000
949
 
1001
950
  for (const etype of etypes) {
1002
951
  // Export entities.
1003
952
  const dataIterator = (
1004
953
  await this.queryIter(
1005
- `SELECT encode(e."guid", 'hex') AS "guid", e."tags", e."cdate", e."mdate", d."name" AS "dname", d."value" AS "dvalue", d."json" AS "djson", c."string", c."number"
954
+ `SELECT encode(e."guid", 'hex') AS "guid", e."tags", e."cdate", e."mdate", d."name", d."value", d."json", d."string", d."number"
1006
955
  FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} e
1007
956
  LEFT JOIN ${PostgreSQLDriver.escape(
1008
957
  `${this.prefix}data_${etype}`,
1009
958
  )} d ON e."guid"=d."guid"
1010
- INNER JOIN ${PostgreSQLDriver.escape(
1011
- `${this.prefix}comparisons_${etype}`,
1012
- )} c ON d."guid"=c."guid" AND d."name"=c."name"
1013
959
  ORDER BY e."guid";`,
1014
960
  )
1015
961
  )[Symbol.iterator]();
@@ -1023,23 +969,23 @@ export default class PostgreSQLDriver extends NymphDriver {
1023
969
  currentEntityExport.push(`{${guid}}<${etype}>[${tags}]`);
1024
970
  currentEntityExport.push(`\tcdate=${JSON.stringify(cdate)}`);
1025
971
  currentEntityExport.push(`\tmdate=${JSON.stringify(mdate)}`);
1026
- if (datum.value.dname != null) {
972
+ if (datum.value.name != null) {
1027
973
  // This do will keep going and adding the data until the
1028
974
  // next entity is reached. $row will end on the next entity.
1029
975
  do {
1030
976
  const value =
1031
- datum.value.dvalue === 'N'
977
+ datum.value.value === 'N'
1032
978
  ? JSON.stringify(Number(datum.value.number))
1033
- : datum.value.dvalue === 'S'
979
+ : datum.value.value === 'S'
1034
980
  ? JSON.stringify(
1035
981
  PostgreSQLDriver.unescapeNulls(datum.value.string),
1036
982
  )
1037
- : datum.value.dvalue === 'J'
983
+ : datum.value.value === 'J'
1038
984
  ? PostgreSQLDriver.unescapeNullSequences(
1039
- JSON.stringify(datum.value.djson),
985
+ JSON.stringify(datum.value.json),
1040
986
  )
1041
- : datum.value.dvalue;
1042
- currentEntityExport.push(`\t${datum.value.dname}=${value}`);
987
+ : datum.value.value;
988
+ currentEntityExport.push(`\t${datum.value.name}=${value}`);
1043
989
  datum = dataIterator.next();
1044
990
  } while (!datum.done && datum.value.guid === guid);
1045
991
  } else {
@@ -1081,7 +1027,6 @@ export default class PostgreSQLDriver extends NymphDriver {
1081
1027
  }
1082
1028
  const eTable = `e${tableSuffix}`;
1083
1029
  const dTable = `d${tableSuffix}`;
1084
- const cTable = `c${tableSuffix}`;
1085
1030
  const fTable = `f${tableSuffix}`;
1086
1031
  const ieTable = `ie${tableSuffix}`;
1087
1032
  const countTable = `count${tableSuffix}`;
@@ -1169,9 +1114,7 @@ export default class PostgreSQLDriver extends NymphDriver {
1169
1114
  curQuery +=
1170
1115
  (xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
1171
1116
  'EXISTS (SELECT "guid" FROM ' +
1172
- PostgreSQLDriver.escape(
1173
- this.prefix + 'comparisons_' + etype,
1174
- ) +
1117
+ PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
1175
1118
  ' WHERE "guid"=' +
1176
1119
  ieTable +
1177
1120
  '."guid" AND "name"=@' +
@@ -1220,9 +1163,7 @@ export default class PostgreSQLDriver extends NymphDriver {
1220
1163
  curQuery +=
1221
1164
  (xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
1222
1165
  'EXISTS (SELECT "guid" FROM ' +
1223
- PostgreSQLDriver.escape(
1224
- this.prefix + 'comparisons_' + etype,
1225
- ) +
1166
+ PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
1226
1167
  ' WHERE "guid"=' +
1227
1168
  ieTable +
1228
1169
  '."guid" AND "name"=@' +
@@ -1241,9 +1182,7 @@ export default class PostgreSQLDriver extends NymphDriver {
1241
1182
  curQuery +=
1242
1183
  (xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
1243
1184
  'EXISTS (SELECT "guid" FROM ' +
1244
- PostgreSQLDriver.escape(
1245
- this.prefix + 'comparisons_' + etype,
1246
- ) +
1185
+ PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
1247
1186
  ' WHERE "guid"=' +
1248
1187
  ieTable +
1249
1188
  '."guid" AND "name"=@' +
@@ -1278,7 +1217,7 @@ export default class PostgreSQLDriver extends NymphDriver {
1278
1217
  ieTable +
1279
1218
  '."guid" AND "name"=@' +
1280
1219
  name +
1281
- ' AND "json"=' +
1220
+ ' AND "json"=@' +
1282
1221
  value +
1283
1222
  ')';
1284
1223
  params[name] = curValue[0];
@@ -1295,7 +1234,7 @@ export default class PostgreSQLDriver extends NymphDriver {
1295
1234
  curQuery +=
1296
1235
  (xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
1297
1236
  ieTable +
1298
- '."cdate"=' +
1237
+ '."cdate"=@' +
1299
1238
  cdate;
1300
1239
  params[cdate] = isNaN(Number(curValue[1]))
1301
1240
  ? null
@@ -1309,7 +1248,7 @@ export default class PostgreSQLDriver extends NymphDriver {
1309
1248
  curQuery +=
1310
1249
  (xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
1311
1250
  ieTable +
1312
- '."mdate"=' +
1251
+ '."mdate"=@' +
1313
1252
  mdate;
1314
1253
  params[mdate] = isNaN(Number(curValue[1]))
1315
1254
  ? null
@@ -1389,9 +1328,7 @@ export default class PostgreSQLDriver extends NymphDriver {
1389
1328
  curQuery +=
1390
1329
  (xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
1391
1330
  'EXISTS (SELECT "guid" FROM ' +
1392
- PostgreSQLDriver.escape(
1393
- this.prefix + 'comparisons_' + etype,
1394
- ) +
1331
+ PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
1395
1332
  ' WHERE "guid"=' +
1396
1333
  ieTable +
1397
1334
  '."guid" AND "name"=@' +
@@ -1442,9 +1379,7 @@ export default class PostgreSQLDriver extends NymphDriver {
1442
1379
  curQuery +=
1443
1380
  (xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
1444
1381
  'EXISTS (SELECT "guid" FROM ' +
1445
- PostgreSQLDriver.escape(
1446
- this.prefix + 'comparisons_' + etype,
1447
- ) +
1382
+ PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
1448
1383
  ' WHERE "guid"=' +
1449
1384
  ieTable +
1450
1385
  '."guid" AND "name"=@' +
@@ -1495,9 +1430,7 @@ export default class PostgreSQLDriver extends NymphDriver {
1495
1430
  curQuery +=
1496
1431
  (xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
1497
1432
  'EXISTS (SELECT "guid" FROM ' +
1498
- PostgreSQLDriver.escape(
1499
- this.prefix + 'comparisons_' + etype,
1500
- ) +
1433
+ PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
1501
1434
  ' WHERE "guid"=' +
1502
1435
  ieTable +
1503
1436
  '."guid" AND "name"=@' +
@@ -1548,9 +1481,7 @@ export default class PostgreSQLDriver extends NymphDriver {
1548
1481
  curQuery +=
1549
1482
  (xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
1550
1483
  'EXISTS (SELECT "guid" FROM ' +
1551
- PostgreSQLDriver.escape(
1552
- this.prefix + 'comparisons_' + etype,
1553
- ) +
1484
+ PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
1554
1485
  ' WHERE "guid"=' +
1555
1486
  ieTable +
1556
1487
  '."guid" AND "name"=@' +
@@ -1601,9 +1532,7 @@ export default class PostgreSQLDriver extends NymphDriver {
1601
1532
  curQuery +=
1602
1533
  (xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
1603
1534
  'EXISTS (SELECT "guid" FROM ' +
1604
- PostgreSQLDriver.escape(
1605
- this.prefix + 'comparisons_' + etype,
1606
- ) +
1535
+ PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
1607
1536
  ' WHERE "guid"=' +
1608
1537
  ieTable +
1609
1538
  '."guid" AND "name"=@' +
@@ -1656,9 +1585,7 @@ export default class PostgreSQLDriver extends NymphDriver {
1656
1585
  curQuery +=
1657
1586
  (xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
1658
1587
  'EXISTS (SELECT "guid" FROM ' +
1659
- PostgreSQLDriver.escape(
1660
- this.prefix + 'comparisons_' + etype,
1661
- ) +
1588
+ PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
1662
1589
  ' WHERE "guid"=' +
1663
1590
  ieTable +
1664
1591
  '."guid" AND "name"=@' +
@@ -1711,9 +1638,7 @@ export default class PostgreSQLDriver extends NymphDriver {
1711
1638
  curQuery +=
1712
1639
  (xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
1713
1640
  'EXISTS (SELECT "guid" FROM ' +
1714
- PostgreSQLDriver.escape(
1715
- this.prefix + 'comparisons_' + etype,
1716
- ) +
1641
+ PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
1717
1642
  ' WHERE "guid"=' +
1718
1643
  ieTable +
1719
1644
  '."guid" AND "name"=@' +
@@ -1766,9 +1691,7 @@ export default class PostgreSQLDriver extends NymphDriver {
1766
1691
  curQuery +=
1767
1692
  (xor(typeIsNot, clauseNot) ? 'NOT ' : '') +
1768
1693
  'EXISTS (SELECT "guid" FROM ' +
1769
- PostgreSQLDriver.escape(
1770
- this.prefix + 'comparisons_' + etype,
1771
- ) +
1694
+ PostgreSQLDriver.escape(this.prefix + 'data_' + etype) +
1772
1695
  ' WHERE "guid"=' +
1773
1696
  ieTable +
1774
1697
  '."guid" AND "name"=@' +
@@ -1899,17 +1822,13 @@ export default class PostgreSQLDriver extends NymphDriver {
1899
1822
  const name = `param${++count.i}`;
1900
1823
  sortJoin = `LEFT JOIN (
1901
1824
  SELECT "guid", "string", "number"
1902
- FROM ${PostgreSQLDriver.escape(
1903
- this.prefix + 'comparisons_' + etype,
1904
- )}
1825
+ FROM ${PostgreSQLDriver.escape(this.prefix + 'data_' + etype)}
1905
1826
  WHERE "name"=@${name}
1906
1827
  ORDER BY "number"${order}, "string"${order}
1907
1828
  ) ${sTable} ON ${eTable}."guid"=${sTable}."guid"`;
1908
1829
  sortJoinInner = `LEFT JOIN (
1909
1830
  SELECT "guid", "string", "number"
1910
- FROM ${PostgreSQLDriver.escape(
1911
- this.prefix + 'comparisons_' + etype,
1912
- )}
1831
+ FROM ${PostgreSQLDriver.escape(this.prefix + 'data_' + etype)}
1913
1832
  WHERE "name"=@${name}
1914
1833
  ORDER BY "number"${order}, "string"${order}
1915
1834
  ) ${sTable} ON ${ieTable}."guid"=${sTable}."guid"`;
@@ -1977,17 +1896,14 @@ export default class PostgreSQLDriver extends NymphDriver {
1977
1896
  ${dTable}."name",
1978
1897
  ${dTable}."value",
1979
1898
  ${dTable}."json",
1980
- ${cTable}."string",
1981
- ${cTable}."number"
1899
+ ${dTable}."string",
1900
+ ${dTable}."number"
1982
1901
  FROM ${PostgreSQLDriver.escape(
1983
1902
  `${this.prefix}entities_${etype}`,
1984
1903
  )} ${eTable}
1985
1904
  LEFT JOIN ${PostgreSQLDriver.escape(
1986
1905
  `${this.prefix}data_${etype}`,
1987
1906
  )} ${dTable} ON ${eTable}."guid"=${dTable}."guid"
1988
- INNER JOIN ${PostgreSQLDriver.escape(
1989
- `${this.prefix}comparisons_${etype}`,
1990
- )} ${cTable} ON ${dTable}."guid"=${cTable}."guid" AND ${dTable}."name"=${cTable}."name"
1991
1907
  ${sortJoin}
1992
1908
  INNER JOIN (
1993
1909
  SELECT ${ieTable}."guid"
@@ -2056,17 +1972,14 @@ export default class PostgreSQLDriver extends NymphDriver {
2056
1972
  ${dTable}."name",
2057
1973
  ${dTable}."value",
2058
1974
  ${dTable}."json",
2059
- ${cTable}."string",
2060
- ${cTable}."number"
1975
+ ${dTable}."string",
1976
+ ${dTable}."number"
2061
1977
  FROM ${PostgreSQLDriver.escape(
2062
1978
  `${this.prefix}entities_${etype}`,
2063
1979
  )} ${eTable}
2064
1980
  LEFT JOIN ${PostgreSQLDriver.escape(
2065
1981
  `${this.prefix}data_${etype}`,
2066
1982
  )} ${dTable} ON ${eTable}."guid"=${dTable}."guid"
2067
- INNER JOIN ${PostgreSQLDriver.escape(
2068
- `${this.prefix}comparisons_${etype}`,
2069
- )} ${cTable} ON ${dTable}."guid"=${cTable}."guid" AND ${dTable}."name"=${cTable}."name"
2070
1983
  ${sortJoin}
2071
1984
  INNER JOIN (
2072
1985
  SELECT ${ieTable}."guid"
@@ -2087,17 +2000,14 @@ export default class PostgreSQLDriver extends NymphDriver {
2087
2000
  ${dTable}."name",
2088
2001
  ${dTable}."value",
2089
2002
  ${dTable}."json",
2090
- ${cTable}."string",
2091
- ${cTable}."number"
2003
+ ${dTable}."string",
2004
+ ${dTable}."number"
2092
2005
  FROM ${PostgreSQLDriver.escape(
2093
2006
  `${this.prefix}entities_${etype}`,
2094
2007
  )} ${eTable}
2095
2008
  LEFT JOIN ${PostgreSQLDriver.escape(
2096
2009
  `${this.prefix}data_${etype}`,
2097
2010
  )} ${dTable} ON ${eTable}."guid"=${dTable}."guid"
2098
- INNER JOIN ${PostgreSQLDriver.escape(
2099
- `${this.prefix}comparisons_${etype}`,
2100
- )} ${cTable} ON ${dTable}."guid"=${cTable}."guid" AND ${dTable}."name"=${cTable}."name"
2101
2011
  ${sortJoin}
2102
2012
  ${guidSelector ? `WHERE ${eTable}."guid"=${guidSelector}` : ''}
2103
2013
  ORDER BY ${sortBy}, ${eTable}."guid"`;
@@ -2225,38 +2135,11 @@ export default class PostgreSQLDriver extends NymphDriver {
2225
2135
  etype: string;
2226
2136
  }) {
2227
2137
  try {
2228
- await this.internalTransaction(`nymph-import-entity-${guid}`);
2229
-
2230
- await this.queryRun(
2231
- `DELETE FROM ${PostgreSQLDriver.escape(
2232
- `${this.prefix}entities_${etype}`,
2233
- )} WHERE "guid"=decode(@guid, 'hex');`,
2234
- {
2235
- etypes: [etype],
2236
- params: {
2237
- guid,
2238
- },
2239
- },
2240
- );
2241
- await this.queryRun(
2242
- `INSERT INTO ${PostgreSQLDriver.escape(
2243
- `${this.prefix}entities_${etype}`,
2244
- )} ("guid", "tags", "cdate", "mdate") VALUES (decode(@guid, 'hex'), @tags, @cdate, @mdate);`,
2245
- {
2246
- etypes: [etype],
2247
- params: {
2248
- guid,
2249
- tags,
2250
- cdate: isNaN(cdate) ? null : cdate,
2251
- mdate: isNaN(mdate) ? null : mdate,
2252
- },
2253
- },
2254
- );
2255
- const promises = [];
2138
+ let promises = [];
2256
2139
  promises.push(
2257
2140
  this.queryRun(
2258
2141
  `DELETE FROM ${PostgreSQLDriver.escape(
2259
- `${this.prefix}data_${etype}`,
2142
+ `${this.prefix}entities_${etype}`,
2260
2143
  )} WHERE "guid"=decode(@guid, 'hex');`,
2261
2144
  {
2262
2145
  etypes: [etype],
@@ -2269,7 +2152,7 @@ export default class PostgreSQLDriver extends NymphDriver {
2269
2152
  promises.push(
2270
2153
  this.queryRun(
2271
2154
  `DELETE FROM ${PostgreSQLDriver.escape(
2272
- `${this.prefix}comparisons_${etype}`,
2155
+ `${this.prefix}data_${etype}`,
2273
2156
  )} WHERE "guid"=decode(@guid, 'hex');`,
2274
2157
  {
2275
2158
  etypes: [etype],
@@ -2306,6 +2189,21 @@ export default class PostgreSQLDriver extends NymphDriver {
2306
2189
  ),
2307
2190
  );
2308
2191
  await Promise.all(promises);
2192
+ promises = [];
2193
+ await this.queryRun(
2194
+ `INSERT INTO ${PostgreSQLDriver.escape(
2195
+ `${this.prefix}entities_${etype}`,
2196
+ )} ("guid", "tags", "cdate", "mdate") VALUES (decode(@guid, 'hex'), @tags, @cdate, @mdate);`,
2197
+ {
2198
+ etypes: [etype],
2199
+ params: {
2200
+ guid,
2201
+ tags,
2202
+ cdate: isNaN(cdate) ? null : cdate,
2203
+ mdate: isNaN(mdate) ? null : mdate,
2204
+ },
2205
+ },
2206
+ );
2309
2207
  for (const name in sdata) {
2310
2208
  const value = sdata[name];
2311
2209
  const uvalue = JSON.parse(value);
@@ -2322,12 +2220,11 @@ export default class PostgreSQLDriver extends NymphDriver {
2322
2220
  storageValue === 'J'
2323
2221
  ? PostgreSQLDriver.escapeNullSequences(value)
2324
2222
  : null;
2325
- const promises = [];
2326
2223
  promises.push(
2327
2224
  this.queryRun(
2328
2225
  `INSERT INTO ${PostgreSQLDriver.escape(
2329
2226
  `${this.prefix}data_${etype}`,
2330
- )} ("guid", "name", "value", "json") VALUES (decode(@guid, 'hex'), @name, @storageValue, @jsonValue);`,
2227
+ )} ("guid", "name", "value", "json", "string", "number", "truthy") VALUES (decode(@guid, 'hex'), @name, @storageValue, @jsonValue, @string, @number, @truthy);`,
2331
2228
  {
2332
2229
  etypes: [etype],
2333
2230
  params: {
@@ -2335,26 +2232,12 @@ export default class PostgreSQLDriver extends NymphDriver {
2335
2232
  name,
2336
2233
  storageValue,
2337
2234
  jsonValue,
2338
- },
2339
- },
2340
- ),
2341
- );
2342
- promises.push(
2343
- this.queryRun(
2344
- `INSERT INTO ${PostgreSQLDriver.escape(
2345
- `${this.prefix}comparisons_${etype}`,
2346
- )} ("guid", "name", "truthy", "string", "number") VALUES (decode(@guid, 'hex'), @name, @truthy, @string, @number);`,
2347
- {
2348
- etypes: [etype],
2349
- params: {
2350
- guid,
2351
- name,
2352
- truthy: !!uvalue,
2353
2235
  string:
2354
2236
  storageValue === 'J'
2355
2237
  ? null
2356
2238
  : PostgreSQLDriver.escapeNulls(`${uvalue}`),
2357
2239
  number: isNaN(Number(uvalue)) ? null : Number(uvalue),
2240
+ truthy: !!uvalue,
2358
2241
  },
2359
2242
  },
2360
2243
  ),
@@ -2406,10 +2289,8 @@ export default class PostgreSQLDriver extends NymphDriver {
2406
2289
  );
2407
2290
  }
2408
2291
  await Promise.all(promises);
2409
- await this.commit(`nymph-import-entity-${guid}`);
2410
2292
  } catch (e: any) {
2411
2293
  this.nymph.config.debugError('postgresql', `Import entity error: "${e}"`);
2412
- await this.rollback(`nymph-import-entity-${guid}`);
2413
2294
  throw e;
2414
2295
  }
2415
2296
  }
@@ -2573,7 +2454,7 @@ export default class PostgreSQLDriver extends NymphDriver {
2573
2454
  this.queryRun(
2574
2455
  `INSERT INTO ${PostgreSQLDriver.escape(
2575
2456
  `${this.prefix}data_${etype}`,
2576
- )} ("guid", "name", "value", "json") VALUES (decode(@guid, 'hex'), @name, @storageValue, @jsonValue);`,
2457
+ )} ("guid", "name", "value", "json", "string", "number", "truthy") VALUES (decode(@guid, 'hex'), @name, @storageValue, @jsonValue, @string, @number, @truthy);`,
2577
2458
  {
2578
2459
  etypes: [etype],
2579
2460
  params: {
@@ -2581,26 +2462,12 @@ export default class PostgreSQLDriver extends NymphDriver {
2581
2462
  name,
2582
2463
  storageValue,
2583
2464
  jsonValue,
2584
- },
2585
- },
2586
- ),
2587
- );
2588
- promises.push(
2589
- this.queryRun(
2590
- `INSERT INTO ${PostgreSQLDriver.escape(
2591
- `${this.prefix}comparisons_${etype}`,
2592
- )} ("guid", "name", "truthy", "string", "number") VALUES (decode(@guid, 'hex'), @name, @truthy, @string, @number);`,
2593
- {
2594
- etypes: [etype],
2595
- params: {
2596
- guid,
2597
- name,
2598
- truthy: !!value,
2599
2465
  string:
2600
2466
  storageValue === 'J'
2601
2467
  ? null
2602
2468
  : PostgreSQLDriver.escapeNulls(`${value}`),
2603
2469
  number: isNaN(Number(value)) ? null : Number(value),
2470
+ truthy: !!value,
2604
2471
  },
2605
2472
  },
2606
2473
  ),
@@ -2717,19 +2584,6 @@ export default class PostgreSQLDriver extends NymphDriver {
2717
2584
  },
2718
2585
  ),
2719
2586
  );
2720
- promises.push(
2721
- this.queryRun(
2722
- `SELECT 1 FROM ${PostgreSQLDriver.escape(
2723
- `${this.prefix}comparisons_${etype}`,
2724
- )} WHERE "guid"=decode(@guid, 'hex') FOR UPDATE;`,
2725
- {
2726
- etypes: [etype],
2727
- params: {
2728
- guid,
2729
- },
2730
- },
2731
- ),
2732
- );
2733
2587
  promises.push(
2734
2588
  this.queryRun(
2735
2589
  `SELECT 1 FROM ${PostgreSQLDriver.escape(
@@ -2787,19 +2641,6 @@ export default class PostgreSQLDriver extends NymphDriver {
2787
2641
  },
2788
2642
  ),
2789
2643
  );
2790
- promises.push(
2791
- this.queryRun(
2792
- `DELETE FROM ${PostgreSQLDriver.escape(
2793
- `${this.prefix}comparisons_${etype}`,
2794
- )} WHERE "guid"=decode(@guid, 'hex');`,
2795
- {
2796
- etypes: [etype],
2797
- params: {
2798
- guid,
2799
- },
2800
- },
2801
- ),
2802
- );
2803
2644
  promises.push(
2804
2645
  this.queryRun(
2805
2646
  `DELETE FROM ${PostgreSQLDriver.escape(
@@ -2932,4 +2773,29 @@ export default class PostgreSQLDriver extends NymphDriver {
2932
2773
 
2933
2774
  return nymph;
2934
2775
  }
2776
+
2777
+ public async needsMigration(): Promise<boolean> {
2778
+ const table = await this.queryGet(
2779
+ 'SELECT "table_name" AS "table_name" FROM "information_schema"."tables" WHERE "table_catalog"=@db AND "table_schema"=\'public\' AND "table_name" LIKE @prefix LIMIT 1;',
2780
+ {
2781
+ params: {
2782
+ db: this.config.database,
2783
+ prefix: this.prefix + 'data_' + '%',
2784
+ },
2785
+ },
2786
+ );
2787
+ if (table?.table_name) {
2788
+ const result = await this.queryGet(
2789
+ 'SELECT 1 AS "exists" FROM "information_schema"."columns" WHERE "table_catalog"=@db AND "table_schema"=\'public\' AND "table_name"=@table AND "column_name"=\'json\';',
2790
+ {
2791
+ params: {
2792
+ db: this.config.database,
2793
+ table: table.table_name,
2794
+ },
2795
+ },
2796
+ );
2797
+ return !result?.exists;
2798
+ }
2799
+ return false;
2800
+ }
2935
2801
  }