postgrejs 2.22.1 → 2.22.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
+ # v2.22.2
2
+ [2024-11-04]
3
+
4
+ ### Changes
5
+
6
+ * refactor: Improved displaying error line ([`3a6bbe9`](https://github.com/panates/postgrejs/commit/3a6bbe929efdb4f46a38c7b9626b4f28739ad341))
7
+
1
8
  # v2.22.1
2
- [2024-10-15]
9
+ [2024-10-16]
3
10
 
4
11
  # v2.22.0
5
12
  [2024-10-15]
@@ -133,6 +133,7 @@ class Connection extends safe_event_emitter_js_1.SafeEventEmitter {
133
133
  * @param options {ScriptExecuteOptions} - Execute options
134
134
  */
135
135
  async execute(sql, options) {
136
+ this.emit('execute', sql, options);
136
137
  return this._captureErrorStack(this._intlCon.execute(sql, options)).catch((e) => {
137
138
  throw this._handleError(e, sql);
138
139
  });
@@ -148,6 +149,7 @@ class Connection extends safe_event_emitter_js_1.SafeEventEmitter {
148
149
  sql,
149
150
  });
150
151
  }
152
+ this.emit('query', sql, options);
151
153
  const typeMap = options?.typeMap || data_type_map_js_1.GlobalTypeMap;
152
154
  const paramTypes = options?.params?.map(prm => prm instanceof bind_param_js_1.BindParam ? prm.oid : typeMap.determine(prm));
153
155
  const statement = await this.prepare(sql, { paramTypes, typeMap }).catch((e) => {
@@ -251,17 +253,16 @@ class Connection extends safe_event_emitter_js_1.SafeEventEmitter {
251
253
  this._closing = false;
252
254
  }
253
255
  _handleError(err, script) {
254
- if (err.position) {
255
- const i1 = script.lastIndexOf('\n', err.position) + 1;
256
- let i2 = script.indexOf('\n', err.position);
257
- if (i2 < 0)
258
- i2 = Number.MAX_SAFE_INTEGER;
259
- err.line = script.substring(i1, i2);
260
- err.lineNr = [...script.substring(0, i1).matchAll(/\n/g)].length;
256
+ if (err.position != null) {
257
+ const i1 = script.lastIndexOf('\n', err.position - 1) + 1;
258
+ err.lineNr = [...script.substring(0, i1).matchAll(/\n/g)].length + 1;
261
259
  err.colNr = err.position - i1;
262
- err.message +=
263
- `\nAt line ${err.lineNr} column ${err.colNr}` +
264
- `\n | ${err.line}\n | ${' '.repeat(Math.max(err.colNr - 2, 0))}-^-`;
260
+ const lines = script.split('\n');
261
+ err.line = lines[err.lineNr - 1];
262
+ err.message += `\n at line ${err.lineNr} column ${err.colNr}`;
263
+ if (err.lineNr > 1)
264
+ err.message += `\n${String(err.lineNr - 1).padStart(3)}| ${lines[err.lineNr - 2]}`;
265
+ err.message += `\n${String(err.lineNr).padStart(3)}| ${err.line}\n .${'-'.repeat(Math.max(err.colNr - 1, 0))}^`;
265
266
  }
266
267
  return err;
267
268
  }
@@ -135,6 +135,10 @@ class Pool extends safe_event_emitter_js_1.SafeEventEmitter {
135
135
  /* istanbul ignore next */
136
136
  if (this.listenerCount('debug'))
137
137
  connection.on('debug', (...args) => this.emit('debug', ...args));
138
+ if (this.listenerCount('execute'))
139
+ connection.on('execute', (...args) => this.emit('execute', ...args));
140
+ if (this.listenerCount('query'))
141
+ connection.on('query', (...args) => this.emit('query', ...args));
138
142
  return connection;
139
143
  }
140
144
  /**
@@ -130,6 +130,7 @@ export class Connection extends SafeEventEmitter {
130
130
  * @param options {ScriptExecuteOptions} - Execute options
131
131
  */
132
132
  async execute(sql, options) {
133
+ this.emit('execute', sql, options);
133
134
  return this._captureErrorStack(this._intlCon.execute(sql, options)).catch((e) => {
134
135
  throw this._handleError(e, sql);
135
136
  });
@@ -145,6 +146,7 @@ export class Connection extends SafeEventEmitter {
145
146
  sql,
146
147
  });
147
148
  }
149
+ this.emit('query', sql, options);
148
150
  const typeMap = options?.typeMap || GlobalTypeMap;
149
151
  const paramTypes = options?.params?.map(prm => prm instanceof BindParam ? prm.oid : typeMap.determine(prm));
150
152
  const statement = await this.prepare(sql, { paramTypes, typeMap }).catch((e) => {
@@ -248,17 +250,16 @@ export class Connection extends SafeEventEmitter {
248
250
  this._closing = false;
249
251
  }
250
252
  _handleError(err, script) {
251
- if (err.position) {
252
- const i1 = script.lastIndexOf('\n', err.position) + 1;
253
- let i2 = script.indexOf('\n', err.position);
254
- if (i2 < 0)
255
- i2 = Number.MAX_SAFE_INTEGER;
256
- err.line = script.substring(i1, i2);
257
- err.lineNr = [...script.substring(0, i1).matchAll(/\n/g)].length;
253
+ if (err.position != null) {
254
+ const i1 = script.lastIndexOf('\n', err.position - 1) + 1;
255
+ err.lineNr = [...script.substring(0, i1).matchAll(/\n/g)].length + 1;
258
256
  err.colNr = err.position - i1;
259
- err.message +=
260
- `\nAt line ${err.lineNr} column ${err.colNr}` +
261
- `\n | ${err.line}\n | ${' '.repeat(Math.max(err.colNr - 2, 0))}-^-`;
257
+ const lines = script.split('\n');
258
+ err.line = lines[err.lineNr - 1];
259
+ err.message += `\n at line ${err.lineNr} column ${err.colNr}`;
260
+ if (err.lineNr > 1)
261
+ err.message += `\n${String(err.lineNr - 1).padStart(3)}| ${lines[err.lineNr - 2]}`;
262
+ err.message += `\n${String(err.lineNr).padStart(3)}| ${err.line}\n .${'-'.repeat(Math.max(err.colNr - 1, 0))}^`;
262
263
  }
263
264
  return err;
264
265
  }
@@ -132,6 +132,10 @@ export class Pool extends SafeEventEmitter {
132
132
  /* istanbul ignore next */
133
133
  if (this.listenerCount('debug'))
134
134
  connection.on('debug', (...args) => this.emit('debug', ...args));
135
+ if (this.listenerCount('execute'))
136
+ connection.on('execute', (...args) => this.emit('execute', ...args));
137
+ if (this.listenerCount('query'))
138
+ connection.on('query', (...args) => this.emit('query', ...args));
135
139
  return connection;
136
140
  }
137
141
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "postgrejs",
3
3
  "description": "Professional PostgreSQL client NodeJS",
4
- "version": "2.22.1",
4
+ "version": "2.22.2",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
7
7
  "dependencies": {
@@ -12,7 +12,7 @@
12
12
  "putil-merge": "^3.13.0",
13
13
  "putil-promisify": "^1.10.1",
14
14
  "putil-varhelpers": "^1.6.5",
15
- "tslib": "^2.8.0"
15
+ "tslib": "^2.8.1"
16
16
  },
17
17
  "type": "module",
18
18
  "exports": {