@nymphjs/driver-postgresql 1.0.0-beta.40 → 1.0.0-beta.42
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 +10 -0
- package/dist/PostgreSQLDriver.d.ts +1 -17
- package/dist/PostgreSQLDriver.js +43 -145
- package/dist/PostgreSQLDriver.js.map +1 -1
- package/package.json +6 -6
- package/src/PostgreSQLDriver.ts +43 -242
- package/dist/runPostgresqlSync.js +0 -41
- package/src/runPostgresqlSync.js +0 -41
- package/src/testpostgresql.js +0 -119
package/src/PostgreSQLDriver.ts
CHANGED
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Worker,
|
|
3
|
-
MessageChannel,
|
|
4
|
-
receiveMessageOnPort,
|
|
5
|
-
} from 'node:worker_threads';
|
|
6
|
-
import { resolve } from 'node:path';
|
|
7
1
|
import { Pool, PoolClient, PoolConfig, QueryResult } from 'pg';
|
|
8
2
|
import format from 'pg-format';
|
|
9
3
|
import {
|
|
@@ -49,8 +43,6 @@ export default class PostgreSQLDriver extends NymphDriver {
|
|
|
49
43
|
// @ts-ignore: this is assigned in connect(), which is called by the constructor.
|
|
50
44
|
protected link: Pool;
|
|
51
45
|
protected transaction: PostgreSQLDriverTransaction | null = null;
|
|
52
|
-
// @ts-ignore: this is assigned in connect(), which is called by the constructor.
|
|
53
|
-
protected worker: Worker;
|
|
54
46
|
|
|
55
47
|
static escape(input: string) {
|
|
56
48
|
return format.ident(input);
|
|
@@ -63,8 +55,7 @@ export default class PostgreSQLDriver extends NymphDriver {
|
|
|
63
55
|
constructor(
|
|
64
56
|
config: Partial<PostgreSQLDriverConfig>,
|
|
65
57
|
link?: Pool,
|
|
66
|
-
transaction?: PostgreSQLDriverTransaction
|
|
67
|
-
worker?: Worker
|
|
58
|
+
transaction?: PostgreSQLDriverTransaction
|
|
68
59
|
) {
|
|
69
60
|
super();
|
|
70
61
|
this.config = { ...defaults, ...config };
|
|
@@ -85,9 +76,6 @@ export default class PostgreSQLDriver extends NymphDriver {
|
|
|
85
76
|
if (transaction != null) {
|
|
86
77
|
this.transaction = transaction;
|
|
87
78
|
}
|
|
88
|
-
if (worker != null) {
|
|
89
|
-
this.worker = worker;
|
|
90
|
-
}
|
|
91
79
|
if (link == null) {
|
|
92
80
|
this.connect();
|
|
93
81
|
}
|
|
@@ -102,8 +90,7 @@ export default class PostgreSQLDriver extends NymphDriver {
|
|
|
102
90
|
return new PostgreSQLDriver(
|
|
103
91
|
this.config,
|
|
104
92
|
this.link,
|
|
105
|
-
this.transaction ?? undefined
|
|
106
|
-
this.worker ?? undefined
|
|
93
|
+
this.transaction ?? undefined
|
|
107
94
|
);
|
|
108
95
|
}
|
|
109
96
|
|
|
@@ -142,7 +129,6 @@ export default class PostgreSQLDriver extends NymphDriver {
|
|
|
142
129
|
})
|
|
143
130
|
);
|
|
144
131
|
connection.done();
|
|
145
|
-
this.worker.postMessage('halt');
|
|
146
132
|
}
|
|
147
133
|
} catch (e: any) {
|
|
148
134
|
this.connected = false;
|
|
@@ -152,17 +138,6 @@ export default class PostgreSQLDriver extends NymphDriver {
|
|
|
152
138
|
if (!this.connected) {
|
|
153
139
|
try {
|
|
154
140
|
this.link = new Pool(this.postgresqlConfig);
|
|
155
|
-
const worker = new Worker(resolve(__dirname, 'runPostgresqlSync.js'), {
|
|
156
|
-
workerData: this.postgresqlConfig,
|
|
157
|
-
});
|
|
158
|
-
worker.on('message', (message) => {
|
|
159
|
-
if (message === 'halted') {
|
|
160
|
-
worker.terminate();
|
|
161
|
-
} else if (typeof message === 'object' && 'error' in message) {
|
|
162
|
-
console.error('Worker Thread Error', message.error);
|
|
163
|
-
}
|
|
164
|
-
});
|
|
165
|
-
this.worker = worker;
|
|
166
141
|
this.connected = true;
|
|
167
142
|
} catch (e: any) {
|
|
168
143
|
if (
|
|
@@ -190,7 +165,6 @@ export default class PostgreSQLDriver extends NymphDriver {
|
|
|
190
165
|
public async disconnect() {
|
|
191
166
|
if (this.connected) {
|
|
192
167
|
await new Promise((resolve) => this.link.end(() => resolve(0)));
|
|
193
|
-
this.worker.postMessage('halt');
|
|
194
168
|
this.connected = false;
|
|
195
169
|
}
|
|
196
170
|
return this.connected;
|
|
@@ -215,10 +189,10 @@ export default class PostgreSQLDriver extends NymphDriver {
|
|
|
215
189
|
* @param etype The entity type to create a table for. If this is blank, the default tables are created.
|
|
216
190
|
* @returns True on success, false on failure.
|
|
217
191
|
*/
|
|
218
|
-
private createTables(etype: string | null = null) {
|
|
192
|
+
private async createTables(etype: string | null = null) {
|
|
219
193
|
if (etype != null) {
|
|
220
194
|
// Create the entity table.
|
|
221
|
-
this.
|
|
195
|
+
await this.queryRun(
|
|
222
196
|
`CREATE TABLE IF NOT EXISTS ${PostgreSQLDriver.escape(
|
|
223
197
|
`${this.prefix}entities_${etype}`
|
|
224
198
|
)} (
|
|
@@ -229,41 +203,41 @@ export default class PostgreSQLDriver extends NymphDriver {
|
|
|
229
203
|
PRIMARY KEY ("guid")
|
|
230
204
|
) WITH ( OIDS=FALSE );`
|
|
231
205
|
);
|
|
232
|
-
this.
|
|
206
|
+
await this.queryRun(
|
|
233
207
|
`ALTER TABLE ${PostgreSQLDriver.escape(
|
|
234
208
|
`${this.prefix}entities_${etype}`
|
|
235
209
|
)} OWNER TO ${PostgreSQLDriver.escape(this.config.user)};`
|
|
236
210
|
);
|
|
237
|
-
this.
|
|
211
|
+
await this.queryRun(
|
|
238
212
|
`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(
|
|
239
213
|
`${this.prefix}entities_${etype}_id_cdate`
|
|
240
214
|
)};`
|
|
241
215
|
);
|
|
242
|
-
this.
|
|
216
|
+
await this.queryRun(
|
|
243
217
|
`CREATE INDEX ${PostgreSQLDriver.escape(
|
|
244
218
|
`${this.prefix}entities_${etype}_id_cdate`
|
|
245
219
|
)} ON ${PostgreSQLDriver.escape(
|
|
246
220
|
`${this.prefix}entities_${etype}`
|
|
247
221
|
)} USING btree ("cdate");`
|
|
248
222
|
);
|
|
249
|
-
this.
|
|
223
|
+
await this.queryRun(
|
|
250
224
|
`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(
|
|
251
225
|
`${this.prefix}entities_${etype}_id_mdate`
|
|
252
226
|
)};`
|
|
253
227
|
);
|
|
254
|
-
this.
|
|
228
|
+
await this.queryRun(
|
|
255
229
|
`CREATE INDEX ${PostgreSQLDriver.escape(
|
|
256
230
|
`${this.prefix}entities_${etype}_id_mdate`
|
|
257
231
|
)} ON ${PostgreSQLDriver.escape(
|
|
258
232
|
`${this.prefix}entities_${etype}`
|
|
259
233
|
)} USING btree ("mdate");`
|
|
260
234
|
);
|
|
261
|
-
this.
|
|
235
|
+
await this.queryRun(
|
|
262
236
|
`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(
|
|
263
237
|
`${this.prefix}entities_${etype}_id_tags`
|
|
264
238
|
)};`
|
|
265
239
|
);
|
|
266
|
-
this.
|
|
240
|
+
await this.queryRun(
|
|
267
241
|
`CREATE INDEX ${PostgreSQLDriver.escape(
|
|
268
242
|
`${this.prefix}entities_${etype}_id_tags`
|
|
269
243
|
)} ON ${PostgreSQLDriver.escape(
|
|
@@ -271,7 +245,7 @@ export default class PostgreSQLDriver extends NymphDriver {
|
|
|
271
245
|
)} USING gin ("tags");`
|
|
272
246
|
);
|
|
273
247
|
// Create the data table.
|
|
274
|
-
this.
|
|
248
|
+
await this.queryRun(
|
|
275
249
|
`CREATE TABLE IF NOT EXISTS ${PostgreSQLDriver.escape(
|
|
276
250
|
`${this.prefix}data_${etype}`
|
|
277
251
|
)} (
|
|
@@ -285,53 +259,53 @@ export default class PostgreSQLDriver extends NymphDriver {
|
|
|
285
259
|
)} ("guid") MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE
|
|
286
260
|
) WITH ( OIDS=FALSE );`
|
|
287
261
|
);
|
|
288
|
-
this.
|
|
262
|
+
await this.queryRun(
|
|
289
263
|
`ALTER TABLE ${PostgreSQLDriver.escape(
|
|
290
264
|
`${this.prefix}data_${etype}`
|
|
291
265
|
)} OWNER TO ${PostgreSQLDriver.escape(this.config.user)};`
|
|
292
266
|
);
|
|
293
|
-
this.
|
|
267
|
+
await this.queryRun(
|
|
294
268
|
`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(
|
|
295
269
|
`${this.prefix}data_${etype}_id_guid`
|
|
296
270
|
)};`
|
|
297
271
|
);
|
|
298
|
-
this.
|
|
272
|
+
await this.queryRun(
|
|
299
273
|
`CREATE INDEX ${PostgreSQLDriver.escape(
|
|
300
274
|
`${this.prefix}data_${etype}_id_guid`
|
|
301
275
|
)} ON ${PostgreSQLDriver.escape(
|
|
302
276
|
`${this.prefix}data_${etype}`
|
|
303
277
|
)} USING btree ("guid");`
|
|
304
278
|
);
|
|
305
|
-
this.
|
|
279
|
+
await this.queryRun(
|
|
306
280
|
`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(
|
|
307
281
|
`${this.prefix}data_${etype}_id_name`
|
|
308
282
|
)};`
|
|
309
283
|
);
|
|
310
|
-
this.
|
|
284
|
+
await this.queryRun(
|
|
311
285
|
`CREATE INDEX ${PostgreSQLDriver.escape(
|
|
312
286
|
`${this.prefix}data_${etype}_id_name`
|
|
313
287
|
)} ON ${PostgreSQLDriver.escape(
|
|
314
288
|
`${this.prefix}data_${etype}`
|
|
315
289
|
)} USING btree ("name");`
|
|
316
290
|
);
|
|
317
|
-
this.
|
|
291
|
+
await this.queryRun(
|
|
318
292
|
`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(
|
|
319
293
|
`${this.prefix}data_${etype}_id_guid_name__user`
|
|
320
294
|
)};`
|
|
321
295
|
);
|
|
322
|
-
this.
|
|
296
|
+
await this.queryRun(
|
|
323
297
|
`CREATE INDEX ${PostgreSQLDriver.escape(
|
|
324
298
|
`${this.prefix}data_${etype}_id_guid_name__user`
|
|
325
299
|
)} ON ${PostgreSQLDriver.escape(
|
|
326
300
|
`${this.prefix}data_${etype}`
|
|
327
301
|
)} USING btree ("guid") WHERE "name" = 'user'::text;`
|
|
328
302
|
);
|
|
329
|
-
this.
|
|
303
|
+
await this.queryRun(
|
|
330
304
|
`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(
|
|
331
305
|
`${this.prefix}data_${etype}_id_guid_name__group`
|
|
332
306
|
)};`
|
|
333
307
|
);
|
|
334
|
-
this.
|
|
308
|
+
await this.queryRun(
|
|
335
309
|
`CREATE INDEX ${PostgreSQLDriver.escape(
|
|
336
310
|
`${this.prefix}data_${etype}_id_guid_name__group`
|
|
337
311
|
)} ON ${PostgreSQLDriver.escape(
|
|
@@ -339,7 +313,7 @@ export default class PostgreSQLDriver extends NymphDriver {
|
|
|
339
313
|
)} USING btree ("guid") WHERE "name" = 'group'::text;`
|
|
340
314
|
);
|
|
341
315
|
// Create the data comparisons table.
|
|
342
|
-
this.
|
|
316
|
+
await this.queryRun(
|
|
343
317
|
`CREATE TABLE IF NOT EXISTS ${PostgreSQLDriver.escape(
|
|
344
318
|
`${this.prefix}comparisons_${etype}`
|
|
345
319
|
)} (
|
|
@@ -355,53 +329,53 @@ export default class PostgreSQLDriver extends NymphDriver {
|
|
|
355
329
|
)} ("guid") MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE
|
|
356
330
|
) WITH ( OIDS=FALSE );`
|
|
357
331
|
);
|
|
358
|
-
this.
|
|
332
|
+
await this.queryRun(
|
|
359
333
|
`ALTER TABLE ${PostgreSQLDriver.escape(
|
|
360
334
|
`${this.prefix}comparisons_${etype}`
|
|
361
335
|
)} OWNER TO ${PostgreSQLDriver.escape(this.config.user)};`
|
|
362
336
|
);
|
|
363
|
-
this.
|
|
337
|
+
await this.queryRun(
|
|
364
338
|
`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(
|
|
365
339
|
`${this.prefix}comparisons_${etype}_id_guid`
|
|
366
340
|
)};`
|
|
367
341
|
);
|
|
368
|
-
this.
|
|
342
|
+
await this.queryRun(
|
|
369
343
|
`CREATE INDEX ${PostgreSQLDriver.escape(
|
|
370
344
|
`${this.prefix}comparisons_${etype}_id_guid`
|
|
371
345
|
)} ON ${PostgreSQLDriver.escape(
|
|
372
346
|
`${this.prefix}comparisons_${etype}`
|
|
373
347
|
)} USING btree ("guid");`
|
|
374
348
|
);
|
|
375
|
-
this.
|
|
349
|
+
await this.queryRun(
|
|
376
350
|
`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(
|
|
377
351
|
`${this.prefix}comparisons_${etype}_id_name`
|
|
378
352
|
)};`
|
|
379
353
|
);
|
|
380
|
-
this.
|
|
354
|
+
await this.queryRun(
|
|
381
355
|
`CREATE INDEX ${PostgreSQLDriver.escape(
|
|
382
356
|
`${this.prefix}comparisons_${etype}_id_name`
|
|
383
357
|
)} ON ${PostgreSQLDriver.escape(
|
|
384
358
|
`${this.prefix}comparisons_${etype}`
|
|
385
359
|
)} USING btree ("name");`
|
|
386
360
|
);
|
|
387
|
-
this.
|
|
361
|
+
await this.queryRun(
|
|
388
362
|
`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(
|
|
389
363
|
`${this.prefix}comparisons_${etype}_id_guid_name_truthy`
|
|
390
364
|
)};`
|
|
391
365
|
);
|
|
392
|
-
this.
|
|
366
|
+
await this.queryRun(
|
|
393
367
|
`CREATE INDEX ${PostgreSQLDriver.escape(
|
|
394
368
|
`${this.prefix}comparisons_${etype}_id_guid_name_truthy`
|
|
395
369
|
)} ON ${PostgreSQLDriver.escape(
|
|
396
370
|
`${this.prefix}comparisons_${etype}`
|
|
397
371
|
)} USING btree ("guid", "name") WHERE "truthy" = TRUE;`
|
|
398
372
|
);
|
|
399
|
-
this.
|
|
373
|
+
await this.queryRun(
|
|
400
374
|
`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(
|
|
401
375
|
`${this.prefix}comparisons_${etype}_id_guid_name_falsy`
|
|
402
376
|
)};`
|
|
403
377
|
);
|
|
404
|
-
this.
|
|
378
|
+
await this.queryRun(
|
|
405
379
|
`CREATE INDEX ${PostgreSQLDriver.escape(
|
|
406
380
|
`${this.prefix}comparisons_${etype}_id_guid_name_falsy`
|
|
407
381
|
)} ON ${PostgreSQLDriver.escape(
|
|
@@ -409,7 +383,7 @@ export default class PostgreSQLDriver extends NymphDriver {
|
|
|
409
383
|
)} USING btree ("guid", "name") WHERE "truthy" <> TRUE;`
|
|
410
384
|
);
|
|
411
385
|
// Create the references table.
|
|
412
|
-
this.
|
|
386
|
+
await this.queryRun(
|
|
413
387
|
`CREATE TABLE IF NOT EXISTS ${PostgreSQLDriver.escape(
|
|
414
388
|
`${this.prefix}references_${etype}`
|
|
415
389
|
)} (
|
|
@@ -423,41 +397,41 @@ export default class PostgreSQLDriver extends NymphDriver {
|
|
|
423
397
|
)} ("guid") MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE
|
|
424
398
|
) WITH ( OIDS=FALSE );`
|
|
425
399
|
);
|
|
426
|
-
this.
|
|
400
|
+
await this.queryRun(
|
|
427
401
|
`ALTER TABLE ${PostgreSQLDriver.escape(
|
|
428
402
|
`${this.prefix}references_${etype}`
|
|
429
403
|
)} OWNER TO ${PostgreSQLDriver.escape(this.config.user)};`
|
|
430
404
|
);
|
|
431
|
-
this.
|
|
405
|
+
await this.queryRun(
|
|
432
406
|
`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(
|
|
433
407
|
`${this.prefix}references_${etype}_id_guid`
|
|
434
408
|
)};`
|
|
435
409
|
);
|
|
436
|
-
this.
|
|
410
|
+
await this.queryRun(
|
|
437
411
|
`CREATE INDEX ${PostgreSQLDriver.escape(
|
|
438
412
|
`${this.prefix}references_${etype}_id_guid`
|
|
439
413
|
)} ON ${PostgreSQLDriver.escape(
|
|
440
414
|
`${this.prefix}references_${etype}`
|
|
441
415
|
)} USING btree ("guid");`
|
|
442
416
|
);
|
|
443
|
-
this.
|
|
417
|
+
await this.queryRun(
|
|
444
418
|
`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(
|
|
445
419
|
`${this.prefix}references_${etype}_id_name`
|
|
446
420
|
)};`
|
|
447
421
|
);
|
|
448
|
-
this.
|
|
422
|
+
await this.queryRun(
|
|
449
423
|
`CREATE INDEX ${PostgreSQLDriver.escape(
|
|
450
424
|
`${this.prefix}references_${etype}_id_name`
|
|
451
425
|
)} ON ${PostgreSQLDriver.escape(
|
|
452
426
|
`${this.prefix}references_${etype}`
|
|
453
427
|
)} USING btree ("name");`
|
|
454
428
|
);
|
|
455
|
-
this.
|
|
429
|
+
await this.queryRun(
|
|
456
430
|
`DROP INDEX IF EXISTS ${PostgreSQLDriver.escape(
|
|
457
431
|
`${this.prefix}references_${etype}_id_reference`
|
|
458
432
|
)};`
|
|
459
433
|
);
|
|
460
|
-
this.
|
|
434
|
+
await this.queryRun(
|
|
461
435
|
`CREATE INDEX ${PostgreSQLDriver.escape(
|
|
462
436
|
`${this.prefix}references_${etype}_id_reference`
|
|
463
437
|
)} ON ${PostgreSQLDriver.escape(
|
|
@@ -466,7 +440,7 @@ export default class PostgreSQLDriver extends NymphDriver {
|
|
|
466
440
|
);
|
|
467
441
|
} else {
|
|
468
442
|
// Create the UID table.
|
|
469
|
-
this.
|
|
443
|
+
await this.queryRun(
|
|
470
444
|
`CREATE TABLE IF NOT EXISTS ${PostgreSQLDriver.escape(
|
|
471
445
|
`${this.prefix}uids`
|
|
472
446
|
)} (
|
|
@@ -475,7 +449,7 @@ export default class PostgreSQLDriver extends NymphDriver {
|
|
|
475
449
|
PRIMARY KEY ("name")
|
|
476
450
|
) WITH ( OIDS = FALSE );`
|
|
477
451
|
);
|
|
478
|
-
this.
|
|
452
|
+
await this.queryRun(
|
|
479
453
|
`ALTER TABLE ${PostgreSQLDriver.escape(
|
|
480
454
|
`${this.prefix}uids`
|
|
481
455
|
)} OWNER TO ${PostgreSQLDriver.escape(this.config.user)};`
|
|
@@ -513,10 +487,10 @@ export default class PostgreSQLDriver extends NymphDriver {
|
|
|
513
487
|
return await runQuery();
|
|
514
488
|
} catch (e: any) {
|
|
515
489
|
const errorCode = e?.code;
|
|
516
|
-
if (errorCode === '42P01' && this.createTables()) {
|
|
490
|
+
if (errorCode === '42P01' && (await this.createTables())) {
|
|
517
491
|
// If the tables don't exist yet, create them.
|
|
518
492
|
for (let etype of etypes) {
|
|
519
|
-
this.createTables(etype);
|
|
493
|
+
await this.createTables(etype);
|
|
520
494
|
}
|
|
521
495
|
try {
|
|
522
496
|
return await runQuery();
|
|
@@ -532,34 +506,6 @@ export default class PostgreSQLDriver extends NymphDriver {
|
|
|
532
506
|
}
|
|
533
507
|
}
|
|
534
508
|
|
|
535
|
-
private querySync<T extends () => any>(
|
|
536
|
-
runQuery: T,
|
|
537
|
-
query: string,
|
|
538
|
-
etypes: string[] = []
|
|
539
|
-
): ReturnType<T> {
|
|
540
|
-
try {
|
|
541
|
-
return runQuery();
|
|
542
|
-
} catch (e: any) {
|
|
543
|
-
const errorCode = e?.code;
|
|
544
|
-
if (errorCode === '42P01' && this.createTables()) {
|
|
545
|
-
// If the tables don't exist yet, create them.
|
|
546
|
-
for (let etype of etypes) {
|
|
547
|
-
this.createTables(etype);
|
|
548
|
-
}
|
|
549
|
-
try {
|
|
550
|
-
return runQuery();
|
|
551
|
-
} catch (e2: any) {
|
|
552
|
-
throw new QueryFailedError(
|
|
553
|
-
'Query failed: ' + e2?.code + ' - ' + e2?.message,
|
|
554
|
-
query
|
|
555
|
-
);
|
|
556
|
-
}
|
|
557
|
-
} else {
|
|
558
|
-
throw e;
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
|
|
563
509
|
private queryIter(
|
|
564
510
|
query: string,
|
|
565
511
|
{
|
|
@@ -597,43 +543,6 @@ export default class PostgreSQLDriver extends NymphDriver {
|
|
|
597
543
|
);
|
|
598
544
|
}
|
|
599
545
|
|
|
600
|
-
private queryIterSync(
|
|
601
|
-
query: string,
|
|
602
|
-
{
|
|
603
|
-
etypes = [],
|
|
604
|
-
params = {},
|
|
605
|
-
}: { etypes?: string[]; params?: { [k: string]: any } } = {}
|
|
606
|
-
) {
|
|
607
|
-
const { query: newQuery, params: newParams } = this.translateQuery(
|
|
608
|
-
query,
|
|
609
|
-
params
|
|
610
|
-
);
|
|
611
|
-
return this.querySync(
|
|
612
|
-
() => {
|
|
613
|
-
const channel = new MessageChannel();
|
|
614
|
-
|
|
615
|
-
this.worker.postMessage(
|
|
616
|
-
{ query: newQuery, params: newParams, port: channel.port2 },
|
|
617
|
-
[channel.port2]
|
|
618
|
-
);
|
|
619
|
-
|
|
620
|
-
let output = undefined;
|
|
621
|
-
while (!output) {
|
|
622
|
-
output = receiveMessageOnPort(channel.port1);
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
if (output.message.error) {
|
|
626
|
-
throw new Error(output.message.error);
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
const { results } = output.message;
|
|
630
|
-
return results.rows;
|
|
631
|
-
},
|
|
632
|
-
`${query} -- ${JSON.stringify(params)}`,
|
|
633
|
-
etypes
|
|
634
|
-
);
|
|
635
|
-
}
|
|
636
|
-
|
|
637
546
|
private queryGet(
|
|
638
547
|
query: string,
|
|
639
548
|
{
|
|
@@ -708,43 +617,6 @@ export default class PostgreSQLDriver extends NymphDriver {
|
|
|
708
617
|
);
|
|
709
618
|
}
|
|
710
619
|
|
|
711
|
-
private queryRunSync(
|
|
712
|
-
query: string,
|
|
713
|
-
{
|
|
714
|
-
etypes = [],
|
|
715
|
-
params = {},
|
|
716
|
-
}: { etypes?: string[]; params?: { [k: string]: any } } = {}
|
|
717
|
-
) {
|
|
718
|
-
const { query: newQuery, params: newParams } = this.translateQuery(
|
|
719
|
-
query,
|
|
720
|
-
params
|
|
721
|
-
);
|
|
722
|
-
return this.querySync(
|
|
723
|
-
() => {
|
|
724
|
-
const channel = new MessageChannel();
|
|
725
|
-
|
|
726
|
-
this.worker.postMessage(
|
|
727
|
-
{ query: newQuery, params: newParams, port: channel.port2 },
|
|
728
|
-
[channel.port2]
|
|
729
|
-
);
|
|
730
|
-
|
|
731
|
-
let output = undefined;
|
|
732
|
-
while (!output) {
|
|
733
|
-
output = receiveMessageOnPort(channel.port1);
|
|
734
|
-
}
|
|
735
|
-
|
|
736
|
-
if (output.message.error) {
|
|
737
|
-
throw new Error(output.message.error);
|
|
738
|
-
}
|
|
739
|
-
|
|
740
|
-
const { results } = output.message;
|
|
741
|
-
return { rowCount: results.rowCount ?? 0 };
|
|
742
|
-
},
|
|
743
|
-
`${query} -- ${JSON.stringify(params)}`,
|
|
744
|
-
etypes
|
|
745
|
-
);
|
|
746
|
-
}
|
|
747
|
-
|
|
748
620
|
public async commit(name: string) {
|
|
749
621
|
if (name == null || typeof name !== 'string' || name.length === 0) {
|
|
750
622
|
throw new InvalidParametersError(
|
|
@@ -2009,26 +1881,6 @@ export default class PostgreSQLDriver extends NymphDriver {
|
|
|
2009
1881
|
};
|
|
2010
1882
|
}
|
|
2011
1883
|
|
|
2012
|
-
protected performQuerySync(
|
|
2013
|
-
options: Options,
|
|
2014
|
-
formattedSelectors: FormattedSelector[],
|
|
2015
|
-
etype: string
|
|
2016
|
-
): {
|
|
2017
|
-
result: any;
|
|
2018
|
-
} {
|
|
2019
|
-
const { query, params, etypes } = this.makeEntityQuery(
|
|
2020
|
-
options,
|
|
2021
|
-
formattedSelectors,
|
|
2022
|
-
etype
|
|
2023
|
-
);
|
|
2024
|
-
const result = (this.queryIterSync(query, { etypes, params }) || [])[
|
|
2025
|
-
Symbol.iterator
|
|
2026
|
-
]();
|
|
2027
|
-
return {
|
|
2028
|
-
result,
|
|
2029
|
-
};
|
|
2030
|
-
}
|
|
2031
|
-
|
|
2032
1884
|
public async getEntities<T extends EntityConstructor = EntityConstructor>(
|
|
2033
1885
|
options: Options<T> & { return: 'count' },
|
|
2034
1886
|
...selectors: Selector[]
|
|
@@ -2082,57 +1934,6 @@ export default class PostgreSQLDriver extends NymphDriver {
|
|
|
2082
1934
|
return value;
|
|
2083
1935
|
}
|
|
2084
1936
|
|
|
2085
|
-
protected getEntitiesSync<T extends EntityConstructor = EntityConstructor>(
|
|
2086
|
-
options: Options<T> & { return: 'count' },
|
|
2087
|
-
...selectors: Selector[]
|
|
2088
|
-
): number;
|
|
2089
|
-
protected getEntitiesSync<T extends EntityConstructor = EntityConstructor>(
|
|
2090
|
-
options: Options<T> & { return: 'guid' },
|
|
2091
|
-
...selectors: Selector[]
|
|
2092
|
-
): string[];
|
|
2093
|
-
protected getEntitiesSync<T extends EntityConstructor = EntityConstructor>(
|
|
2094
|
-
options?: Options<T>,
|
|
2095
|
-
...selectors: Selector[]
|
|
2096
|
-
): ReturnType<T['factorySync']>[];
|
|
2097
|
-
protected getEntitiesSync<T extends EntityConstructor = EntityConstructor>(
|
|
2098
|
-
options: Options<T> = {},
|
|
2099
|
-
...selectors: Selector[]
|
|
2100
|
-
): ReturnType<T['factorySync']>[] | string[] | number {
|
|
2101
|
-
const { result, process } = this.getEntitesRowLike<T>(
|
|
2102
|
-
// @ts-ignore: options is correct here.
|
|
2103
|
-
options,
|
|
2104
|
-
selectors,
|
|
2105
|
-
(options, formattedSelectors, etype) =>
|
|
2106
|
-
this.performQuerySync(options, formattedSelectors, etype),
|
|
2107
|
-
() => {
|
|
2108
|
-
const next: any = result.next();
|
|
2109
|
-
return next.done ? null : next.value;
|
|
2110
|
-
},
|
|
2111
|
-
() => undefined,
|
|
2112
|
-
(row) => Number(row.count),
|
|
2113
|
-
(row) => row.guid,
|
|
2114
|
-
(row) => ({
|
|
2115
|
-
tags: row.tags,
|
|
2116
|
-
cdate: isNaN(Number(row.cdate)) ? null : Number(row.cdate),
|
|
2117
|
-
mdate: isNaN(Number(row.mdate)) ? null : Number(row.mdate),
|
|
2118
|
-
}),
|
|
2119
|
-
(row) => ({
|
|
2120
|
-
name: row.name,
|
|
2121
|
-
svalue:
|
|
2122
|
-
row.value === 'N'
|
|
2123
|
-
? JSON.stringify(Number(row.number))
|
|
2124
|
-
: row.value === 'S'
|
|
2125
|
-
? JSON.stringify(row.string)
|
|
2126
|
-
: row.value,
|
|
2127
|
-
})
|
|
2128
|
-
);
|
|
2129
|
-
const value = process();
|
|
2130
|
-
if (value instanceof Error) {
|
|
2131
|
-
throw value;
|
|
2132
|
-
}
|
|
2133
|
-
return value;
|
|
2134
|
-
}
|
|
2135
|
-
|
|
2136
1937
|
public async getUID(name: string) {
|
|
2137
1938
|
if (name == null) {
|
|
2138
1939
|
throw new InvalidParametersError('Name not given for UID.');
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
const { isMainThread, workerData, parentPort } = require('node:worker_threads');
|
|
2
|
-
const pg = require('pg');
|
|
3
|
-
|
|
4
|
-
if (isMainThread) {
|
|
5
|
-
throw new Error("Don't load this file as main thread.");
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
try {
|
|
9
|
-
const postgresqlConfig = workerData;
|
|
10
|
-
const pool = new pg.Pool(postgresqlConfig);
|
|
11
|
-
|
|
12
|
-
parentPort.on('message', async (message) => {
|
|
13
|
-
if (message === 'halt') {
|
|
14
|
-
pool.end(() => {
|
|
15
|
-
parentPort.postMessage('halted');
|
|
16
|
-
});
|
|
17
|
-
} else {
|
|
18
|
-
const { query, params, port } = message;
|
|
19
|
-
|
|
20
|
-
try {
|
|
21
|
-
const results = await new Promise((resolve, reject) =>
|
|
22
|
-
pool.query(query, params).then(
|
|
23
|
-
(results) => resolve(results),
|
|
24
|
-
(error) => reject(error)
|
|
25
|
-
)
|
|
26
|
-
);
|
|
27
|
-
|
|
28
|
-
port.postMessage({
|
|
29
|
-
results: {
|
|
30
|
-
rows: results.rows,
|
|
31
|
-
rowCount: results.rowCount,
|
|
32
|
-
},
|
|
33
|
-
});
|
|
34
|
-
} catch (e) {
|
|
35
|
-
port.postMessage({ error: e.message });
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
} catch (e) {
|
|
40
|
-
parentPort.postMessage({ error: e.message });
|
|
41
|
-
}
|
package/src/runPostgresqlSync.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
const { isMainThread, workerData, parentPort } = require('node:worker_threads');
|
|
2
|
-
const pg = require('pg');
|
|
3
|
-
|
|
4
|
-
if (isMainThread) {
|
|
5
|
-
throw new Error("Don't load this file as main thread.");
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
try {
|
|
9
|
-
const postgresqlConfig = workerData;
|
|
10
|
-
const pool = new pg.Pool(postgresqlConfig);
|
|
11
|
-
|
|
12
|
-
parentPort.on('message', async (message) => {
|
|
13
|
-
if (message === 'halt') {
|
|
14
|
-
pool.end(() => {
|
|
15
|
-
parentPort.postMessage('halted');
|
|
16
|
-
});
|
|
17
|
-
} else {
|
|
18
|
-
const { query, params, port } = message;
|
|
19
|
-
|
|
20
|
-
try {
|
|
21
|
-
const results = await new Promise((resolve, reject) =>
|
|
22
|
-
pool.query(query, params).then(
|
|
23
|
-
(results) => resolve(results),
|
|
24
|
-
(error) => reject(error)
|
|
25
|
-
)
|
|
26
|
-
);
|
|
27
|
-
|
|
28
|
-
port.postMessage({
|
|
29
|
-
results: {
|
|
30
|
-
rows: results.rows,
|
|
31
|
-
rowCount: results.rowCount,
|
|
32
|
-
},
|
|
33
|
-
});
|
|
34
|
-
} catch (e) {
|
|
35
|
-
port.postMessage({ error: e.message });
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
} catch (e) {
|
|
40
|
-
parentPort.postMessage({ error: e.message });
|
|
41
|
-
}
|