@trenskow/pged 4.1.2 → 4.1.5

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.
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "cSpell.words": [
3
3
  "caseit",
4
+ "pged",
4
5
  "trenskow"
5
6
  ]
6
- }
7
+ }
@@ -0,0 +1,43 @@
1
+ 'use strict';
2
+
3
+ module.exports = exports = class EventEmitter {
4
+
5
+ constructor() {
6
+ this._identifier = -1;
7
+ this._listeners = {};
8
+ }
9
+
10
+ on(name, handler) {
11
+ this._listeners[name] = this._listeners[name] || [];
12
+ this._listeners[name].push({
13
+ handler,
14
+ identifier: ++this._identifier
15
+ });
16
+ return this._identifier;
17
+ }
18
+
19
+ remove(name, identifier) {
20
+ if (typeof this._listeners[name] === 'undefined') return;
21
+ this._listeners[name] = this._listeners[name].filter((listener) => {
22
+ return listener.handler !== identifier && listener.identifier !== identifier;
23
+ });
24
+ }
25
+
26
+ once(name, handler) {
27
+ const identifier = this.on(name, async (...args) => {
28
+ this.remove(name, identifier);
29
+ await handler(...args);
30
+ });
31
+ }
32
+
33
+ removeAll(name) {
34
+ this._identifier[name] = undefined;
35
+ }
36
+
37
+ async emit(name, ...args) {
38
+ await Promise.all((this._listeners[name] || []).map(async (listener) => {
39
+ await listener.handler(...args);
40
+ }));
41
+ }
42
+
43
+ };
package/index.js CHANGED
@@ -6,14 +6,15 @@ const
6
6
  { Pool } = require('pg');
7
7
 
8
8
  const
9
- QueryBuilder = require('./query-builder');
9
+ QueryBuilder = require('./query-builder'),
10
+ EventEmitter = require('./event-emitter');
10
11
 
11
12
  let id = 0;
12
13
 
13
14
  let pgOptions;
14
15
  let pool;
15
16
 
16
- module.exports = exports = class PGed {
17
+ module.exports = exports = class PGed extends EventEmitter {
17
18
 
18
19
  static get pg() {
19
20
  return pgOptions;
@@ -30,6 +31,8 @@ module.exports = exports = class PGed {
30
31
 
31
32
  constructor(options = {}) {
32
33
 
34
+ super();
35
+
33
36
  pool = pool || new Pool(pgOptions);
34
37
 
35
38
  options.casing = options.casing || {};
@@ -105,6 +108,7 @@ module.exports = exports = class PGed {
105
108
  this._connectionCount++;
106
109
  if (this._connectionCount == 1) {
107
110
  this._client = await pool.connect();
111
+ await this.emit('connected');
108
112
  }
109
113
  }
110
114
 
@@ -113,6 +117,7 @@ module.exports = exports = class PGed {
113
117
  if (this._connectionCount == 0) {
114
118
  await this._client.release();
115
119
  this._client = undefined;
120
+ await this.emit('disconnected');
116
121
  }
117
122
  }
118
123
 
@@ -151,6 +156,7 @@ module.exports = exports = class PGed {
151
156
  await this.set.transactionMode[this._transactions.mode]();
152
157
  }
153
158
  await this._query('begin;');
159
+ await this.emit('startedTransaction');
154
160
  }
155
161
  });
156
162
  }
@@ -160,7 +166,9 @@ module.exports = exports = class PGed {
160
166
  opt.rethrow = opt.rethrow !== false;
161
167
  this._transactions.count--;
162
168
  if (this._transactions.count == 0) {
163
- await this._query(err || !this._commit ? 'rollback;' : 'commit;');
169
+ const shouldCommit = typeof err === 'undefined' && this._commit;
170
+ await this._query(shouldCommit ? 'commit;' : 'rollback;');
171
+ await this.emit('endedTransaction', err, shouldCommit);
164
172
  }
165
173
  await this._release();
166
174
  if (err && opt.rethrow) throw err;
@@ -212,8 +220,14 @@ module.exports = exports = class PGed {
212
220
 
213
221
  const todo = async () => result = this._convertResult(await this._query(query, parameters), options);
214
222
 
215
- if (this._transactions.always || options.transaction) await this.transaction(todo);
216
- else await this.retained(todo);
223
+ await this.emit('preQuery', query, parameters);
224
+
225
+ try {
226
+ if (this._transactions.always || options.transaction) await this.transaction(todo);
227
+ else await this.retained(todo);
228
+ } finally {
229
+ await this.emit('query', query, parameters);
230
+ }
217
231
 
218
232
  if (options.first === true) {
219
233
  return (result || [])[0];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/pged",
3
- "version": "4.1.2",
3
+ "version": "4.1.5",
4
4
  "description": "Just a silly little db management and query builder for PostgreSQL.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -25,6 +25,6 @@
25
25
  "@trenskow/caseit": "^1.1.4",
26
26
  "@trenskow/custom-promise": "^0.10.1",
27
27
  "pg": "^8.7.3",
28
- "puqeue": "^1.0.5"
28
+ "puqeue": "^1.0.6"
29
29
  }
30
30
  }