@nymphjs/driver-postgresql 1.0.0-beta.81 → 1.0.0-beta.83

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.
@@ -1,5 +1,7 @@
1
- import { Pool, PoolClient, PoolConfig, QueryResult } from 'pg';
1
+ import pg from 'pg';
2
+ import type { Pool, PoolClient, PoolConfig, QueryResult } from 'pg';
2
3
  import format from 'pg-format';
4
+ import Cursor from 'pg-cursor';
3
5
  import {
4
6
  NymphDriver,
5
7
  type EntityConstructor,
@@ -22,7 +24,7 @@ import { makeTableSuffix } from '@nymphjs/guid';
22
24
  import {
23
25
  PostgreSQLDriverConfig,
24
26
  PostgreSQLDriverConfigDefaults as defaults,
25
- } from './conf';
27
+ } from './conf/index.js';
26
28
 
27
29
  type PostgreSQLDriverConnection = {
28
30
  client: PoolClient;
@@ -145,8 +147,8 @@ export default class PostgreSQLDriver extends NymphDriver {
145
147
  err
146
148
  ? reject(err)
147
149
  : client
148
- ? resolve({ client, done })
149
- : reject('No client returned from connect.'),
150
+ ? resolve({ client, done })
151
+ : reject('No client returned from connect.'),
150
152
  ),
151
153
  );
152
154
  }
@@ -166,8 +168,8 @@ export default class PostgreSQLDriver extends NymphDriver {
166
168
  err
167
169
  ? reject(err)
168
170
  : client
169
- ? resolve({ client, done })
170
- : reject('No client returned from connect.'),
171
+ ? resolve({ client, done })
172
+ : reject('No client returned from connect.'),
171
173
  ),
172
174
  );
