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