sedentary-pg 0.1.2 → 0.1.3

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/pgdb.js CHANGED
@@ -82,14 +82,16 @@ class PGDB extends sedentary_1.DB {
82
82
  return async (where, tx) => {
83
83
  const client = tx ? tx._client : await this.pool.connect();
84
84
  let rowCount = 0;
85
+ let withError = true;
85
86
  try {
86
87
  const query = `DELETE FROM ${tableName}${where ? ` WHERE ${where}` : ""}`;
87
88
  this.log(query);
88
89
  ({ rowCount } = (await client.query(query)));
90
+ withError = false;
89
91
  }
90
92
  finally {
91
93
  if (!tx)
92
- client.release();
94
+ client.release(withError);
93
95
  }
94
96
  return rowCount;
95
97
  };
@@ -119,6 +121,7 @@ class PGDB extends sedentary_1.DB {
119
121
  const ret = [];
120
122
  const client = tx ? tx._client : await this.pool.connect();
121
123
  const oidPK = {};
124
+ let withError = true;
122
125
  try {
123
126
  const forUpdate = lock ? " FOR UPDATE" : "";
124
127
  const orderBy = order?.length
@@ -138,6 +141,7 @@ class PGDB extends sedentary_1.DB {
138
141
  tx.addEntry(entry);
139
142
  ret.push(entry);
140
143
  entry.postLoad();
144
+ withError = false;
141
145
  }
142
146
  else {
143
147
  if (!oidPK[row.tableoid])
@@ -156,7 +160,7 @@ class PGDB extends sedentary_1.DB {
156
160
  }
157
161
  finally {
158
162
  if (!tx)
159
- client.release();
163
+ client.release(withError);
160
164
  }
161
165
  return ret;
162
166
  };
@@ -169,14 +173,16 @@ class PGDB extends sedentary_1.DB {
169
173
  return async function () {
170
174
  const client = this[sedentary_1.transaction] ? this[sedentary_1.transaction]._client : await self.pool.connect();
171
175
  let removed;
176
+ let withError = true;
172
177
  try {
173
178
  const query = `DELETE FROM ${tableName} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`;
174
179
  self.log(query);
175
180
  removed = (await client.query(query)).rowCount;
181
+ withError = false;
176
182
  }
177
183
  finally {
178
184
  if (!this[sedentary_1.transaction])
179
- client.release();
185
+ client.release(withError);
180
186
  }
181
187
  return removed;
182
188
  };
@@ -190,6 +196,7 @@ class PGDB extends sedentary_1.DB {
190
196
  const client = this[sedentary_1.transaction] ? this[sedentary_1.transaction]._client : await self.pool.connect();
191
197
  let changed = false;
192
198
  let result = null;
199
+ let withError = true;
193
200
  const save = async (query) => {
194
201
  self.log(query);
195
202
  changed = true;
@@ -220,10 +227,11 @@ class PGDB extends sedentary_1.DB {
220
227
  }
221
228
  await save(fields.length ? `INSERT INTO ${tableName} (${fields.join(", ")}) VALUES (${values.join(", ")})` : `INSERT INTO ${tableName} DEFAULT VALUES`);
222
229
  }
230
+ withError = false;
223
231
  }
224
232
  finally {
225
233
  if (!this[sedentary_1.transaction])
226
- client.release();
234
+ client.release(withError);
227
235
  }
228
236
  return changed && result.rowCount;
229
237
  };
@@ -319,14 +327,16 @@ class PGDB extends sedentary_1.DB {
319
327
  }
320
328
  }
321
329
  async syncDataBase() {
330
+ let withError = true;
322
331
  try {
323
332
  await super.syncDataBase();
324
333
  for (const table of this.tables)
325
334
  this.oidLoad[table.oid || 0] = (ids) => table.model.load({ [table.pk.attributeName]: ["IN", ids] });
335
+ withError = false;
326
336
  }
327
337
  finally {
328
338
  this.released = true;
329
- this._client.release();
339
+ this._client.release(withError);
330
340
  }
331
341
  }
332
342
  fieldType(attribute) {
@@ -523,30 +533,35 @@ class TransactionPG extends sedentary_1.Transaction {
523
533
  client() {
524
534
  return Promise.resolve(this._client);
525
535
  }
526
- release() {
527
- this.released = true;
528
- this._client.release();
536
+ release(error = false) {
537
+ if (!this.released) {
538
+ this.released = true;
539
+ this._client.release(error);
540
+ }
529
541
  }
530
542
  async commit() {
531
- if (!this.released) {
543
+ try {
532
544
  this.preCommit();
533
545
  this.log("COMMIT");
534
546
  await this._client.query("COMMIT");
535
547
  this.release();
536
548
  await super.commit();
537
549
  }
550
+ catch (error) {
551
+ this.release(true);
552
+ throw error;
553
+ }
538
554
  }
539
555
  async rollback() {
540
556
  try {
541
- if (!this.released) {
542
- await super.rollback();
543
- this.log("ROLLBACK");
544
- await this._client.query("ROLLBACK");
545
- }
557
+ await super.rollback();
558
+ this.log("ROLLBACK");
559
+ await this._client.query("ROLLBACK");
560
+ this.release();
546
561
  }
547
- finally {
548
- if (!this.released)
549
- this.release();
562
+ catch (error) {
563
+ this.release(true);
564
+ throw error;
550
565
  }
551
566
  }
552
567
  }
package/dist/es/pgdb.js CHANGED
@@ -77,14 +77,16 @@ export class PGDB extends DB {
77
77
  return async (where, tx) => {
78
78
  const client = tx ? tx._client : await this.pool.connect();
79
79
  let rowCount = 0;
80
+ let withError = true;
80
81
  try {
81
82
  const query = `DELETE FROM ${tableName}${where ? ` WHERE ${where}` : ""}`;
82
83
  this.log(query);
83
84
  ({ rowCount } = (await client.query(query)));
85
+ withError = false;
84
86
  }
85
87
  finally {
86
88
  if (!tx)
87
- client.release();
89
+ client.release(withError);
88
90
  }
89
91
  return rowCount;
90
92
  };
@@ -114,6 +116,7 @@ export class PGDB extends DB {
114
116
  const ret = [];
115
117
  const client = tx ? tx._client : await this.pool.connect();
116
118
  const oidPK = {};
119
+ let withError = true;
117
120
  try {
118
121
  const forUpdate = lock ? " FOR UPDATE" : "";
119
122
  const orderBy = order?.length
@@ -133,6 +136,7 @@ export class PGDB extends DB {
133
136
  tx.addEntry(entry);
134
137
  ret.push(entry);
135
138
  entry.postLoad();
139
+ withError = false;
136
140
  }
137
141
  else {
138
142
  if (!oidPK[row.tableoid])
@@ -151,7 +155,7 @@ export class PGDB extends DB {
151
155
  }
152
156
  finally {
153
157
  if (!tx)
154
- client.release();
158
+ client.release(withError);
155
159
  }
156
160
  return ret;
157
161
  };
@@ -164,14 +168,16 @@ export class PGDB extends DB {
164
168
  return async function () {
165
169
  const client = this[transaction] ? this[transaction]._client : await self.pool.connect();
166
170
  let removed;
171
+ let withError = true;
167
172
  try {
168
173
  const query = `DELETE FROM ${tableName} WHERE ${pkFldName} = ${self.escape(this[pkAttrName])}`;
169
174
  self.log(query);
170
175
  removed = (await client.query(query)).rowCount;
176
+ withError = false;
171
177
  }
172
178
  finally {
173
179
  if (!this[transaction])
174
- client.release();
180
+ client.release(withError);
175
181
  }
176
182
  return removed;
177
183
  };
@@ -185,6 +191,7 @@ export class PGDB extends DB {
185
191
  const client = this[transaction] ? this[transaction]._client : await self.pool.connect();
186
192
  let changed = false;
187
193
  let result = null;
194
+ let withError = true;
188
195
  const save = async (query) => {
189
196
  self.log(query);
190
197
  changed = true;
@@ -215,10 +222,11 @@ export class PGDB extends DB {
215
222
  }
216
223
  await save(fields.length ? `INSERT INTO ${tableName} (${fields.join(", ")}) VALUES (${values.join(", ")})` : `INSERT INTO ${tableName} DEFAULT VALUES`);
217
224
  }
225
+ withError = false;
218
226
  }
219
227
  finally {
220
228
  if (!this[transaction])
221
- client.release();
229
+ client.release(withError);
222
230
  }
223
231
  return changed && result.rowCount;
224
232
  };
@@ -314,14 +322,16 @@ export class PGDB extends DB {
314
322
  }
315
323
  }
316
324
  async syncDataBase() {
325
+ let withError = true;
317
326
  try {
318
327
  await super.syncDataBase();
319
328
  for (const table of this.tables)
320
329
  this.oidLoad[table.oid || 0] = (ids) => table.model.load({ [table.pk.attributeName]: ["IN", ids] });
330
+ withError = false;
321
331
  }
322
332
  finally {
323
333
  this.released = true;
324
- this._client.release();
334
+ this._client.release(withError);
325
335
  }
326
336
  }
327
337
  fieldType(attribute) {
@@ -518,30 +528,35 @@ export class TransactionPG extends Transaction {
518
528
  client() {
519
529
  return Promise.resolve(this._client);
520
530
  }
521
- release() {
522
- this.released = true;
523
- this._client.release();
531
+ release(error = false) {
532
+ if (!this.released) {
533
+ this.released = true;
534
+ this._client.release(error);
535
+ }
524
536
  }
525
537
  async commit() {
526
- if (!this.released) {
538
+ try {
527
539
  this.preCommit();
528
540
  this.log("COMMIT");
529
541
  await this._client.query("COMMIT");
530
542
  this.release();
531
543
  await super.commit();
532
544
  }
545
+ catch (error) {
546
+ this.release(true);
547
+ throw error;
548
+ }
533
549
  }
534
550
  async rollback() {
535
551
  try {
536
- if (!this.released) {
537
- await super.rollback();
538
- this.log("ROLLBACK");
539
- await this._client.query("ROLLBACK");
540
- }
552
+ await super.rollback();
553
+ this.log("ROLLBACK");
554
+ await this._client.query("ROLLBACK");
555
+ this.release();
541
556
  }
542
- finally {
543
- if (!this.released)
544
- this.release();
557
+ catch (error) {
558
+ this.release(true);
559
+ throw error;
545
560
  }
546
561
  }
547
562
  }
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "@types/pg": "^8.16.0",
10
10
  "pg": "^8.18.0",
11
11
  "pg-format": "^1.0.4",
12
- "sedentary": "0.1.2"
12
+ "sedentary": "0.1.3"
13
13
  },
14
14
  "description": "The ORM which never needs to migrate - PostgreSQL",
15
15
  "engines": {
@@ -44,5 +44,5 @@
44
44
  "preinstall": "if [ -f Makefile ] ; then make ; fi"
45
45
  },
46
46
  "types": "./dist/types/index.d.ts",
47
- "version": "0.1.2"
47
+ "version": "0.1.3"
48
48
  }