sedentary-pg 0.0.34 → 0.0.37
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/dist/cjs/index.js +3 -0
- package/dist/cjs/pgdb.js +68 -44
- package/dist/es/index.js +3 -0
- package/dist/es/pgdb.js +68 -46
- package/dist/types/index.d.ts +1 -0
- package/dist/types/pgdb.d.ts +6 -3
- package/package.json +4 -4
package/dist/cjs/index.js
CHANGED
package/dist/cjs/pgdb.js
CHANGED
|
@@ -16,33 +16,40 @@ const needDrop = [
|
|
|
16
16
|
["INT8", "timestamptz"]
|
|
17
17
|
];
|
|
18
18
|
const needUsing = [
|
|
19
|
+
["BOOLEAN", "varchar"],
|
|
19
20
|
["DATETIME", "varchar"],
|
|
20
21
|
["INT", "varchar"],
|
|
21
22
|
["INT8", "varchar"],
|
|
22
23
|
["NUMBER", "varchar"]
|
|
23
24
|
];
|
|
24
|
-
const types = { int2: "SMALLINT", int4: "INTEGER", int8: "BIGINT", numeric: "NUMERIC", timestamptz: "DATETIME", varchar: "VARCHAR" };
|
|
25
|
+
const types = { bool: "BOOL", int2: "SMALLINT", int4: "INTEGER", int8: "BIGINT", numeric: "NUMERIC", timestamptz: "DATETIME", varchar: "VARCHAR" };
|
|
25
26
|
const actions = { cascade: "c", "no action": "a", restrict: "r", "set default": "d", "set null": "n" };
|
|
26
27
|
function parseInt8(value) {
|
|
27
28
|
return BigInt(value);
|
|
28
29
|
}
|
|
30
|
+
function parseNumber(value) {
|
|
31
|
+
return parseFloat(value);
|
|
32
|
+
}
|
|
29
33
|
pg_1.types.setTypeParser(20, parseInt8);
|
|
34
|
+
pg_1.types.setTypeParser(1700, parseNumber);
|
|
30
35
|
class PGDB extends sedentary_1.DB {
|
|
31
36
|
constructor(connection, log) {
|
|
32
37
|
super(log);
|
|
33
|
-
this.
|
|
34
|
-
this.client = {};
|
|
38
|
+
this._client = {};
|
|
35
39
|
this.indexes = [];
|
|
36
|
-
this.
|
|
40
|
+
this.oidLoad = {};
|
|
41
|
+
this.released = false;
|
|
37
42
|
this.version = 0;
|
|
43
|
+
this.pool = new pg_1.Pool(connection);
|
|
38
44
|
}
|
|
39
45
|
async connect() {
|
|
40
|
-
this.
|
|
41
|
-
const res = await this.
|
|
46
|
+
this._client = await this.pool.connect();
|
|
47
|
+
const res = await this._client.query("SELECT version()");
|
|
42
48
|
this.version = parseInt(res.rows[0].version.split(" ")[1].split(".")[0], 10);
|
|
43
49
|
}
|
|
44
50
|
async end() {
|
|
45
|
-
this.
|
|
51
|
+
if (!this.released)
|
|
52
|
+
this._client.release();
|
|
46
53
|
await this.pool.end();
|
|
47
54
|
}
|
|
48
55
|
defaultNeq(src, value) {
|
|
@@ -52,9 +59,13 @@ class PGDB extends sedentary_1.DB {
|
|
|
52
59
|
}
|
|
53
60
|
async begin() {
|
|
54
61
|
const ret = new TransactionPG(this.log, await this.pool.connect());
|
|
55
|
-
|
|
62
|
+
this.log("BEGIN");
|
|
63
|
+
await ret._client.query("BEGIN");
|
|
56
64
|
return ret;
|
|
57
65
|
}
|
|
66
|
+
async client() {
|
|
67
|
+
return await this.pool.connect();
|
|
68
|
+
}
|
|
58
69
|
escape(value) {
|
|
59
70
|
if (value === null || value === undefined)
|
|
60
71
|
throw new Error("SedentaryPG: Can't escape null nor undefined values; use the 'IS NULL' operator instead");
|
|
@@ -78,7 +89,7 @@ class PGDB extends sedentary_1.DB {
|
|
|
78
89
|
return async (where, order, tx, lock) => {
|
|
79
90
|
const { oid } = table;
|
|
80
91
|
const ret = [];
|
|
81
|
-
const client = tx ? tx.
|
|
92
|
+
const client = tx ? tx._client : await this.pool.connect();
|
|
82
93
|
const oidPK = {};
|
|
83
94
|
try {
|
|
84
95
|
const forUpdate = lock ? " FOR UPDATE" : "";
|
|
@@ -123,7 +134,7 @@ class PGDB extends sedentary_1.DB {
|
|
|
123
134
|
const pkAttrnName = pk.attributeName;
|
|
124
135
|
const pkFldName = pk.fieldName;
|
|
125
136
|
return async function () {
|
|
126
|
-
const client = this.tx ? this.tx.
|
|
137
|
+
const client = this.tx ? this.tx._client : await self.pool.connect();
|
|
127
138
|
let removed = false;
|
|
128
139
|
try {
|
|
129
140
|
const query = `DELETE FROM ${tableName} WHERE ${pkFldName} = ${self.escape(this[pkAttrnName])}`;
|
|
@@ -143,7 +154,7 @@ class PGDB extends sedentary_1.DB {
|
|
|
143
154
|
const pkAttrnName = pk.attributeName;
|
|
144
155
|
const pkFldName = pk.fieldName;
|
|
145
156
|
return async function () {
|
|
146
|
-
const client = this.tx ? this.tx.
|
|
157
|
+
const client = this.tx ? this.tx._client : await self.pool.connect();
|
|
147
158
|
let changed = false;
|
|
148
159
|
try {
|
|
149
160
|
const { loaded } = this;
|
|
@@ -186,7 +197,7 @@ class PGDB extends sedentary_1.DB {
|
|
|
186
197
|
}
|
|
187
198
|
async dropConstraints(table) {
|
|
188
199
|
const indexes = [];
|
|
189
|
-
const res = await this.
|
|
200
|
+
const res = await this._client.query("SELECT confdeltype, confupdtype, conindid, conname, contype FROM pg_constraint WHERE conrelid = $1 ORDER BY conname", [table.oid]);
|
|
190
201
|
for (const row of res.rows) {
|
|
191
202
|
const arr = table.constraints.filter(_ => _.constraintName === row.conname && _.type === row.contype);
|
|
192
203
|
let drop = false;
|
|
@@ -203,7 +214,7 @@ class PGDB extends sedentary_1.DB {
|
|
|
203
214
|
const statement = `ALTER TABLE ${table.tableName} DROP CONSTRAINT ${row.conname} CASCADE`;
|
|
204
215
|
this.syncLog(statement);
|
|
205
216
|
if (this.sync)
|
|
206
|
-
await this.
|
|
217
|
+
await this._client.query(statement);
|
|
207
218
|
}
|
|
208
219
|
}
|
|
209
220
|
return indexes;
|
|
@@ -212,10 +223,10 @@ class PGDB extends sedentary_1.DB {
|
|
|
212
223
|
const statement = `ALTER TABLE ${tableName} DROP COLUMN ${fieldName}`;
|
|
213
224
|
this.syncLog(statement);
|
|
214
225
|
if (this.sync)
|
|
215
|
-
await this.
|
|
226
|
+
await this._client.query(statement);
|
|
216
227
|
}
|
|
217
228
|
async dropFields(table) {
|
|
218
|
-
const res = await this.
|
|
229
|
+
const res = await this._client.query("SELECT attname FROM pg_attribute WHERE attrelid = $1 AND attnum > 0 AND attisdropped = false AND attinhcount = 0", [table.oid]);
|
|
219
230
|
for (const i in res.rows)
|
|
220
231
|
if (!table.findField(res.rows[i].attname))
|
|
221
232
|
await this.dropField(table.tableName, res.rows[i].attname);
|
|
@@ -223,7 +234,7 @@ class PGDB extends sedentary_1.DB {
|
|
|
223
234
|
async dropIndexes(table, constraintIndexes) {
|
|
224
235
|
const { indexes, oid } = table;
|
|
225
236
|
const iobject = {};
|
|
226
|
-
const res = await this.
|
|
237
|
+
const res = await this._client.query("SELECT amname, attname, indexrelid, indisunique, relname FROM pg_class, pg_index, pg_attribute, pg_am WHERE indrelid = $1 AND indexrelid = pg_class.oid AND attrelid = pg_class.oid AND relam = pg_am.oid ORDER BY attnum", [oid]);
|
|
227
238
|
for (const row of res.rows) {
|
|
228
239
|
const { amname, attname, indexrelid, indisunique, relname } = row;
|
|
229
240
|
if (!constraintIndexes.includes(indexrelid)) {
|
|
@@ -245,13 +256,13 @@ class PGDB extends sedentary_1.DB {
|
|
|
245
256
|
const statement = `DROP INDEX ${index}`;
|
|
246
257
|
this.syncLog(statement);
|
|
247
258
|
if (this.sync)
|
|
248
|
-
await this.
|
|
259
|
+
await this._client.query(statement);
|
|
249
260
|
}
|
|
250
261
|
}
|
|
251
262
|
async syncConstraints(table) {
|
|
252
263
|
for (const constraint of table.constraints) {
|
|
253
264
|
const { attribute, constraintName, type } = constraint;
|
|
254
|
-
const res = await this.
|
|
265
|
+
const res = await this._client.query("SELECT attname FROM pg_attribute, pg_constraint WHERE attrelid = $1 AND conrelid = $1 AND attnum = conkey[1] AND attname = $2", [
|
|
255
266
|
table.oid,
|
|
256
267
|
attribute.fieldName
|
|
257
268
|
]);
|
|
@@ -268,19 +279,30 @@ class PGDB extends sedentary_1.DB {
|
|
|
268
279
|
const statement = `ALTER TABLE ${table.tableName} ADD CONSTRAINT ${constraintName} ${query}`;
|
|
269
280
|
this.syncLog(statement);
|
|
270
281
|
if (this.sync)
|
|
271
|
-
await this.
|
|
282
|
+
await this._client.query(statement);
|
|
272
283
|
}
|
|
273
284
|
}
|
|
274
285
|
}
|
|
275
286
|
async syncDataBase() {
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
287
|
+
try {
|
|
288
|
+
await super.syncDataBase();
|
|
289
|
+
for (const table of this.tables)
|
|
290
|
+
this.oidLoad[table.oid || 0] = (ids) => table.model.load({ [table.pk.attributeName]: ["IN", ids] });
|
|
291
|
+
}
|
|
292
|
+
catch (e) {
|
|
293
|
+
throw e;
|
|
294
|
+
}
|
|
295
|
+
finally {
|
|
296
|
+
this.released = true;
|
|
297
|
+
this._client.release();
|
|
298
|
+
}
|
|
279
299
|
}
|
|
280
300
|
fieldType(attribute) {
|
|
281
301
|
const { size, type } = attribute;
|
|
282
302
|
let ret;
|
|
283
303
|
switch (type) {
|
|
304
|
+
case "BOOLEAN":
|
|
305
|
+
return ["BOOL", "BOOL"];
|
|
284
306
|
case "DATETIME":
|
|
285
307
|
return ["DATETIME", "TIMESTAMP (3) WITH TIME ZONE"];
|
|
286
308
|
case "NUMBER":
|
|
@@ -302,18 +324,18 @@ class PGDB extends sedentary_1.DB {
|
|
|
302
324
|
const defaultValue = attribute.defaultValue === undefined ? (autoIncrement && fieldName === "id" ? `nextval('${tableName}_id_seq'::regclass)` : undefined) : this.escape(attribute.defaultValue);
|
|
303
325
|
const [base, type] = this.fieldType(attribute);
|
|
304
326
|
const where = "attrelid = $1 AND attnum > 0 AND atttypid = pg_type.oid AND attislocal = 't' AND attname = $2";
|
|
305
|
-
const res = await this.
|
|
327
|
+
const res = await this._client.query(`SELECT attnotnull, atttypmod, typname, ${(0, adsrc_1.adsrc)(this.version)} FROM pg_type, pg_attribute LEFT JOIN pg_attrdef ON adrelid = attrelid AND adnum = attnum WHERE ${where}`, [oid, fieldName]);
|
|
306
328
|
const addField = async () => {
|
|
307
329
|
const statement = `ALTER TABLE ${tableName} ADD COLUMN ${fieldName} ${type}`;
|
|
308
330
|
this.syncLog(statement);
|
|
309
331
|
if (this.sync)
|
|
310
|
-
await this.
|
|
332
|
+
await this._client.query(statement);
|
|
311
333
|
};
|
|
312
334
|
const dropDefault = async () => {
|
|
313
335
|
const statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} DROP DEFAULT`;
|
|
314
336
|
this.syncLog(statement);
|
|
315
337
|
if (this.sync)
|
|
316
|
-
await this.
|
|
338
|
+
await this._client.query(statement);
|
|
317
339
|
};
|
|
318
340
|
const setNotNull = async (isNotNull) => {
|
|
319
341
|
if (isNotNull === notNull)
|
|
@@ -321,19 +343,19 @@ class PGDB extends sedentary_1.DB {
|
|
|
321
343
|
const statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} ${notNull ? "SET" : "DROP"} NOT NULL`;
|
|
322
344
|
this.syncLog(statement);
|
|
323
345
|
if (this.sync)
|
|
324
|
-
await this.
|
|
346
|
+
await this._client.query(statement);
|
|
325
347
|
};
|
|
326
348
|
const setDefault = async (isNotNull, create) => {
|
|
327
349
|
if (defaultValue !== undefined) {
|
|
328
350
|
let statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} SET DEFAULT ${defaultValue}`;
|
|
329
351
|
this.syncLog(statement);
|
|
330
352
|
if (this.sync)
|
|
331
|
-
await this.
|
|
353
|
+
await this._client.query(statement);
|
|
332
354
|
if (!isNotNull && !create) {
|
|
333
355
|
statement = `UPDATE ${tableName} SET ${fieldName} = ${defaultValue} WHERE ${fieldName} IS NULL`;
|
|
334
356
|
this.syncLog(statement);
|
|
335
357
|
if (this.sync)
|
|
336
|
-
this.
|
|
358
|
+
this._client.query(statement);
|
|
337
359
|
}
|
|
338
360
|
}
|
|
339
361
|
await setNotNull(isNotNull);
|
|
@@ -357,7 +379,7 @@ class PGDB extends sedentary_1.DB {
|
|
|
357
379
|
const statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} TYPE ${type}${using}`;
|
|
358
380
|
this.syncLog(statement);
|
|
359
381
|
if (this.sync)
|
|
360
|
-
await this.
|
|
382
|
+
await this._client.query(statement);
|
|
361
383
|
await setDefault(attnotnull, false);
|
|
362
384
|
}
|
|
363
385
|
}
|
|
@@ -379,7 +401,7 @@ class PGDB extends sedentary_1.DB {
|
|
|
379
401
|
const statement = `CREATE${unique ? " UNIQUE" : ""} INDEX ${indexName} ON ${tableName} USING ${type} (${fields.join(", ")})`;
|
|
380
402
|
this.syncLog(statement);
|
|
381
403
|
if (this.sync)
|
|
382
|
-
await this.
|
|
404
|
+
await this._client.query(statement);
|
|
383
405
|
}
|
|
384
406
|
}
|
|
385
407
|
}
|
|
@@ -389,14 +411,13 @@ class PGDB extends sedentary_1.DB {
|
|
|
389
411
|
const statement = `ALTER SEQUENCE ${table.tableName}_id_seq OWNED BY ${table.tableName}.id`;
|
|
390
412
|
this.syncLog(statement);
|
|
391
413
|
if (this.sync)
|
|
392
|
-
await this.
|
|
414
|
+
await this._client.query(statement);
|
|
393
415
|
}
|
|
394
416
|
async syncTable(table) {
|
|
395
|
-
var _a;
|
|
396
417
|
if (table.autoIncrement) {
|
|
397
418
|
await (async () => {
|
|
398
419
|
try {
|
|
399
|
-
await this.
|
|
420
|
+
await this._client.query(`SELECT currval('${table.tableName}_id_seq')`);
|
|
400
421
|
}
|
|
401
422
|
catch (e) {
|
|
402
423
|
if (e instanceof pg_1.DatabaseError && e.code === "55000")
|
|
@@ -405,7 +426,7 @@ class PGDB extends sedentary_1.DB {
|
|
|
405
426
|
const statement = `CREATE SEQUENCE ${table.tableName}_id_seq`;
|
|
406
427
|
this.syncLog(statement);
|
|
407
428
|
if (this.sync)
|
|
408
|
-
await this.
|
|
429
|
+
await this._client.query(statement);
|
|
409
430
|
table.autoIncrementOwn = true;
|
|
410
431
|
return;
|
|
411
432
|
}
|
|
@@ -414,11 +435,11 @@ class PGDB extends sedentary_1.DB {
|
|
|
414
435
|
})();
|
|
415
436
|
}
|
|
416
437
|
let create = false;
|
|
417
|
-
const resTable = await this.
|
|
438
|
+
const resTable = await this._client.query("SELECT oid FROM pg_class WHERE relname = $1", [table.tableName]);
|
|
418
439
|
if (resTable.rowCount) {
|
|
419
440
|
table.oid = resTable.rows[0].oid;
|
|
420
441
|
let drop = false;
|
|
421
|
-
const resParent = await this.
|
|
442
|
+
const resParent = await this._client.query("SELECT inhparent FROM pg_inherits WHERE inhrelid = $1", [table.oid]);
|
|
422
443
|
if (resParent.rowCount) {
|
|
423
444
|
if (!table.parent)
|
|
424
445
|
drop = true;
|
|
@@ -433,7 +454,7 @@ class PGDB extends sedentary_1.DB {
|
|
|
433
454
|
create = true;
|
|
434
455
|
this.syncLog(statement);
|
|
435
456
|
if (this.sync)
|
|
436
|
-
await this.
|
|
457
|
+
await this._client.query(statement);
|
|
437
458
|
}
|
|
438
459
|
}
|
|
439
460
|
else
|
|
@@ -443,9 +464,9 @@ class PGDB extends sedentary_1.DB {
|
|
|
443
464
|
const statement = `CREATE TABLE ${table.tableName} ()${parent}`;
|
|
444
465
|
this.syncLog(statement);
|
|
445
466
|
if (this.sync)
|
|
446
|
-
await this.
|
|
447
|
-
const resTable = await this.
|
|
448
|
-
table.oid =
|
|
467
|
+
await this._client.query(statement);
|
|
468
|
+
const resTable = await this._client.query("SELECT oid FROM pg_class WHERE relname = $1", [table.tableName]);
|
|
469
|
+
table.oid = resTable.rows[0]?.oid;
|
|
449
470
|
}
|
|
450
471
|
}
|
|
451
472
|
}
|
|
@@ -454,16 +475,19 @@ class TransactionPG extends sedentary_1.Transaction {
|
|
|
454
475
|
constructor(log, client) {
|
|
455
476
|
super(log);
|
|
456
477
|
this.released = false;
|
|
457
|
-
this.
|
|
478
|
+
this._client = client;
|
|
479
|
+
}
|
|
480
|
+
async client() {
|
|
481
|
+
return this._client;
|
|
458
482
|
}
|
|
459
483
|
release() {
|
|
460
484
|
this.released = true;
|
|
461
|
-
this.
|
|
485
|
+
this._client.release();
|
|
462
486
|
}
|
|
463
487
|
async commit() {
|
|
464
488
|
if (!this.released) {
|
|
465
489
|
this.log("COMMIT");
|
|
466
|
-
await this.
|
|
490
|
+
await this._client.query("COMMIT");
|
|
467
491
|
this.release();
|
|
468
492
|
super.commit();
|
|
469
493
|
}
|
|
@@ -473,7 +497,7 @@ class TransactionPG extends sedentary_1.Transaction {
|
|
|
473
497
|
if (!this.released) {
|
|
474
498
|
super.rollback();
|
|
475
499
|
this.log("ROLLBACK");
|
|
476
|
-
await this.
|
|
500
|
+
await this._client.query("ROLLBACK");
|
|
477
501
|
}
|
|
478
502
|
}
|
|
479
503
|
finally {
|
package/dist/es/index.js
CHANGED
package/dist/es/pgdb.js
CHANGED
|
@@ -10,37 +10,41 @@ const needDrop = [
|
|
|
10
10
|
["INT8", "timestamptz"]
|
|
11
11
|
];
|
|
12
12
|
const needUsing = [
|
|
13
|
+
["BOOLEAN", "varchar"],
|
|
13
14
|
["DATETIME", "varchar"],
|
|
14
15
|
["INT", "varchar"],
|
|
15
16
|
["INT8", "varchar"],
|
|
16
17
|
["NUMBER", "varchar"]
|
|
17
18
|
];
|
|
18
|
-
const types = { int2: "SMALLINT", int4: "INTEGER", int8: "BIGINT", numeric: "NUMERIC", timestamptz: "DATETIME", varchar: "VARCHAR" };
|
|
19
|
+
const types = { bool: "BOOL", int2: "SMALLINT", int4: "INTEGER", int8: "BIGINT", numeric: "NUMERIC", timestamptz: "DATETIME", varchar: "VARCHAR" };
|
|
19
20
|
const actions = { cascade: "c", "no action": "a", restrict: "r", "set default": "d", "set null": "n" };
|
|
20
21
|
function parseInt8(value) {
|
|
21
22
|
return BigInt(value);
|
|
22
23
|
}
|
|
24
|
+
function parseNumber(value) {
|
|
25
|
+
return parseFloat(value);
|
|
26
|
+
}
|
|
23
27
|
PGtypes.setTypeParser(20, parseInt8);
|
|
28
|
+
PGtypes.setTypeParser(1700, parseNumber);
|
|
24
29
|
export class PGDB extends DB {
|
|
25
|
-
|
|
26
|
-
indexes;
|
|
30
|
+
_client = {};
|
|
31
|
+
indexes = [];
|
|
27
32
|
oidLoad = {};
|
|
28
33
|
pool;
|
|
29
|
-
|
|
34
|
+
released = false;
|
|
35
|
+
version = 0;
|
|
30
36
|
constructor(connection, log) {
|
|
31
37
|
super(log);
|
|
32
|
-
this.client = {};
|
|
33
|
-
this.indexes = [];
|
|
34
38
|
this.pool = new Pool(connection);
|
|
35
|
-
this.version = 0;
|
|
36
39
|
}
|
|
37
40
|
async connect() {
|
|
38
|
-
this.
|
|
39
|
-
const res = await this.
|
|
41
|
+
this._client = await this.pool.connect();
|
|
42
|
+
const res = await this._client.query("SELECT version()");
|
|
40
43
|
this.version = parseInt(res.rows[0].version.split(" ")[1].split(".")[0], 10);
|
|
41
44
|
}
|
|
42
45
|
async end() {
|
|
43
|
-
this.
|
|
46
|
+
if (!this.released)
|
|
47
|
+
this._client.release();
|
|
44
48
|
await this.pool.end();
|
|
45
49
|
}
|
|
46
50
|
defaultNeq(src, value) {
|
|
@@ -50,9 +54,13 @@ export class PGDB extends DB {
|
|
|
50
54
|
}
|
|
51
55
|
async begin() {
|
|
52
56
|
const ret = new TransactionPG(this.log, await this.pool.connect());
|
|
53
|
-
|
|
57
|
+
this.log("BEGIN");
|
|
58
|
+
await ret._client.query("BEGIN");
|
|
54
59
|
return ret;
|
|
55
60
|
}
|
|
61
|
+
async client() {
|
|
62
|
+
return await this.pool.connect();
|
|
63
|
+
}
|
|
56
64
|
escape(value) {
|
|
57
65
|
if (value === null || value === undefined)
|
|
58
66
|
throw new Error("SedentaryPG: Can't escape null nor undefined values; use the 'IS NULL' operator instead");
|
|
@@ -76,7 +84,7 @@ export class PGDB extends DB {
|
|
|
76
84
|
return async (where, order, tx, lock) => {
|
|
77
85
|
const { oid } = table;
|
|
78
86
|
const ret = [];
|
|
79
|
-
const client = tx ? tx.
|
|
87
|
+
const client = tx ? tx._client : await this.pool.connect();
|
|
80
88
|
const oidPK = {};
|
|
81
89
|
try {
|
|
82
90
|
const forUpdate = lock ? " FOR UPDATE" : "";
|
|
@@ -121,7 +129,7 @@ export class PGDB extends DB {
|
|
|
121
129
|
const pkAttrnName = pk.attributeName;
|
|
122
130
|
const pkFldName = pk.fieldName;
|
|
123
131
|
return async function () {
|
|
124
|
-
const client = this.tx ? this.tx.
|
|
132
|
+
const client = this.tx ? this.tx._client : await self.pool.connect();
|
|
125
133
|
let removed = false;
|
|
126
134
|
try {
|
|
127
135
|
const query = `DELETE FROM ${tableName} WHERE ${pkFldName} = ${self.escape(this[pkAttrnName])}`;
|
|
@@ -141,7 +149,7 @@ export class PGDB extends DB {
|
|
|
141
149
|
const pkAttrnName = pk.attributeName;
|
|
142
150
|
const pkFldName = pk.fieldName;
|
|
143
151
|
return async function () {
|
|
144
|
-
const client = this.tx ? this.tx.
|
|
152
|
+
const client = this.tx ? this.tx._client : await self.pool.connect();
|
|
145
153
|
let changed = false;
|
|
146
154
|
try {
|
|
147
155
|
const { loaded } = this;
|
|
@@ -184,7 +192,7 @@ export class PGDB extends DB {
|
|
|
184
192
|
}
|
|
185
193
|
async dropConstraints(table) {
|
|
186
194
|
const indexes = [];
|
|
187
|
-
const res = await this.
|
|
195
|
+
const res = await this._client.query("SELECT confdeltype, confupdtype, conindid, conname, contype FROM pg_constraint WHERE conrelid = $1 ORDER BY conname", [table.oid]);
|
|
188
196
|
for (const row of res.rows) {
|
|
189
197
|
const arr = table.constraints.filter(_ => _.constraintName === row.conname && _.type === row.contype);
|
|
190
198
|
let drop = false;
|
|
@@ -201,7 +209,7 @@ export class PGDB extends DB {
|
|
|
201
209
|
const statement = `ALTER TABLE ${table.tableName} DROP CONSTRAINT ${row.conname} CASCADE`;
|
|
202
210
|
this.syncLog(statement);
|
|
203
211
|
if (this.sync)
|
|
204
|
-
await this.
|
|
212
|
+
await this._client.query(statement);
|
|
205
213
|
}
|
|
206
214
|
}
|
|
207
215
|
return indexes;
|
|
@@ -210,10 +218,10 @@ export class PGDB extends DB {
|
|
|
210
218
|
const statement = `ALTER TABLE ${tableName} DROP COLUMN ${fieldName}`;
|
|
211
219
|
this.syncLog(statement);
|
|
212
220
|
if (this.sync)
|
|
213
|
-
await this.
|
|
221
|
+
await this._client.query(statement);
|
|
214
222
|
}
|
|
215
223
|
async dropFields(table) {
|
|
216
|
-
const res = await this.
|
|
224
|
+
const res = await this._client.query("SELECT attname FROM pg_attribute WHERE attrelid = $1 AND attnum > 0 AND attisdropped = false AND attinhcount = 0", [table.oid]);
|
|
217
225
|
for (const i in res.rows)
|
|
218
226
|
if (!table.findField(res.rows[i].attname))
|
|
219
227
|
await this.dropField(table.tableName, res.rows[i].attname);
|
|
@@ -221,7 +229,7 @@ export class PGDB extends DB {
|
|
|
221
229
|
async dropIndexes(table, constraintIndexes) {
|
|
222
230
|
const { indexes, oid } = table;
|
|
223
231
|
const iobject = {};
|
|
224
|
-
const res = await this.
|
|
232
|
+
const res = await this._client.query("SELECT amname, attname, indexrelid, indisunique, relname FROM pg_class, pg_index, pg_attribute, pg_am WHERE indrelid = $1 AND indexrelid = pg_class.oid AND attrelid = pg_class.oid AND relam = pg_am.oid ORDER BY attnum", [oid]);
|
|
225
233
|
for (const row of res.rows) {
|
|
226
234
|
const { amname, attname, indexrelid, indisunique, relname } = row;
|
|
227
235
|
if (!constraintIndexes.includes(indexrelid)) {
|
|
@@ -243,13 +251,13 @@ export class PGDB extends DB {
|
|
|
243
251
|
const statement = `DROP INDEX ${index}`;
|
|
244
252
|
this.syncLog(statement);
|
|
245
253
|
if (this.sync)
|
|
246
|
-
await this.
|
|
254
|
+
await this._client.query(statement);
|
|
247
255
|
}
|
|
248
256
|
}
|
|
249
257
|
async syncConstraints(table) {
|
|
250
258
|
for (const constraint of table.constraints) {
|
|
251
259
|
const { attribute, constraintName, type } = constraint;
|
|
252
|
-
const res = await this.
|
|
260
|
+
const res = await this._client.query("SELECT attname FROM pg_attribute, pg_constraint WHERE attrelid = $1 AND conrelid = $1 AND attnum = conkey[1] AND attname = $2", [
|
|
253
261
|
table.oid,
|
|
254
262
|
attribute.fieldName
|
|
255
263
|
]);
|
|
@@ -266,19 +274,30 @@ export class PGDB extends DB {
|
|
|
266
274
|
const statement = `ALTER TABLE ${table.tableName} ADD CONSTRAINT ${constraintName} ${query}`;
|
|
267
275
|
this.syncLog(statement);
|
|
268
276
|
if (this.sync)
|
|
269
|
-
await this.
|
|
277
|
+
await this._client.query(statement);
|
|
270
278
|
}
|
|
271
279
|
}
|
|
272
280
|
}
|
|
273
281
|
async syncDataBase() {
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
282
|
+
try {
|
|
283
|
+
await super.syncDataBase();
|
|
284
|
+
for (const table of this.tables)
|
|
285
|
+
this.oidLoad[table.oid || 0] = (ids) => table.model.load({ [table.pk.attributeName]: ["IN", ids] });
|
|
286
|
+
}
|
|
287
|
+
catch (e) {
|
|
288
|
+
throw e;
|
|
289
|
+
}
|
|
290
|
+
finally {
|
|
291
|
+
this.released = true;
|
|
292
|
+
this._client.release();
|
|
293
|
+
}
|
|
277
294
|
}
|
|
278
295
|
fieldType(attribute) {
|
|
279
296
|
const { size, type } = attribute;
|
|
280
297
|
let ret;
|
|
281
298
|
switch (type) {
|
|
299
|
+
case "BOOLEAN":
|
|
300
|
+
return ["BOOL", "BOOL"];
|
|
282
301
|
case "DATETIME":
|
|
283
302
|
return ["DATETIME", "TIMESTAMP (3) WITH TIME ZONE"];
|
|
284
303
|
case "NUMBER":
|
|
@@ -300,18 +319,18 @@ export class PGDB extends DB {
|
|
|
300
319
|
const defaultValue = attribute.defaultValue === undefined ? (autoIncrement && fieldName === "id" ? `nextval('${tableName}_id_seq'::regclass)` : undefined) : this.escape(attribute.defaultValue);
|
|
301
320
|
const [base, type] = this.fieldType(attribute);
|
|
302
321
|
const where = "attrelid = $1 AND attnum > 0 AND atttypid = pg_type.oid AND attislocal = 't' AND attname = $2";
|
|
303
|
-
const res = await this.
|
|
322
|
+
const res = await this._client.query(`SELECT attnotnull, atttypmod, typname, ${adsrc(this.version)} FROM pg_type, pg_attribute LEFT JOIN pg_attrdef ON adrelid = attrelid AND adnum = attnum WHERE ${where}`, [oid, fieldName]);
|
|
304
323
|
const addField = async () => {
|
|
305
324
|
const statement = `ALTER TABLE ${tableName} ADD COLUMN ${fieldName} ${type}`;
|
|
306
325
|
this.syncLog(statement);
|
|
307
326
|
if (this.sync)
|
|
308
|
-
await this.
|
|
327
|
+
await this._client.query(statement);
|
|
309
328
|
};
|
|
310
329
|
const dropDefault = async () => {
|
|
311
330
|
const statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} DROP DEFAULT`;
|
|
312
331
|
this.syncLog(statement);
|
|
313
332
|
if (this.sync)
|
|
314
|
-
await this.
|
|
333
|
+
await this._client.query(statement);
|
|
315
334
|
};
|
|
316
335
|
const setNotNull = async (isNotNull) => {
|
|
317
336
|
if (isNotNull === notNull)
|
|
@@ -319,19 +338,19 @@ export class PGDB extends DB {
|
|
|
319
338
|
const statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} ${notNull ? "SET" : "DROP"} NOT NULL`;
|
|
320
339
|
this.syncLog(statement);
|
|
321
340
|
if (this.sync)
|
|
322
|
-
await this.
|
|
341
|
+
await this._client.query(statement);
|
|
323
342
|
};
|
|
324
343
|
const setDefault = async (isNotNull, create) => {
|
|
325
344
|
if (defaultValue !== undefined) {
|
|
326
345
|
let statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} SET DEFAULT ${defaultValue}`;
|
|
327
346
|
this.syncLog(statement);
|
|
328
347
|
if (this.sync)
|
|
329
|
-
await this.
|
|
348
|
+
await this._client.query(statement);
|
|
330
349
|
if (!isNotNull && !create) {
|
|
331
350
|
statement = `UPDATE ${tableName} SET ${fieldName} = ${defaultValue} WHERE ${fieldName} IS NULL`;
|
|
332
351
|
this.syncLog(statement);
|
|
333
352
|
if (this.sync)
|
|
334
|
-
this.
|
|
353
|
+
this._client.query(statement);
|
|
335
354
|
}
|
|
336
355
|
}
|
|
337
356
|
await setNotNull(isNotNull);
|
|
@@ -355,7 +374,7 @@ export class PGDB extends DB {
|
|
|
355
374
|
const statement = `ALTER TABLE ${tableName} ALTER COLUMN ${fieldName} TYPE ${type}${using}`;
|
|
356
375
|
this.syncLog(statement);
|
|
357
376
|
if (this.sync)
|
|
358
|
-
await this.
|
|
377
|
+
await this._client.query(statement);
|
|
359
378
|
await setDefault(attnotnull, false);
|
|
360
379
|
}
|
|
361
380
|
}
|
|
@@ -377,7 +396,7 @@ export class PGDB extends DB {
|
|
|
377
396
|
const statement = `CREATE${unique ? " UNIQUE" : ""} INDEX ${indexName} ON ${tableName} USING ${type} (${fields.join(", ")})`;
|
|
378
397
|
this.syncLog(statement);
|
|
379
398
|
if (this.sync)
|
|
380
|
-
await this.
|
|
399
|
+
await this._client.query(statement);
|
|
381
400
|
}
|
|
382
401
|
}
|
|
383
402
|
}
|
|
@@ -387,13 +406,13 @@ export class PGDB extends DB {
|
|
|
387
406
|
const statement = `ALTER SEQUENCE ${table.tableName}_id_seq OWNED BY ${table.tableName}.id`;
|
|
388
407
|
this.syncLog(statement);
|
|
389
408
|
if (this.sync)
|
|
390
|
-
await this.
|
|
409
|
+
await this._client.query(statement);
|
|
391
410
|
}
|
|
392
411
|
async syncTable(table) {
|
|
393
412
|
if (table.autoIncrement) {
|
|
394
413
|
await (async () => {
|
|
395
414
|
try {
|
|
396
|
-
await this.
|
|
415
|
+
await this._client.query(`SELECT currval('${table.tableName}_id_seq')`);
|
|
397
416
|
}
|
|
398
417
|
catch (e) {
|
|
399
418
|
if (e instanceof DatabaseError && e.code === "55000")
|
|
@@ -402,7 +421,7 @@ export class PGDB extends DB {
|
|
|
402
421
|
const statement = `CREATE SEQUENCE ${table.tableName}_id_seq`;
|
|
403
422
|
this.syncLog(statement);
|
|
404
423
|
if (this.sync)
|
|
405
|
-
await this.
|
|
424
|
+
await this._client.query(statement);
|
|
406
425
|
table.autoIncrementOwn = true;
|
|
407
426
|
return;
|
|
408
427
|
}
|
|
@@ -411,11 +430,11 @@ export class PGDB extends DB {
|
|
|
411
430
|
})();
|
|
412
431
|
}
|
|
413
432
|
let create = false;
|
|
414
|
-
const resTable = await this.
|
|
433
|
+
const resTable = await this._client.query("SELECT oid FROM pg_class WHERE relname = $1", [table.tableName]);
|
|
415
434
|
if (resTable.rowCount) {
|
|
416
435
|
table.oid = resTable.rows[0].oid;
|
|
417
436
|
let drop = false;
|
|
418
|
-
const resParent = await this.
|
|
437
|
+
const resParent = await this._client.query("SELECT inhparent FROM pg_inherits WHERE inhrelid = $1", [table.oid]);
|
|
419
438
|
if (resParent.rowCount) {
|
|
420
439
|
if (!table.parent)
|
|
421
440
|
drop = true;
|
|
@@ -430,7 +449,7 @@ export class PGDB extends DB {
|
|
|
430
449
|
create = true;
|
|
431
450
|
this.syncLog(statement);
|
|
432
451
|
if (this.sync)
|
|
433
|
-
await this.
|
|
452
|
+
await this._client.query(statement);
|
|
434
453
|
}
|
|
435
454
|
}
|
|
436
455
|
else
|
|
@@ -440,27 +459,30 @@ export class PGDB extends DB {
|
|
|
440
459
|
const statement = `CREATE TABLE ${table.tableName} ()${parent}`;
|
|
441
460
|
this.syncLog(statement);
|
|
442
461
|
if (this.sync)
|
|
443
|
-
await this.
|
|
444
|
-
const resTable = await this.
|
|
462
|
+
await this._client.query(statement);
|
|
463
|
+
const resTable = await this._client.query("SELECT oid FROM pg_class WHERE relname = $1", [table.tableName]);
|
|
445
464
|
table.oid = resTable.rows[0]?.oid;
|
|
446
465
|
}
|
|
447
466
|
}
|
|
448
467
|
}
|
|
449
468
|
export class TransactionPG extends Transaction {
|
|
450
|
-
|
|
469
|
+
_client;
|
|
451
470
|
released = false;
|
|
452
471
|
constructor(log, client) {
|
|
453
472
|
super(log);
|
|
454
|
-
this.
|
|
473
|
+
this._client = client;
|
|
474
|
+
}
|
|
475
|
+
async client() {
|
|
476
|
+
return this._client;
|
|
455
477
|
}
|
|
456
478
|
release() {
|
|
457
479
|
this.released = true;
|
|
458
|
-
this.
|
|
480
|
+
this._client.release();
|
|
459
481
|
}
|
|
460
482
|
async commit() {
|
|
461
483
|
if (!this.released) {
|
|
462
484
|
this.log("COMMIT");
|
|
463
|
-
await this.
|
|
485
|
+
await this._client.query("COMMIT");
|
|
464
486
|
this.release();
|
|
465
487
|
super.commit();
|
|
466
488
|
}
|
|
@@ -470,7 +492,7 @@ export class TransactionPG extends Transaction {
|
|
|
470
492
|
if (!this.released) {
|
|
471
493
|
super.rollback();
|
|
472
494
|
this.log("ROLLBACK");
|
|
473
|
-
await this.
|
|
495
|
+
await this._client.query("ROLLBACK");
|
|
474
496
|
}
|
|
475
497
|
}
|
|
476
498
|
finally {
|
package/dist/types/index.d.ts
CHANGED
|
@@ -7,5 +7,6 @@ export declare class SedentaryPG extends Sedentary<PGDB, TransactionPG> {
|
|
|
7
7
|
constructor(connection: PoolConfig, options?: SedentaryOptions);
|
|
8
8
|
FKEY<N extends Natural, E extends EntryBase>(attribute: Attribute<N, E>, options?: ForeignKeyOptions): Type<N, E>;
|
|
9
9
|
begin(): Promise<TransactionPG>;
|
|
10
|
+
client(): Promise<import("pg").PoolClient>;
|
|
10
11
|
}
|
|
11
12
|
export declare const Package: typeof SedentaryPG;
|
package/dist/types/pgdb.d.ts
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
import { PoolClient, PoolConfig } from "pg";
|
|
2
2
|
import { Attribute, DB, EntryBase, Natural, Table, Transaction } from "sedentary";
|
|
3
3
|
export declare class PGDB extends DB<TransactionPG> {
|
|
4
|
-
private
|
|
4
|
+
private _client;
|
|
5
5
|
private indexes;
|
|
6
6
|
private oidLoad;
|
|
7
7
|
private pool;
|
|
8
|
+
private released;
|
|
8
9
|
private version;
|
|
9
10
|
constructor(connection: PoolConfig, log: (message: string) => void);
|
|
10
11
|
connect(): Promise<void>;
|
|
11
12
|
end(): Promise<void>;
|
|
12
13
|
defaultNeq(src: string, value: Natural): boolean;
|
|
13
14
|
begin(): Promise<TransactionPG>;
|
|
15
|
+
client(): Promise<PoolClient>;
|
|
14
16
|
escape(value: Natural): string;
|
|
15
17
|
fill(attributes: Record<string, string>, row: Record<string, Natural>, entry: Record<string, Natural>): void;
|
|
16
18
|
load(tableName: string, attributes: Record<string, string>, pk: Attribute<Natural, unknown>, model: new (from: "load") => EntryBase, table: Table): (where: string, order?: string[], tx?: Transaction) => Promise<EntryBase[]>;
|
|
@@ -34,9 +36,10 @@ export declare class PGDB extends DB<TransactionPG> {
|
|
|
34
36
|
syncTable(table: Table): Promise<void>;
|
|
35
37
|
}
|
|
36
38
|
export declare class TransactionPG extends Transaction {
|
|
37
|
-
|
|
38
|
-
released
|
|
39
|
+
private _client;
|
|
40
|
+
private released;
|
|
39
41
|
constructor(log: (message: string) => void, client: PoolClient);
|
|
42
|
+
client(): Promise<PoolClient>;
|
|
40
43
|
private release;
|
|
41
44
|
commit(): Promise<void>;
|
|
42
45
|
rollback(): Promise<void>;
|
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"@types/pg": "8.6.5",
|
|
10
10
|
"pg": "8.7.3",
|
|
11
11
|
"pg-format": "1.0.4",
|
|
12
|
-
"sedentary": "0.0.
|
|
12
|
+
"sedentary": "0.0.37"
|
|
13
13
|
},
|
|
14
14
|
"description": "The ORM which never needs to migrate - PostgreSQL",
|
|
15
15
|
"devDependencies": {
|
|
@@ -71,7 +71,7 @@
|
|
|
71
71
|
"compilerOptions": {
|
|
72
72
|
"alwaysStrict": true,
|
|
73
73
|
"esModuleInterop": true,
|
|
74
|
-
"moduleResolution": "
|
|
74
|
+
"moduleResolution": "Node",
|
|
75
75
|
"noImplicitAny": true,
|
|
76
76
|
"noImplicitReturns": true,
|
|
77
77
|
"noImplicitThis": true,
|
|
@@ -80,9 +80,9 @@
|
|
|
80
80
|
"strictFunctionTypes": true,
|
|
81
81
|
"strictNullChecks": true,
|
|
82
82
|
"strictPropertyInitialization": true,
|
|
83
|
-
"target": "
|
|
83
|
+
"target": "ESNext"
|
|
84
84
|
}
|
|
85
85
|
},
|
|
86
86
|
"types": "./dist/types/index.d.ts",
|
|
87
|
-
"version": "0.0.
|
|
87
|
+
"version": "0.0.37"
|
|
88
88
|
}
|