@nymphjs/driver-sqlite3 1.0.0-beta.61 → 1.0.0-beta.63

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,21 @@
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.63](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.62...v1.0.0-beta.63) (2024-06-18)
7
+
8
+ ### Bug Fixes
9
+
10
+ - undo ts compilation to module change ([84be6d4](https://github.com/sciactive/nymphjs/commit/84be6d434be29f8afd53907d15be2eb77d1736ce))
11
+
12
+ ### Features
13
+
14
+ - allow importing from text and iterables ([9d766bd](https://github.com/sciactive/nymphjs/commit/9d766bdad4b0f17bc2dd68b0336a0064857eb4e9))
15
+ - export data iterator ([b86aa19](https://github.com/sciactive/nymphjs/commit/b86aa19fc77d744b5a683046dfb697fc4746df5c))
16
+
17
+ # [1.0.0-beta.62](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.61...v1.0.0-beta.62) (2024-06-15)
18
+
19
+ **Note:** Version bump only for package @nymphjs/driver-sqlite3
20
+
6
21
  # [1.0.0-beta.61](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.60...v1.0.0-beta.61) (2024-06-14)
7
22
 
8
23
  **Note:** Version bump only for package @nymphjs/driver-sqlite3
@@ -1,5 +1,5 @@
1
1
  import SQLite3 from 'better-sqlite3';
2
- import { NymphDriver, type EntityConstructor, type EntityInterface, type EntityInstanceType, type FormattedSelector, type Options, type Selector } from '@nymphjs/nymph';
2
+ import { NymphDriver, type EntityConstructor, type EntityInterface, type EntityInstanceType, type SerializedEntityData, type FormattedSelector, type Options, type Selector } from '@nymphjs/nymph';
3
3
  import { SQLite3DriverConfig } from './conf';
4
4
  declare class InternalStore {
5
5
  link: SQLite3.Database;
@@ -56,7 +56,10 @@ export default class SQLite3Driver extends NymphDriver {
56
56
  commit(name: string): Promise<boolean>;
57
57
  deleteEntityByID(guid: string, className?: EntityConstructor | string | null): Promise<boolean>;
58
58
  deleteUID(name: string): Promise<boolean>;
59
- protected exportEntities(writeLine: (line: string) => void): Promise<void>;
59
+ exportDataIterator(): AsyncGenerator<{
60
+ type: 'comment' | 'uid' | 'entity';
61
+ content: string;
62
+ }, void, false | undefined>;
60
63
  /**
61
64
  * Generate the SQLite3 query.
62
65
  * @param options The options array.
@@ -79,12 +82,24 @@ export default class SQLite3Driver extends NymphDriver {
79
82
  }, ...selectors: Selector[]): Promise<string[]>;
80
83
  getEntities<T extends EntityConstructor = EntityConstructor>(options?: Options<T>, ...selectors: Selector[]): Promise<EntityInstanceType<T>[]>;
81
84
  getUID(name: string): Promise<number | null>;
82
- import(filename: string, transaction?: boolean): Promise<boolean>;
85
+ importEntity({ guid, cdate, mdate, tags, sdata, etype, }: {
86
+ guid: string;
87
+ cdate: number;
88
+ mdate: number;
89
+ tags: string[];
90
+ sdata: SerializedEntityData;
91
+ etype: string;
92
+ }): Promise<void>;
93
+ importUID({ name, value }: {
94
+ name: string;
95
+ value: number;
96
+ }): Promise<void>;
83
97
  newUID(name: string): Promise<number>;
84
98
  renameUID(oldName: string, newName: string): Promise<boolean>;
85
99
  rollback(name: string): Promise<boolean>;
86
100
  saveEntity(entity: EntityInterface): Promise<boolean>;
87
101
  setUID(name: string, curUid: number): Promise<boolean>;
102
+ internalTransaction(name: string): Promise<void>;
88
103
  startTransaction(name: string): Promise<import("@nymphjs/nymph").Nymph>;
89
104
  }
90
105
  export {};
@@ -8,9 +8,11 @@ const nymph_1 = require("@nymphjs/nymph");
8
8
  const guid_1 = require("@nymphjs/guid");
9
9
  const conf_1 = require("./conf");
10
10
  class InternalStore {
11
+ link;
12
+ linkWrite;
13
+ connected = false;
14
+ transactionsStarted = 0;
11
15
  constructor(link) {
12
- this.connected = false;
13
- this.transactionsStarted = 0;
14
16
  this.link = link;
15
17
  }
16
18
  }
@@ -18,6 +20,10 @@ class InternalStore {
18
20
  * The SQLite3 Nymph database driver.
19
21
  */
20
22
  class SQLite3Driver extends nymph_1.NymphDriver {
23
+ config;
24
+ prefix;
25
+ // @ts-ignore: this is assigned in connect(), which is called by the constructor.
26
+ store;
21
27
  static escape(input) {
22
28
  if (input.indexOf('\x00') !== -1) {
23
29
  throw new nymph_1.InvalidParametersError('SQLite3 identifiers (like entity ETYPE) cannot contain null characters.');
@@ -346,27 +352,49 @@ class SQLite3Driver extends nymph_1.NymphDriver {
346
352
  await this.commit('nymph-delete-uid');
347
353
  return true;
348
354
  }
349
- async exportEntities(writeLine) {
350
- writeLine('#nex2');
351
- writeLine('# Nymph Entity Exchange v2');
352
- writeLine('# http://nymph.io');
353
- writeLine('#');
354
- writeLine('# Generation Time: ' + new Date().toLocaleString());
355
- writeLine('');
356
- writeLine('#');
357
- writeLine('# UIDs');
358
- writeLine('#');
359
- writeLine('');
355
+ async *exportDataIterator() {
356
+ if (yield {
357
+ type: 'comment',
358
+ content: `#nex2
359
+ # Nymph Entity Exchange v2
360
+ # http://nymph.io
361
+ #
362
+ # Generation Time: ${new Date().toLocaleString()}
363
+ `,
364
+ }) {
365
+ return;
366
+ }
367
+ if (yield {
368
+ type: 'comment',
369
+ content: `
370
+
371
+ #
372
+ # UIDs
373
+ #
374
+
375
+ `,
376
+ }) {
377
+ return;
378
+ }
360
379
  // Export UIDs.
361
380
  let uids = this.queryIter(`SELECT * FROM ${SQLite3Driver.escape(`${this.prefix}uids`)} ORDER BY "name";`);
362
381
  for (const uid of uids) {
363
- writeLine(`<${uid.name}>[${uid.cur_uid}]`);
382
+ if (yield { type: 'uid', content: `<${uid.name}>[${uid.cur_uid}]\n` }) {
383
+ return;
384
+ }
385
+ }
386
+ if (yield {
387
+ type: 'comment',
388
+ content: `
389
+
390
+ #
391
+ # Entities
392
+ #
393
+
394
+ `,
395
+ }) {
396
+ return;
364
397
  }
365
- writeLine('');
366
- writeLine('#');
367
- writeLine('# Entities');
368
- writeLine('#');
369
- writeLine('');
370
398
  // Get the etypes.
371
399
  const tables = this.queryIter("SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name;");
372
400
  const etypes = [];
@@ -384,9 +412,10 @@ class SQLite3Driver extends nymph_1.NymphDriver {
384
412
  const tags = datum.value.tags.slice(1, -1);
385
413
  const cdate = datum.value.cdate;
386
414
  const mdate = datum.value.mdate;
387
- writeLine(`{${guid}}<${etype}>[${tags}]`);
388
- writeLine(`\tcdate=${JSON.stringify(cdate)}`);
389
- writeLine(`\tmdate=${JSON.stringify(mdate)}`);
415
+ let currentEntityExport = [];
416
+ currentEntityExport.push(`{${guid}}<${etype}>[${tags}]`);
417
+ currentEntityExport.push(`\tcdate=${JSON.stringify(cdate)}`);
418
+ currentEntityExport.push(`\tmdate=${JSON.stringify(mdate)}`);
390
419
  if (datum.value.dname != null) {
391
420
  // This do will keep going and adding the data until the
392
421
  // next entity is reached. datum will end on the next entity.
@@ -396,7 +425,7 @@ class SQLite3Driver extends nymph_1.NymphDriver {
396
425
  : datum.value.dvalue === 'S'
397
426
  ? JSON.stringify(datum.value.string)
398
427
  : datum.value.dvalue;
399
- writeLine(`\t${datum.value.dname}=${value}`);
428
+ currentEntityExport.push(`\t${datum.value.dname}=${value}`);
400
429
  datum = dataIterator.next();
401
430
  } while (!datum.done && datum.value.guid === guid);
402
431
  }
@@ -404,9 +433,12 @@ class SQLite3Driver extends nymph_1.NymphDriver {
404
433
  // Make sure that datum is incremented :)
405
434
  datum = dataIterator.next();
406
435
  }
436
+ currentEntityExport.push('');
437
+ if (yield { type: 'entity', content: currentEntityExport.join('\n') }) {
438
+ return;
439
+ }
407
440
  }
408
441
  }
409
- return;
410
442
  }
411
443
  /**
412
444
  * Generate the SQLite3 query.
@@ -1381,158 +1413,136 @@ class SQLite3Driver extends nymph_1.NymphDriver {
1381
1413
  });
1382
1414
  return result?.cur_uid ?? null;
1383
1415
  }
1384
- async import(filename, transaction) {
1416
+ async importEntity({ guid, cdate, mdate, tags, sdata, etype, }) {
1385
1417
  try {
1386
- return this.importFromFile(filename, async (guid, tags, sdata, etype) => {
1387
- try {
1388
- await this.startTransaction(`nymph-import-entity-${guid}`);
1389
- const cdate = Number(JSON.parse(sdata.cdate));
1390
- delete sdata.cdate;
1391
- const mdate = Number(JSON.parse(sdata.mdate));
1392
- delete sdata.mdate;
1393
- this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} WHERE "guid"=@guid;`, {
1394
- etypes: [etype],
1395
- params: {
1396
- guid,
1397
- },
1398
- });
1399
- this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} WHERE "guid"=@guid;`, {
1400
- etypes: [etype],
1401
- params: {
1402
- guid,
1403
- },
1404
- });
1405
- this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} WHERE "guid"=@guid;`, {
1406
- etypes: [etype],
1407
- params: {
1408
- guid,
1409
- },
1410
- });
1411
- this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}references_${etype}`)} WHERE "guid"=@guid;`, {
1412
- etypes: [etype],
1413
- params: {
1414
- guid,
1415
- },
1416
- });
1417
- this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}uniques_${etype}`)} WHERE "guid"=@guid;`, {
1418
- etypes: [etype],
1419
- params: {
1420
- guid,
1421
- },
1422
- });
1423
- this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} ("guid", "tags", "cdate", "mdate") VALUES (@guid, @tags, @cdate, @mdate);`, {
1418
+ await this.startTransaction(`nymph-import-entity-${guid}`);
1419
+ this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} WHERE "guid"=@guid;`, {
1420
+ etypes: [etype],
1421
+ params: {
1422
+ guid,
1423
+ },
1424
+ });
1425
+ this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} WHERE "guid"=@guid;`, {
1426
+ etypes: [etype],
1427
+ params: {
1428
+ guid,
1429
+ },
1430
+ });
1431
+ this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} WHERE "guid"=@guid;`, {
1432
+ etypes: [etype],
1433
+ params: {
1434
+ guid,
1435
+ },
1436
+ });
1437
+ this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}references_${etype}`)} WHERE "guid"=@guid;`, {
1438
+ etypes: [etype],
1439
+ params: {
1440
+ guid,
1441
+ },
1442
+ });
1443
+ this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}uniques_${etype}`)} WHERE "guid"=@guid;`, {
1444
+ etypes: [etype],
1445
+ params: {
1446
+ guid,
1447
+ },
1448
+ });
1449
+ this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} ("guid", "tags", "cdate", "mdate") VALUES (@guid, @tags, @cdate, @mdate);`, {
1450
+ etypes: [etype],
1451
+ params: {
1452
+ guid,
1453
+ tags: ',' + tags.join(',') + ',',
1454
+ cdate,
1455
+ mdate,
1456
+ },
1457
+ });
1458
+ for (const name in sdata) {
1459
+ const value = sdata[name];
1460
+ const uvalue = JSON.parse(value);
1461
+ if (value === undefined) {
1462
+ continue;
1463
+ }
1464
+ const storageValue = typeof uvalue === 'number'
1465
+ ? 'N'
1466
+ : typeof uvalue === 'string'
1467
+ ? 'S'
1468
+ : value;
1469
+ this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("guid", "name", "value") VALUES (@guid, @name, @storageValue);`, {
1470
+ etypes: [etype],
1471
+ params: {
1472
+ guid,
1473
+ name,
1474
+ storageValue,
1475
+ },
1476
+ });
1477
+ this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} ("guid", "name", "truthy", "string", "number") VALUES (@guid, @name, @truthy, @string, @number);`, {
1478
+ etypes: [etype],
1479
+ params: {
1480
+ guid,
1481
+ name,
1482
+ truthy: uvalue ? 1 : 0,
1483
+ string: `${uvalue}`,
1484
+ number: Number(uvalue),
1485
+ },
1486
+ });
1487
+ const references = this.findReferences(value);
1488
+ for (const reference of references) {
1489
+ this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}references_${etype}`)} ("guid", "name", "reference") VALUES (@guid, @name, @reference);`, {
1424
1490
  etypes: [etype],
1425
1491
  params: {
1426
1492
  guid,
1427
- tags: ',' + tags.join(',') + ',',
1428
- cdate,
1429
- mdate,
1493
+ name,
1494
+ reference,
1430
1495
  },
1431
1496
  });
1432
- for (const name in sdata) {
1433
- const value = sdata[name];
1434
- const uvalue = JSON.parse(value);
1435
- if (value === undefined) {
1436
- continue;
1437
- }
1438
- const storageValue = typeof uvalue === 'number'
1439
- ? 'N'
1440
- : typeof uvalue === 'string'
1441
- ? 'S'
1442
- : value;
1443
- this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("guid", "name", "value") VALUES (@guid, @name, @storageValue);`, {
1444
- etypes: [etype],
1445
- params: {
1446
- guid,
1447
- name,
1448
- storageValue,
1449
- },
1450
- });
1451
- this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} ("guid", "name", "truthy", "string", "number") VALUES (@guid, @name, @truthy, @string, @number);`, {
1452
- etypes: [etype],
1453
- params: {
1454
- guid,
1455
- name,
1456
- truthy: uvalue ? 1 : 0,
1457
- string: `${uvalue}`,
1458
- number: Number(uvalue),
1459
- },
1460
- });
1461
- const references = this.findReferences(value);
1462
- for (const reference of references) {
1463
- this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}references_${etype}`)} ("guid", "name", "reference") VALUES (@guid, @name, @reference);`, {
1464
- etypes: [etype],
1465
- params: {
1466
- guid,
1467
- name,
1468
- reference,
1469
- },
1470
- });
1471
- }
1472
- }
1473
- const uniques = await this.nymph
1474
- .getEntityClassByEtype(etype)
1475
- .getUniques({ guid, cdate, mdate, tags, data: {}, sdata });
1476
- for (const unique of uniques) {
1477
- try {
1478
- this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}uniques_${etype}`)} ("guid", "unique") VALUES (@guid, @unique);`, {
1479
- etypes: [etype],
1480
- params: {
1481
- guid,
1482
- unique,
1483
- },
1484
- });
1485
- }
1486
- catch (e) {
1487
- if (e instanceof nymph_1.EntityUniqueConstraintError) {
1488
- this.nymph.config.debugError('sqlite3', `Import entity unique constraint violation for GUID "${guid}" on etype "${etype}": "${unique}"`);
1489
- }
1490
- throw e;
1491
- }
1492
- }
1493
- await this.commit(`nymph-import-entity-${guid}`);
1494
1497
  }
1495
- catch (e) {
1496
- this.nymph.config.debugError('sqlite3', `Import entity error: "${e}"`);
1497
- await this.rollback(`nymph-import-entity-${guid}`);
1498
- throw e;
1499
- }
1500
- }, async (name, curUid) => {
1498
+ }
1499
+ const uniques = await this.nymph
1500
+ .getEntityClassByEtype(etype)
1501
+ .getUniques({ guid, cdate, mdate, tags, data: {}, sdata });
1502
+ for (const unique of uniques) {
1501
1503
  try {
1502
- await this.startTransaction(`nymph-import-uid-${name}`);
1503
- this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}uids`)} WHERE "name"=@name;`, {
1504
- params: {
1505
- name,
1506
- },
1507
- });
1508
- this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}uids`)} ("name", "cur_uid") VALUES (@name, @curUid);`, {
1504
+ this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}uniques_${etype}`)} ("guid", "unique") VALUES (@guid, @unique);`, {
1505
+ etypes: [etype],
1509
1506
  params: {
1510
- name,
1511
- curUid,
1507
+ guid,
1508
+ unique,
1512
1509
  },
1513
1510
  });
1514
- await this.commit(`nymph-import-uid-${name}`);
1515
1511
  }
1516
1512
  catch (e) {
1517
- this.nymph.config.debugError('sqlite3', `Import UID error: "${e}"`);
1518
- await this.rollback(`nymph-import-uid-${name}`);
1513
+ if (e instanceof nymph_1.EntityUniqueConstraintError) {
1514
+ this.nymph.config.debugError('sqlite3', `Import entity unique constraint violation for GUID "${guid}" on etype "${etype}": "${unique}"`);
1515
+ }
1519
1516
  throw e;
1520
1517
  }
1521
- }, async () => {
1522
- if (transaction) {
1523
- await this.startTransaction('nymph-import');
1524
- }
1525
- }, async () => {
1526
- if (transaction) {
1527
- await this.commit('nymph-import');
1528
- }
1518
+ }
1519
+ await this.commit(`nymph-import-entity-${guid}`);
1520
+ }
1521
+ catch (e) {
1522
+ this.nymph.config.debugError('sqlite3', `Import entity error: "${e}"`);
1523
+ await this.rollback(`nymph-import-entity-${guid}`);
1524
+ throw e;
1525
+ }
1526
+ }
1527
+ async importUID({ name, value }) {
1528
+ try {
1529
+ await this.startTransaction(`nymph-import-uid-${name}`);
1530
+ this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}uids`)} WHERE "name"=@name;`, {
1531
+ params: {
1532
+ name,
1533
+ },
1534
+ });
1535
+ this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}uids`)} ("name", "cur_uid") VALUES (@name, @value);`, {
1536
+ params: {
1537
+ name,
1538
+ value,
1539
+ },
1529
1540
  });
1541
+ await this.commit(`nymph-import-uid-${name}`);
1530
1542
  }
1531
1543
  catch (e) {
1532
- this.nymph.config.debugError('sqlite3', `Import error: "${e}"`);
1533
- if (transaction) {
1534
- await this.rollback('nymph-import');
1535
- }
1544
+ this.nymph.config.debugError('sqlite3', `Import UID error: "${e}"`);
1545
+ await this.rollback(`nymph-import-uid-${name}`);
1536
1546
  throw e;
1537
1547
  }
1538
1548
  }
@@ -1777,6 +1787,9 @@ class SQLite3Driver extends nymph_1.NymphDriver {
1777
1787
  await this.commit('nymph-set-uid');
1778
1788
  return true;
1779
1789
  }
1790
+ async internalTransaction(name) {
1791
+ await this.startTransaction(name);
1792
+ }
1780
1793
  async startTransaction(name) {
1781
1794
  if (name == null || typeof name !== 'string' || name.length === 0) {
1782
1795
  throw new nymph_1.InvalidParametersError('Transaction start attempted without a name.');