@nymphjs/driver-sqlite3 1.0.0-beta.62 → 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.
@@ -602,18 +602,39 @@ export default class SQLite3Driver extends NymphDriver {
602
602
  return true;
603
603
  }
604
604
 
605
- protected async exportEntities(writeLine: (line: string) => void) {
606
- writeLine('#nex2');
607
- writeLine('# Nymph Entity Exchange v2');
608
- writeLine('# http://nymph.io');
609
- writeLine('#');
610
- writeLine('# Generation Time: ' + new Date().toLocaleString());
611
- writeLine('');
605
+ public async *exportDataIterator(): AsyncGenerator<
606
+ { type: 'comment' | 'uid' | 'entity'; content: string },
607
+ void,
608
+ false | undefined
609
+ > {
610
+ if (
611
+ yield {
612
+ type: 'comment',
613
+ content: `#nex2
614
+ # Nymph Entity Exchange v2
615
+ # http://nymph.io
616
+ #
617
+ # Generation Time: ${new Date().toLocaleString()}
618
+ `,
619
+ }
620
+ ) {
621
+ return;
622
+ }
612
623
 
613
- writeLine('#');
614
- writeLine('# UIDs');
615
- writeLine('#');
616
- writeLine('');
624
+ if (
625
+ yield {
626
+ type: 'comment',
627
+ content: `
628
+
629
+ #
630
+ # UIDs
631
+ #
632
+
633
+ `,
634
+ }
635
+ ) {
636
+ return;
637
+ }
617
638
 
618
639
  // Export UIDs.
619
640
  let uids: IterableIterator<any> = this.queryIter(
@@ -622,14 +643,25 @@ export default class SQLite3Driver extends NymphDriver {
622
643
  )} ORDER BY "name";`,
623
644
  );
624
645
  for (const uid of uids) {
625
- writeLine(`<${uid.name}>[${uid.cur_uid}]`);
646
+ if (yield { type: 'uid', content: `<${uid.name}>[${uid.cur_uid}]\n` }) {
647
+ return;
648
+ }
626
649
  }
627
650
 
628
- writeLine('');
629
- writeLine('#');
630
- writeLine('# Entities');
631
- writeLine('#');
632
- writeLine('');
651
+ if (
652
+ yield {
653
+ type: 'comment',
654
+ content: `
655
+
656
+ #
657
+ # Entities
658
+ #
659
+
660
+ `,
661
+ }
662
+ ) {
663
+ return;
664
+ }
633
665
 
634
666
  // Get the etypes.
635
667
  const tables: IterableIterator<any> = this.queryIter(
@@ -659,9 +691,10 @@ export default class SQLite3Driver extends NymphDriver {
659
691
  const tags = datum.value.tags.slice(1, -1);
660
692
  const cdate = datum.value.cdate;
661
693
  const mdate = datum.value.mdate;
662
- writeLine(`{${guid}}<${etype}>[${tags}]`);
663
- writeLine(`\tcdate=${JSON.stringify(cdate)}`);
664
- writeLine(`\tmdate=${JSON.stringify(mdate)}`);
694
+ let currentEntityExport: string[] = [];
695
+ currentEntityExport.push(`{${guid}}<${etype}>[${tags}]`);
696
+ currentEntityExport.push(`\tcdate=${JSON.stringify(cdate)}`);
697
+ currentEntityExport.push(`\tmdate=${JSON.stringify(mdate)}`);
665
698
  if (datum.value.dname != null) {
666
699
  // This do will keep going and adding the data until the
667
700
  // next entity is reached. datum will end on the next entity.
@@ -672,17 +705,20 @@ export default class SQLite3Driver extends NymphDriver {
672
705
  : datum.value.dvalue === 'S'
673
706
  ? JSON.stringify(datum.value.string)
674
707
  : datum.value.dvalue;
675
- writeLine(`\t${datum.value.dname}=${value}`);
708
+ currentEntityExport.push(`\t${datum.value.dname}=${value}`);
676
709
  datum = dataIterator.next();
677
710
  } while (!datum.done && datum.value.guid === guid);
678
711
  } else {
679
712
  // Make sure that datum is incremented :)
680
713
  datum = dataIterator.next();
681
714
  }
715
+ currentEntityExport.push('');
716
+
717
+ if (yield { type: 'entity', content: currentEntityExport.join('\n') }) {
718
+ return;
719
+ }
682
720
  }
683
721
  }
684
-
685
- return;
686
722
  }
687
723
 
688
724
  /**
@@ -1740,230 +1776,214 @@ export default class SQLite3Driver extends NymphDriver {
1740
1776
  return (result?.cur_uid as number | null) ?? null;
1741
1777
  }
1742
1778
 
1743
- public async import(filename: string, transaction?: boolean) {
1779
+ public async importEntity({
1780
+ guid,
1781
+ cdate,
1782
+ mdate,
1783
+ tags,
1784
+ sdata,
1785
+ etype,
1786
+ }: {
1787
+ guid: string;
1788
+ cdate: number;
1789
+ mdate: number;
1790
+ tags: string[];
1791
+ sdata: SerializedEntityData;
1792
+ etype: string;
1793
+ }) {
1744
1794
  try {
1745
- return this.importFromFile(
1746
- filename,
1747
- async (guid, tags, sdata, etype) => {
1748
- try {
1749
- await this.startTransaction(`nymph-import-entity-${guid}`);
1795
+ await this.startTransaction(`nymph-import-entity-${guid}`);
1750
1796
 
1751
- const cdate = Number(JSON.parse(sdata.cdate));
1752
- delete sdata.cdate;
1753
- const mdate = Number(JSON.parse(sdata.mdate));
1754
- delete sdata.mdate;
1797
+ this.queryRun(
1798
+ `DELETE FROM ${SQLite3Driver.escape(
1799
+ `${this.prefix}entities_${etype}`,
1800
+ )} WHERE "guid"=@guid;`,
1801
+ {
1802
+ etypes: [etype],
1803
+ params: {
1804
+ guid,
1805
+ },
1806
+ },
1807
+ );
1808
+ this.queryRun(
1809
+ `DELETE FROM ${SQLite3Driver.escape(
1810
+ `${this.prefix}data_${etype}`,
1811
+ )} WHERE "guid"=@guid;`,
1812
+ {
1813
+ etypes: [etype],
1814
+ params: {
1815
+ guid,
1816
+ },
1817
+ },
1818
+ );
1819
+ this.queryRun(
1820
+ `DELETE FROM ${SQLite3Driver.escape(
1821
+ `${this.prefix}comparisons_${etype}`,
1822
+ )} WHERE "guid"=@guid;`,
1823
+ {
1824
+ etypes: [etype],
1825
+ params: {
1826
+ guid,
1827
+ },
1828
+ },
1829
+ );
1830
+ this.queryRun(
1831
+ `DELETE FROM ${SQLite3Driver.escape(
1832
+ `${this.prefix}references_${etype}`,
1833
+ )} WHERE "guid"=@guid;`,
1834
+ {
1835
+ etypes: [etype],
1836
+ params: {
1837
+ guid,
1838
+ },
1839
+ },
1840
+ );
1841
+ this.queryRun(
1842
+ `DELETE FROM ${SQLite3Driver.escape(
1843
+ `${this.prefix}uniques_${etype}`,
1844
+ )} WHERE "guid"=@guid;`,
1845
+ {
1846
+ etypes: [etype],
1847
+ params: {
1848
+ guid,
1849
+ },
1850
+ },
1851
+ );
1755
1852
 
1756
- this.queryRun(
1757
- `DELETE FROM ${SQLite3Driver.escape(
1758
- `${this.prefix}entities_${etype}`,
1759
- )} WHERE "guid"=@guid;`,
1760
- {
1761
- etypes: [etype],
1762
- params: {
1763
- guid,
1764
- },
1765
- },
1766
- );
1767
- this.queryRun(
1768
- `DELETE FROM ${SQLite3Driver.escape(
1769
- `${this.prefix}data_${etype}`,
1770
- )} WHERE "guid"=@guid;`,
1771
- {
1772
- etypes: [etype],
1773
- params: {
1774
- guid,
1775
- },
1776
- },
1777
- );
1778
- this.queryRun(
1779
- `DELETE FROM ${SQLite3Driver.escape(
1780
- `${this.prefix}comparisons_${etype}`,
1781
- )} WHERE "guid"=@guid;`,
1782
- {
1783
- etypes: [etype],
1784
- params: {
1785
- guid,
1786
- },
1787
- },
1788
- );
1789
- this.queryRun(
1790
- `DELETE FROM ${SQLite3Driver.escape(
1791
- `${this.prefix}references_${etype}`,
1792
- )} WHERE "guid"=@guid;`,
1793
- {
1794
- etypes: [etype],
1795
- params: {
1796
- guid,
1797
- },
1798
- },
1799
- );
1800
- this.queryRun(
1801
- `DELETE FROM ${SQLite3Driver.escape(
1802
- `${this.prefix}uniques_${etype}`,
1803
- )} WHERE "guid"=@guid;`,
1804
- {
1805
- etypes: [etype],
1806
- params: {
1807
- guid,
1808
- },
1853
+ this.queryRun(
1854
+ `INSERT INTO ${SQLite3Driver.escape(
1855
+ `${this.prefix}entities_${etype}`,
1856
+ )} ("guid", "tags", "cdate", "mdate") VALUES (@guid, @tags, @cdate, @mdate);`,
1857
+ {
1858
+ etypes: [etype],
1859
+ params: {
1860
+ guid,
1861
+ tags: ',' + tags.join(',') + ',',
1862
+ cdate,
1863
+ mdate,
1864
+ },
1865
+ },
1866
+ );
1867
+ for (const name in sdata) {
1868
+ const value = sdata[name];
1869
+ const uvalue = JSON.parse(value);
1870
+ if (value === undefined) {
1871
+ continue;
1872
+ }
1873
+ const storageValue =
1874
+ typeof uvalue === 'number'
1875
+ ? 'N'
1876
+ : typeof uvalue === 'string'
1877
+ ? 'S'
1878
+ : value;
1879
+ this.queryRun(
1880
+ `INSERT INTO ${SQLite3Driver.escape(
1881
+ `${this.prefix}data_${etype}`,
1882
+ )} ("guid", "name", "value") VALUES (@guid, @name, @storageValue);`,
1883
+ {
1884
+ etypes: [etype],
1885
+ params: {
1886
+ guid,
1887
+ name,
1888
+ storageValue,
1889
+ },
1890
+ },
1891
+ );
1892
+ this.queryRun(
1893
+ `INSERT INTO ${SQLite3Driver.escape(
1894
+ `${this.prefix}comparisons_${etype}`,
1895
+ )} ("guid", "name", "truthy", "string", "number") VALUES (@guid, @name, @truthy, @string, @number);`,
1896
+ {
1897
+ etypes: [etype],
1898
+ params: {
1899
+ guid,
1900
+ name,
1901
+ truthy: uvalue ? 1 : 0,
1902
+ string: `${uvalue}`,
1903
+ number: Number(uvalue),
1904
+ },
1905
+ },
1906
+ );
1907
+ const references = this.findReferences(value);
1908
+ for (const reference of references) {
1909
+ this.queryRun(
1910
+ `INSERT INTO ${SQLite3Driver.escape(
1911
+ `${this.prefix}references_${etype}`,
1912
+ )} ("guid", "name", "reference") VALUES (@guid, @name, @reference);`,
1913
+ {
1914
+ etypes: [etype],
1915
+ params: {
1916
+ guid,
1917
+ name,
1918
+ reference,
1809
1919
  },
1810
- );
1811
-
1812
- this.queryRun(
1813
- `INSERT INTO ${SQLite3Driver.escape(
1814
- `${this.prefix}entities_${etype}`,
1815
- )} ("guid", "tags", "cdate", "mdate") VALUES (@guid, @tags, @cdate, @mdate);`,
1816
- {
1817
- etypes: [etype],
1818
- params: {
1819
- guid,
1820
- tags: ',' + tags.join(',') + ',',
1821
- cdate,
1822
- mdate,
1823
- },
1920
+ },
1921
+ );
1922
+ }
1923
+ }
1924
+ const uniques = await this.nymph
1925
+ .getEntityClassByEtype(etype)
1926
+ .getUniques({ guid, cdate, mdate, tags, data: {}, sdata });
1927
+ for (const unique of uniques) {
1928
+ try {
1929
+ this.queryRun(
1930
+ `INSERT INTO ${SQLite3Driver.escape(
1931
+ `${this.prefix}uniques_${etype}`,
1932
+ )} ("guid", "unique") VALUES (@guid, @unique);`,
1933
+ {
1934
+ etypes: [etype],
1935
+ params: {
1936
+ guid,
1937
+ unique,
1824
1938
  },
1825
- );
1826
- for (const name in sdata) {
1827
- const value = sdata[name];
1828
- const uvalue = JSON.parse(value);
1829
- if (value === undefined) {
1830
- continue;
1831
- }
1832
- const storageValue =
1833
- typeof uvalue === 'number'
1834
- ? 'N'
1835
- : typeof uvalue === 'string'
1836
- ? 'S'
1837
- : value;
1838
- this.queryRun(
1839
- `INSERT INTO ${SQLite3Driver.escape(
1840
- `${this.prefix}data_${etype}`,
1841
- )} ("guid", "name", "value") VALUES (@guid, @name, @storageValue);`,
1842
- {
1843
- etypes: [etype],
1844
- params: {
1845
- guid,
1846
- name,
1847
- storageValue,
1848
- },
1849
- },
1850
- );
1851
- this.queryRun(
1852
- `INSERT INTO ${SQLite3Driver.escape(
1853
- `${this.prefix}comparisons_${etype}`,
1854
- )} ("guid", "name", "truthy", "string", "number") VALUES (@guid, @name, @truthy, @string, @number);`,
1855
- {
1856
- etypes: [etype],
1857
- params: {
1858
- guid,
1859
- name,
1860
- truthy: uvalue ? 1 : 0,
1861
- string: `${uvalue}`,
1862
- number: Number(uvalue),
1863
- },
1864
- },
1865
- );
1866
- const references = this.findReferences(value);
1867
- for (const reference of references) {
1868
- this.queryRun(
1869
- `INSERT INTO ${SQLite3Driver.escape(
1870
- `${this.prefix}references_${etype}`,
1871
- )} ("guid", "name", "reference") VALUES (@guid, @name, @reference);`,
1872
- {
1873
- etypes: [etype],
1874
- params: {
1875
- guid,
1876
- name,
1877
- reference,
1878
- },
1879
- },
1880
- );
1881
- }
1882
- }
1883
- const uniques = await this.nymph
1884
- .getEntityClassByEtype(etype)
1885
- .getUniques({ guid, cdate, mdate, tags, data: {}, sdata });
1886
- for (const unique of uniques) {
1887
- try {
1888
- this.queryRun(
1889
- `INSERT INTO ${SQLite3Driver.escape(
1890
- `${this.prefix}uniques_${etype}`,
1891
- )} ("guid", "unique") VALUES (@guid, @unique);`,
1892
- {
1893
- etypes: [etype],
1894
- params: {
1895
- guid,
1896
- unique,
1897
- },
1898
- },
1899
- );
1900
- } catch (e: any) {
1901
- if (e instanceof EntityUniqueConstraintError) {
1902
- this.nymph.config.debugError(
1903
- 'sqlite3',
1904
- `Import entity unique constraint violation for GUID "${guid}" on etype "${etype}": "${unique}"`,
1905
- );
1906
- }
1907
- throw e;
1908
- }
1909
- }
1910
- await this.commit(`nymph-import-entity-${guid}`);
1911
- } catch (e: any) {
1939
+ },
1940
+ );
1941
+ } catch (e: any) {
1942
+ if (e instanceof EntityUniqueConstraintError) {
1912
1943
  this.nymph.config.debugError(
1913
1944
  'sqlite3',
1914
- `Import entity error: "${e}"`,
1915
- );
1916
- await this.rollback(`nymph-import-entity-${guid}`);
1917
- throw e;
1918
- }
1919
- },
1920
- async (name, curUid) => {
1921
- try {
1922
- await this.startTransaction(`nymph-import-uid-${name}`);
1923
- this.queryRun(
1924
- `DELETE FROM ${SQLite3Driver.escape(
1925
- `${this.prefix}uids`,
1926
- )} WHERE "name"=@name;`,
1927
- {
1928
- params: {
1929
- name,
1930
- },
1931
- },
1932
- );
1933
- this.queryRun(
1934
- `INSERT INTO ${SQLite3Driver.escape(
1935
- `${this.prefix}uids`,
1936
- )} ("name", "cur_uid") VALUES (@name, @curUid);`,
1937
- {
1938
- params: {
1939
- name,
1940
- curUid,
1941
- },
1942
- },
1945
+ `Import entity unique constraint violation for GUID "${guid}" on etype "${etype}": "${unique}"`,
1943
1946
  );
1944
- await this.commit(`nymph-import-uid-${name}`);
1945
- } catch (e: any) {
1946
- this.nymph.config.debugError('sqlite3', `Import UID error: "${e}"`);
1947
- await this.rollback(`nymph-import-uid-${name}`);
1948
- throw e;
1949
- }
1950
- },
1951
- async () => {
1952
- if (transaction) {
1953
- await this.startTransaction('nymph-import');
1954
1947
  }
1948
+ throw e;
1949
+ }
1950
+ }
1951
+ await this.commit(`nymph-import-entity-${guid}`);
1952
+ } catch (e: any) {
1953
+ this.nymph.config.debugError('sqlite3', `Import entity error: "${e}"`);
1954
+ await this.rollback(`nymph-import-entity-${guid}`);
1955
+ throw e;
1956
+ }
1957
+ }
1958
+
1959
+ public async importUID({ name, value }: { name: string; value: number }) {
1960
+ try {
1961
+ await this.startTransaction(`nymph-import-uid-${name}`);
1962
+ this.queryRun(
1963
+ `DELETE FROM ${SQLite3Driver.escape(
1964
+ `${this.prefix}uids`,
1965
+ )} WHERE "name"=@name;`,
1966
+ {
1967
+ params: {
1968
+ name,
1969
+ },
1955
1970
  },
1956
- async () => {
1957
- if (transaction) {
1958
- await this.commit('nymph-import');
1959
- }
1971
+ );
1972
+ this.queryRun(
1973
+ `INSERT INTO ${SQLite3Driver.escape(
1974
+ `${this.prefix}uids`,
1975
+ )} ("name", "cur_uid") VALUES (@name, @value);`,
1976
+ {
1977
+ params: {
1978
+ name,
1979
+ value,
1980
+ },
1960
1981
  },
1961
1982
  );
1983
+ await this.commit(`nymph-import-uid-${name}`);
1962
1984
  } catch (e: any) {
1963
- this.nymph.config.debugError('sqlite3', `Import error: "${e}"`);
1964
- if (transaction) {
1965
- await this.rollback('nymph-import');
1966
- }
1985
+ this.nymph.config.debugError('sqlite3', `Import UID error: "${e}"`);
1986
+ await this.rollback(`nymph-import-uid-${name}`);
1967
1987
  throw e;
1968
1988
  }
1969
1989
  }
@@ -2318,6 +2338,10 @@ export default class SQLite3Driver extends NymphDriver {
2318
2338
  return true;
2319
2339
  }
2320
2340
 
2341
+ public async internalTransaction(name: string) {
2342
+ await this.startTransaction(name);
2343
+ }
2344
+
2321
2345
  public async startTransaction(name: string) {
2322
2346
  if (name == null || typeof name !== 'string' || name.length === 0) {
2323
2347
  throw new InvalidParametersError(
package/tsconfig.json CHANGED
@@ -2,8 +2,8 @@
2
2
  "extends": "@tsconfig/recommended/tsconfig.json",
3
3
 
4
4
  "compilerOptions": {
5
- "lib": ["ES2021"],
6
- "target": "ES2021",
5
+ "lib": ["ES2023"],
6
+ "target": "ES2022",
7
7
  "noImplicitAny": true,
8
8
  "removeComments": false,
9
9
  "sourceMap": true,