173
175
  await new Promise((resolve, reject) =>
@@ -187,7 +189,7 @@ export default class PostgreSQLDriver extends NymphDriver {
187
189
  // Connecting, selecting database
188
190
  if (!this.connected) {
189
191
  try {
190
- this.link = new Pool(this.postgresqlConfig);
192
+ this.link = new pg.Pool(this.postgresqlConfig);
191
193
  this.connected = true;
192
194
  } catch (e: any) {
193
195
  if (
@@ -645,7 +647,7 @@ export default class PostgreSQLDriver extends NymphDriver {
645
647
  }
646
648
  }
647
649
 
648
- private queryIter(
650
+ private queryArray(
649
651
  query: string,
650
652
  {
651
653
  etypes = [],
@@ -682,6 +684,54 @@ export default class PostgreSQLDriver extends NymphDriver {
682
684
  );
683
685
  }
684
686
 
687
+ private async queryIter(
688
+ query: string,
689
+ {
690
+ etypes = [],
691
+ params = {},
692
+ }: {
693
+ etypes?: string[];
694
+ params?: { [k: string]: any };
695
+ } = {},
696
+ ) {
697
+ const { query: newQuery, params: newParams } = this.translateQuery(
698
+ query,
699
+ params,
700
+ );
701
+ const that = this;
702
+
703
+ return this.query(
704
+ async function* (): AsyncGenerator<any, void, false | undefined> {
705
+ const transaction = !!that.transaction?.connection;
706
+ const connection = await that.getConnection();
707
+ const cursor = new Cursor(newQuery, newParams);
708
+ const iter = connection.client.query(cursor);
709
+
710
+ while (true) {
711
+ const rows = await iter.read(100);
712
+
713
+ if (!rows.length) {
714
+ await new Promise<void>((resolve) => {
715
+ iter.close(() => {
716
+ if (!transaction) {
717
+ connection.done();
718
+ }
719
+ resolve();
720
+ });
721
+ });
722
+ return;
723
+ }
724
+
725
+ for (let row of rows) {
726
+ yield row;
727
+ }
728
+ }
729
+ },
730
+ `${query} -- ${JSON.stringify(params)}`,
731
+ etypes,
732
+ );
733
+ }
734
+
685
735
  private queryGet(
686
736
  query: string,
687
737
  {
@@ -911,7 +961,7 @@ export default class PostgreSQLDriver extends NymphDriver {
911
961
  `${this.prefix}uids`,
912
962
  )} ORDER BY "name";`,
913
963
  );
914
- for (const uid of uids) {
964
+ for await (const uid of uids) {
915
965
  if (yield { type: 'uid', content: `<${uid.name}>[${uid.cur_uid}]\n` }) {
916
966
  return;
917
967
  }
@@ -933,7 +983,7 @@ export default class PostgreSQLDriver extends NymphDriver {
933
983
  }
934
984
 
935
985
  // Get the etypes.
936
- const tables = await this.queryIter(
986
+ const tables = await this.queryArray(
937
987
  'SELECT "table_name" AS "table_name" FROM "information_schema"."tables" WHERE "table_catalog"=@db AND "table_schema"=\'public\' AND "table_name" LIKE @prefix;',
938
988
  {
939
989
  params: {
@@ -949,17 +999,15 @@ export default class PostgreSQLDriver extends NymphDriver {
949
999
 
950
1000
  for (const etype of etypes) {
951
1001
  // Export entities.
952
- const dataIterator = (
953
- await this.queryIter(
954
- `SELECT encode(e."guid", 'hex') AS "guid", e."tags", e."cdate", e."mdate", d."name", d."value", d."json", d."string", d."number"
1002
+ const dataIterator = await this.queryIter(
1003
+ `SELECT encode(e."guid", 'hex') AS "guid", e."tags", e."cdate", e."mdate", d."name", d."value", d."json", d."string", d."number"
955
1004
  FROM ${PostgreSQLDriver.escape(`${this.prefix}entities_${etype}`)} e
956
1005
  LEFT JOIN ${PostgreSQLDriver.escape(
957
1006
  `${this.prefix}data_${etype}`,
958
1007
  )} d ON e."guid"=d."guid"
959
1008
  ORDER BY e."guid";`,
960
- )
961
- )[Symbol.iterator]();
962
- let datum = dataIterator.next();
1009
+ );
1010
+ let datum = await dataIterator.next();
963
1011
  while (!datum.done) {
964
1012
  const guid = datum.value.guid;
965
1013
  const tags = datum.value.tags.filter((tag: string) => tag).join(',');
@@ -977,20 +1025,20 @@ export default class PostgreSQLDriver extends NymphDriver {
977
1025
  datum.value.value === 'N'
978
1026
  ? JSON.stringify(Number(datum.value.number))
979
1027
  : datum.value.value === 'S'
980
- ? JSON.stringify(
981
- PostgreSQLDriver.unescapeNulls(datum.value.string),
982
- )
983
- : datum.value.value === 'J'
984
- ? PostgreSQLDriver.unescapeNullSequences(
985
- JSON.stringify(datum.value.json),
986
- )
987
- : datum.value.value;
1028
+ ? JSON.stringify(
1029
+ PostgreSQLDriver.unescapeNulls(datum.value.string),
1030
+ )
1031
+ : datum.value.value === 'J'
1032
+ ? PostgreSQLDriver.unescapeNullSequences(
1033
+ JSON.stringify(datum.value.json),
1034
+ )
1035
+ : datum.value.value;
988
1036
  currentEntityExport.push(`\t${datum.value.name}=${value}`);
989
- datum = dataIterator.next();
1037
+ datum = await dataIterator.next();
990
1038
  } while (!datum.done && datum.value.guid === guid);
991
1039
  } else {
992
1040
  // Make sure that datum is incremented :)
993
- datum = dataIterator.next();
1041
+ datum = await dataIterator.next();
994
1042
  }
995
1043
  currentEntityExport.push('');
996
1044
 
@@ -2039,7 +2087,7 @@ export default class PostgreSQLDriver extends NymphDriver {
2039
2087
  formattedSelectors,
2040
2088
  etype,
2041
2089
  );
2042
- const result = this.queryIter(query, { etypes, params }).then((val) =>
2090
+ const result = this.queryArray(query, { etypes, params }).then((val) =>
2043
2091
  val[Symbol.iterator](),
2044
2092
  );
2045
2093
  return {
@@ -2087,10 +2135,12 @@ export default class PostgreSQLDriver extends NymphDriver {
2087
2135
  row.value === 'N'
2088
2136
  ? JSON.stringify(Number(row.number))
2089
2137
  : row.value === 'S'
2090
- ? JSON.stringify(PostgreSQLDriver.unescapeNulls(row.string))
2091
- : row.value === 'J'
2092
- ? PostgreSQLDriver.unescapeNullSequences(JSON.stringify(row.json))
2093
- : row.value,
2138
+ ? JSON.stringify(PostgreSQLDriver.unescapeNulls(row.string))
2139
+ : row.value === 'J'
2140
+ ? PostgreSQLDriver.unescapeNullSequences(
2141
+ JSON.stringify(row.json),
2142
+ )
2143
+ : row.value,
2094
2144
  }),
2095
2145
  );
2096
2146
 
@@ -2214,8 +2264,8 @@ export default class PostgreSQLDriver extends NymphDriver {
2214
2264
  typeof uvalue === 'number'
2215
2265
  ? 'N'
2216
2266
  : typeof uvalue === 'string'
2217
- ? 'S'
2218
- : 'J';
2267
+ ? 'S'
2268
+ : 'J';
2219
2269
  const jsonValue =
2220
2270
  storageValue === 'J'
2221
2271
  ? PostgreSQLDriver.escapeNullSequences(value)
@@ -2443,8 +2493,8 @@ export default class PostgreSQLDriver extends NymphDriver {
2443
2493
  typeof value === 'number'
2444
2494
  ? 'N'
2445
2495
  : typeof value === 'string'
2446
- ? 'S'
2447
- : 'J';
2496
+ ? 'S'
2497
+ : 'J';
2448
2498
  const jsonValue =
2449
2499
  storageValue === 'J'
2450
2500
  ? PostgreSQLDriver.escapeNullSequences(svalue)
@@ -1,4 +1,4 @@
1
- import { PostgreSQLDriverConfig } from './d';
1
+ import { PostgreSQLDriverConfig } from './d.js';
2
2
 
3
3
  export default {
4
4
  host: 'localhost',
package/src/conf/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { PostgreSQLDriverConfig } from './d';
1
+ export { PostgreSQLDriverConfig } from './d.js';
2
2
 
3
- import defaults from './defaults';
3
+ import defaults from './defaults.js';
4
4
  export { defaults as PostgreSQLDriverConfigDefaults };
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
- export * from './conf';
1
+ export * from './conf/index.js';
2
2
 
3
- import PostgreSQLDriver from './PostgreSQLDriver';
3
+ import PostgreSQLDriver from './PostgreSQLDriver.js';
4
4
  export { PostgreSQLDriver };
5
5
  export default PostgreSQLDriver;
package/tsconfig.json CHANGED
@@ -2,7 +2,9 @@
2
2
  "extends": "@tsconfig/recommended/tsconfig.json",
3
3
 
4
4
  "compilerOptions": {
5
- "lib": ["ES2023"],
5
+ "module": "ES2022",
6
+ "moduleResolution": "node",
7
+ "lib": ["ES2022"],
6
8
  "target": "ES2022",
7
9
  "noImplicitAny": true,
8
10
  "removeComments": false,