@nymphjs/driver-postgresql 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.
@@ -767,18 +767,39 @@ export default class PostgreSQLDriver extends NymphDriver {
767
767
  return true;
768
768
  }
769
769
 
770
- protected async exportEntities(writeLine: (line: string) => void) {
771
- writeLine('#nex2');
772
- writeLine('# Nymph Entity Exchange v2');
773
- writeLine('# http://nymph.io');
774
- writeLine('#');
775
- writeLine('# Generation Time: ' + new Date().toLocaleString());
776
- writeLine('');
777
-
778
- writeLine('#');
779
- writeLine('# UIDs');
780
- writeLine('#');
781
- writeLine('');
770
+ public async *exportDataIterator(): AsyncGenerator<
771
+ { type: 'comment' | 'uid' | 'entity'; content: string },
772
+ void,
773
+ false | undefined
774
+ > {
775
+ if (
776
+ yield {
777
+ type: 'comment',
778
+ content: `#nex2
779
+ # Nymph Entity Exchange v2
780
+ # http://nymph.io
781
+ #
782
+ # Generation Time: ${new Date().toLocaleString()}
783
+ `,
784
+ }
785
+ ) {
786
+ return;
787
+ }
788
+
789
+ if (
790
+ yield {
791
+ type: 'comment',
792
+ content: `
793
+
794
+ #
795
+ # UIDs
796
+ #
797
+
798
+ `,
799
+ }
800
+ ) {
801
+ return;
802
+ }
782
803
 
783
804
  // Export UIDs.
784
805
  let uids = await this.queryIter(
@@ -787,14 +808,25 @@ export default class PostgreSQLDriver extends NymphDriver {
787
808
  )} ORDER BY "name";`,
788
809
  );
789
810
  for (const uid of uids) {
790
- writeLine(`<${uid.name}>[${uid.cur_uid}]`);
811
+ if (yield { type: 'uid', content: `<${uid.name}>[${uid.cur_uid}]\n` }) {
812
+ return;
813
+ }
791
814
  }
792
815
 
793
- writeLine('');
794
- writeLine('#');
795
- writeLine('# Entities');
796
- writeLine('#');
797
- writeLine('');
816
+ if (
817
+ yield {
818
+ type: 'comment',
819
+ content: `
820
+
821
+ #
822
+ # Entities
823
+ #
824
+
825
+ `,
826
+ }
827
+ ) {
828
+ return;
829
+ }
798
830
 
799
831
  // Get the etypes.
800
832
  const tables = await this.queryIter(
@@ -829,9 +861,10 @@ export default class PostgreSQLDriver extends NymphDriver {
829
861
  const tags = datum.value.tags.join(',');
830
862
  const cdate = datum.value.cdate;
831
863
  const mdate = datum.value.mdate;
832
- writeLine(`{${guid}}<${etype}>[${tags}]`);
833
- writeLine(`\tcdate=${JSON.stringify(cdate)}`);
834
- writeLine(`\tmdate=${JSON.stringify(mdate)}`);
864
+ let currentEntityExport: string[] = [];
865
+ currentEntityExport.push(`{${guid}}<${etype}>[${tags}]`);
866
+ currentEntityExport.push(`\tcdate=${JSON.stringify(cdate)}`);
867
+ currentEntityExport.push(`\tmdate=${JSON.stringify(mdate)}`);
835
868
  if (datum.value.dname != null) {
836
869
  // This do will keep going and adding the data until the
837
870
  // next entity is reached. $row will end on the next entity.
@@ -842,17 +875,20 @@ export default class PostgreSQLDriver extends NymphDriver {
842
875
  : datum.value.dvalue === 'S'
843
876
  ? JSON.stringify(datum.value.string)
844
877
  : datum.value.dvalue;
845
- writeLine(`\t${datum.value.dname}=${value}`);
878
+ currentEntityExport.push(`\t${datum.value.dname}=${value}`);
846
879
  datum = dataIterator.next();
847
880
  } while (!datum.done && datum.value.guid === guid);
848
881
  } else {
849
882
  // Make sure that datum is incremented :)
850
883
  datum = dataIterator.next();
851
884
  }
885
+ currentEntityExport.push('');
886
+
887
+ if (yield { type: 'entity', content: currentEntityExport.join('\n') }) {
888
+ return;
889
+ }
852
890
  }
853
891
  }
854
-
855
- return;
856
892
  }
857
893
 
858
894
  /**
@@ -1993,253 +2029,232 @@ export default class PostgreSQLDriver extends NymphDriver {
1993
2029
  return result?.cur_uid == null ? null : Number(result.cur_uid);
1994
2030
  }
1995
2031
 
1996
- public async import(filename: string, transaction?: boolean) {
2032
+ public async importEntity({
2033
+ guid,
2034
+ cdate,
2035
+ mdate,
2036
+ tags,
2037
+ sdata,
2038
+ etype,
2039
+ }: {
2040
+ guid: string;
2041
+ cdate: number;
2042
+ mdate: number;
2043
+ tags: string[];
2044
+ sdata: SerializedEntityData;
2045
+ etype: string;
2046
+ }) {
1997
2047
  try {
1998
- const result = await this.importFromFile(
1999
- filename,
2000
- async (guid, tags, sdata, etype) => {
2001
- try {
2002
- await this.internalTransaction(`nymph-import-entity-${guid}`);
2003
-
2004
- const cdate = Number(JSON.parse(sdata.cdate));
2005
- delete sdata.cdate;
2006
- const mdate = Number(JSON.parse(sdata.mdate));
2007
- delete sdata.mdate;
2008
-
2009
- await this.queryRun(
2010
- `DELETE FROM ${PostgreSQLDriver.escape(
2011
- `${this.prefix}entities_${etype}`,
2012
- )} WHERE "guid"=decode(@guid, 'hex');`,
2013
- {
2014
- etypes: [etype],
2015
- params: {
2016
- guid,
2017
- },
2048
+ await this.internalTransaction(`nymph-import-entity-${guid}`);
2049
+
2050
+ await this.queryRun(
2051
+ `DELETE FROM ${PostgreSQLDriver.escape(
2052
+ `${this.prefix}entities_${etype}`,
2053
+ )} WHERE "guid"=decode(@guid, 'hex');`,
2054
+ {
2055
+ etypes: [etype],
2056
+ params: {
2057
+ guid,
2058
+ },
2059
+ },
2060
+ );
2061
+ await this.queryRun(
2062
+ `INSERT INTO ${PostgreSQLDriver.escape(
2063
+ `${this.prefix}entities_${etype}`,
2064
+ )} ("guid", "tags", "cdate", "mdate") VALUES (decode(@guid, 'hex'), @tags, @cdate, @mdate);`,
2065
+ {
2066
+ etypes: [etype],
2067
+ params: {
2068
+ guid,
2069
+ tags,
2070
+ cdate: isNaN(cdate) ? null : cdate,
2071
+ mdate: isNaN(mdate) ? null : mdate,
2072
+ },
2073
+ },
2074
+ );
2075
+ const promises = [];
2076
+ promises.push(
2077
+ this.queryRun(
2078
+ `DELETE FROM ${PostgreSQLDriver.escape(
2079
+ `${this.prefix}data_${etype}`,
2080
+ )} WHERE "guid"=decode(@guid, 'hex');`,
2081
+ {
2082
+ etypes: [etype],
2083
+ params: {
2084
+ guid,
2085
+ },
2086
+ },
2087
+ ),
2088
+ );
2089
+ promises.push(
2090
+ this.queryRun(
2091
+ `DELETE FROM ${PostgreSQLDriver.escape(
2092
+ `${this.prefix}comparisons_${etype}`,
2093
+ )} WHERE "guid"=decode(@guid, 'hex');`,
2094
+ {
2095
+ etypes: [etype],
2096
+ params: {
2097
+ guid,
2098
+ },
2099
+ },
2100
+ ),
2101
+ );
2102
+ promises.push(
2103
+ this.queryRun(
2104
+ `DELETE FROM ${PostgreSQLDriver.escape(
2105
+ `${this.prefix}references_${etype}`,
2106
+ )} WHERE "guid"=decode(@guid, 'hex');`,
2107
+ {
2108
+ etypes: [etype],
2109
+ params: {
2110
+ guid,
2111
+ },
2112
+ },
2113
+ ),
2114
+ );
2115
+ promises.push(
2116
+ this.queryRun(
2117
+ `DELETE FROM ${PostgreSQLDriver.escape(
2118
+ `${this.prefix}uniques_${etype}`,
2119
+ )} WHERE "guid"=decode(@guid, 'hex');`,
2120
+ {
2121
+ etypes: [etype],
2122
+ params: {
2123
+ guid,
2124
+ },
2125
+ },
2126
+ ),
2127
+ );
2128
+ await Promise.all(promises);
2129
+ for (const name in sdata) {
2130
+ const value = sdata[name];
2131
+ const uvalue = JSON.parse(value);
2132
+ if (value === undefined) {
2133
+ continue;
2134
+ }
2135
+ const storageValue =
2136
+ typeof uvalue === 'number'
2137
+ ? 'N'
2138
+ : typeof uvalue === 'string'
2139
+ ? 'S'
2140
+ : value;
2141
+ const promises = [];
2142
+ promises.push(
2143
+ this.queryRun(
2144
+ `INSERT INTO ${PostgreSQLDriver.escape(
2145
+ `${this.prefix}data_${etype}`,
2146
+ )} ("guid", "name", "value") VALUES (decode(@guid, 'hex'), @name, @storageValue);`,
2147
+ {
2148
+ etypes: [etype],
2149
+ params: {
2150
+ guid,
2151
+ name,
2152
+ storageValue,
2018
2153
  },
2019
- );
2020
- await this.queryRun(
2154
+ },
2155
+ ),
2156
+ );
2157
+ promises.push(
2158
+ this.queryRun(
2159
+ `INSERT INTO ${PostgreSQLDriver.escape(
2160
+ `${this.prefix}comparisons_${etype}`,
2161
+ )} ("guid", "name", "truthy", "string", "number") VALUES (decode(@guid, 'hex'), @name, @truthy, @string, @number);`,
2162
+ {
2163
+ etypes: [etype],
2164
+ params: {
2165
+ guid,
2166
+ name,
2167
+ truthy: !!uvalue,
2168
+ string: `${uvalue}`,
2169
+ number: isNaN(Number(uvalue)) ? null : Number(uvalue),
2170
+ },
2171
+ },
2172
+ ),
2173
+ );
2174
+ const references = this.findReferences(value);
2175
+ for (const reference of references) {
2176
+ promises.push(
2177
+ this.queryRun(
2021
2178
  `INSERT INTO ${PostgreSQLDriver.escape(
2022
- `${this.prefix}entities_${etype}`,
2023
- )} ("guid", "tags", "cdate", "mdate") VALUES (decode(@guid, 'hex'), @tags, @cdate, @mdate);`,
2179
+ `${this.prefix}references_${etype}`,
2180
+ )} ("guid", "name", "reference") VALUES (decode(@guid, 'hex'), @name, decode(@reference, 'hex'));`,
2024
2181
  {
2025
2182
  etypes: [etype],
2026
2183
  params: {
2027
2184
  guid,
2028
- tags,
2029
- cdate: isNaN(cdate) ? null : cdate,
2030
- mdate: isNaN(mdate) ? null : mdate,
2031
- },
2032
- },
2033
- );
2034
- const promises = [];
2035
- promises.push(
2036
- this.queryRun(
2037
- `DELETE FROM ${PostgreSQLDriver.escape(
2038
- `${this.prefix}data_${etype}`,
2039
- )} WHERE "guid"=decode(@guid, 'hex');`,
2040
- {
2041
- etypes: [etype],
2042
- params: {
2043
- guid,
2044
- },
2045
- },
2046
- ),
2047
- );
2048
- promises.push(
2049
- this.queryRun(
2050
- `DELETE FROM ${PostgreSQLDriver.escape(
2051
- `${this.prefix}comparisons_${etype}`,
2052
- )} WHERE "guid"=decode(@guid, 'hex');`,
2053
- {
2054
- etypes: [etype],
2055
- params: {
2056
- guid,
2057
- },
2058
- },
2059
- ),
2060
- );
2061
- promises.push(
2062
- this.queryRun(
2063
- `DELETE FROM ${PostgreSQLDriver.escape(
2064
- `${this.prefix}references_${etype}`,
2065
- )} WHERE "guid"=decode(@guid, 'hex');`,
2066
- {
2067
- etypes: [etype],
2068
- params: {
2069
- guid,
2070
- },
2071
- },
2072
- ),
2073
- );
2074
- promises.push(
2075
- this.queryRun(
2076
- `DELETE FROM ${PostgreSQLDriver.escape(
2077
- `${this.prefix}uniques_${etype}`,
2078
- )} WHERE "guid"=decode(@guid, 'hex');`,
2079
- {
2080
- etypes: [etype],
2081
- params: {
2082
- guid,
2083
- },
2084
- },
2085
- ),
2086
- );
2087
- await Promise.all(promises);
2088
- for (const name in sdata) {
2089
- const value = sdata[name];
2090
- const uvalue = JSON.parse(value);
2091
- if (value === undefined) {
2092
- continue;
2093
- }
2094
- const storageValue =
2095
- typeof uvalue === 'number'
2096
- ? 'N'
2097
- : typeof uvalue === 'string'
2098
- ? 'S'
2099
- : value;
2100
- const promises = [];
2101
- promises.push(
2102
- this.queryRun(
2103
- `INSERT INTO ${PostgreSQLDriver.escape(
2104
- `${this.prefix}data_${etype}`,
2105
- )} ("guid", "name", "value") VALUES (decode(@guid, 'hex'), @name, @storageValue);`,
2106
- {
2107
- etypes: [etype],
2108
- params: {
2109
- guid,
2110
- name,
2111
- storageValue,
2112
- },
2113
- },
2114
- ),
2115
- );
2116
- promises.push(
2117
- this.queryRun(
2118
- `INSERT INTO ${PostgreSQLDriver.escape(
2119
- `${this.prefix}comparisons_${etype}`,
2120
- )} ("guid", "name", "truthy", "string", "number") VALUES (decode(@guid, 'hex'), @name, @truthy, @string, @number);`,
2121
- {
2122
- etypes: [etype],
2123
- params: {
2124
- guid,
2125
- name,
2126
- truthy: !!uvalue,
2127
- string: `${uvalue}`,
2128
- number: isNaN(Number(uvalue)) ? null : Number(uvalue),
2129
- },
2130
- },
2131
- ),
2132
- );
2133
- const references = this.findReferences(value);
2134
- for (const reference of references) {
2135
- promises.push(
2136
- this.queryRun(
2137
- `INSERT INTO ${PostgreSQLDriver.escape(
2138
- `${this.prefix}references_${etype}`,
2139
- )} ("guid", "name", "reference") VALUES (decode(@guid, 'hex'), @name, decode(@reference, 'hex'));`,
2140
- {
2141
- etypes: [etype],
2142
- params: {
2143
- guid,
2144
- name,
2145
- reference,
2146
- },
2147
- },
2148
- ),
2149
- );
2150
- }
2151
- }
2152
- const uniques = await this.nymph
2153
- .getEntityClassByEtype(etype)
2154
- .getUniques({ guid, cdate, mdate, tags, data: {}, sdata });
2155
- for (const unique of uniques) {
2156
- promises.push(
2157
- this.queryRun(
2158
- `INSERT INTO ${PostgreSQLDriver.escape(
2159
- `${this.prefix}uniques_${etype}`,
2160
- )} ("guid", "unique") VALUES (decode(@guid, 'hex'), @unique);`,
2161
- {
2162
- etypes: [etype],
2163
- params: {
2164
- guid,
2165
- unique,
2166
- },
2167
- },
2168
- ).catch((e: any) => {
2169
- if (e instanceof EntityUniqueConstraintError) {
2170
- this.nymph.config.debugError(
2171
- 'postgresql',
2172
- `Import entity unique constraint violation for GUID "${guid}" on etype "${etype}": "${unique}"`,
2173
- );
2174
- }
2175
- return e;
2176
- }),
2177
- );
2178
- }
2179
- await Promise.all(promises);
2180
- await this.commit(`nymph-import-entity-${guid}`);
2181
- } catch (e: any) {
2182
- this.nymph.config.debugError(
2183
- 'postgresql',
2184
- `Import entity error: "${e}"`,
2185
- );
2186
- await this.rollback(`nymph-import-entity-${guid}`);
2187
- throw e;
2188
- }
2189
- },
2190
- async (name, curUid) => {
2191
- try {
2192
- await this.internalTransaction(`nymph-import-uid-${name}`);
2193
- await this.queryRun(
2194
- `DELETE FROM ${PostgreSQLDriver.escape(
2195
- `${this.prefix}uids`,
2196
- )} WHERE "name"=@name;`,
2197
- {
2198
- params: {
2199
2185
  name,
2186
+ reference,
2200
2187
  },
2201
2188
  },
2202
- );
2203
- await this.queryRun(
2204
- `INSERT INTO ${PostgreSQLDriver.escape(
2205
- `${this.prefix}uids`,
2206
- )} ("name", "cur_uid") VALUES (@name, @curUid);`,
2207
- {
2208
- params: {
2209
- name,
2210
- curUid,
2211
- },
2189
+ ),
2190
+ );
2191
+ }
2192
+ }
2193
+ const uniques = await this.nymph
2194
+ .getEntityClassByEtype(etype)
2195
+ .getUniques({ guid, cdate, mdate, tags, data: {}, sdata });
2196
+ for (const unique of uniques) {
2197
+ promises.push(
2198
+ this.queryRun(
2199
+ `INSERT INTO ${PostgreSQLDriver.escape(
2200
+ `${this.prefix}uniques_${etype}`,
2201
+ )} ("guid", "unique") VALUES (decode(@guid, 'hex'), @unique);`,
2202
+ {
2203
+ etypes: [etype],
2204
+ params: {
2205
+ guid,
2206
+ unique,
2212
2207
  },
2213
- );
2214
- await this.commit(`nymph-import-uid-${name}`);
2215
- } catch (e: any) {
2216
- this.nymph.config.debugError(
2217
- 'postgresql',
2218
- `Import UID error: "${e}"`,
2219
- );
2220
- await this.rollback(`nymph-import-uid-${name}`);
2221
- throw e;
2222
- }
2223
- },
2224
- async () => {
2225
- if (transaction) {
2226
- await this.internalTransaction('nymph-import');
2227
- }
2208
+ },
2209
+ ).catch((e: any) => {
2210
+ if (e instanceof EntityUniqueConstraintError) {
2211
+ this.nymph.config.debugError(
2212
+ 'postgresql',
2213
+ `Import entity unique constraint violation for GUID "${guid}" on etype "${etype}": "${unique}"`,
2214
+ );
2215
+ }
2216
+ return e;
2217
+ }),
2218
+ );
2219
+ }
2220
+ await Promise.all(promises);
2221
+ await this.commit(`nymph-import-entity-${guid}`);
2222
+ } catch (e: any) {
2223
+ this.nymph.config.debugError('postgresql', `Import entity error: "${e}"`);
2224
+ await this.rollback(`nymph-import-entity-${guid}`);
2225
+ throw e;
2226
+ }
2227
+ }
2228
+
2229
+ public async importUID({ name, value }: { name: string; value: number }) {
2230
+ try {
2231
+ await this.internalTransaction(`nymph-import-uid-${name}`);
2232
+ await this.queryRun(
2233
+ `DELETE FROM ${PostgreSQLDriver.escape(
2234
+ `${this.prefix}uids`,
2235
+ )} WHERE "name"=@name;`,
2236
+ {
2237
+ params: {
2238
+ name,
2239
+ },
2228
2240
  },
2229
- async () => {
2230
- if (transaction) {
2231
- await this.commit('nymph-import');
2232
- }
2241
+ );
2242
+ await this.queryRun(
2243
+ `INSERT INTO ${PostgreSQLDriver.escape(
2244
+ `${this.prefix}uids`,
2245
+ )} ("name", "cur_uid") VALUES (@name, @value);`,
2246
+ {
2247
+ params: {
2248
+ name,
2249
+ value,
2250
+ },
2233
2251
  },
2234
2252
  );
2235
-
2236
- return result;
2253
+ await this.commit(`nymph-import-uid-${name}`);
2237
2254
  } catch (e: any) {
2238
- this.nymph.config.debugError('postgresql', `Import error: "${e}"`);
2239
- if (transaction) {
2240
- await this.rollback('nymph-import');
2241
- }
2242
- return false;
2255
+ this.nymph.config.debugError('postgresql', `Import UID error: "${e}"`);
2256
+ await this.rollback(`nymph-import-uid-${name}`);
2257
+ throw e;
2243
2258
  }
2244
2259
  }
2245
2260
 
@@ -2685,7 +2700,7 @@ export default class PostgreSQLDriver extends NymphDriver {
2685
2700
  return true;
2686
2701
  }
2687
2702
 
2688
- private async internalTransaction(name: string) {
2703
+ protected async internalTransaction(name: string) {
2689
2704
  if (name == null || typeof name !== 'string' || name.length === 0) {
2690
2705
  throw new InvalidParametersError(
2691
2706
  'Transaction start attempted without a name.',
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,