sedentary-pg 0.0.35 → 0.0.38

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