postgrejs 2.16.0 → 2.17.0
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 +8 -0
- package/esm/connection/bind-param.js +1 -5
- package/esm/connection/connection.js +16 -20
- package/esm/connection/cursor.js +10 -15
- package/esm/connection/intl-connection.js +41 -46
- package/esm/connection/pool.js +28 -32
- package/esm/connection/portal.js +27 -31
- package/esm/connection/prepared-statement.js +31 -35
- package/esm/constants.js +122 -125
- package/esm/data-type-map.js +71 -75
- package/esm/data-types/bool-type.js +7 -10
- package/esm/data-types/box-type.js +7 -10
- package/esm/data-types/bytea-type.js +9 -13
- package/esm/data-types/char-type.js +7 -10
- package/esm/data-types/circle-type.js +7 -10
- package/esm/data-types/date-type.js +12 -15
- package/esm/data-types/float4-type.js +7 -10
- package/esm/data-types/float8-type.js +7 -10
- package/esm/data-types/int2-type.js +10 -13
- package/esm/data-types/int2-vector-type.js +13 -16
- package/esm/data-types/int4-type.js +10 -13
- package/esm/data-types/int8-type.js +9 -12
- package/esm/data-types/json-type.js +9 -12
- package/esm/data-types/jsonb-type.js +11 -14
- package/esm/data-types/lseg-type.js +7 -10
- package/esm/data-types/numeric-type.js +8 -12
- package/esm/data-types/oid-type.js +15 -18
- package/esm/data-types/point-type.js +7 -10
- package/esm/data-types/time-type.js +13 -16
- package/esm/data-types/timestamp-type.js +12 -15
- package/esm/data-types/timestamptz-type.js +12 -15
- package/esm/data-types/uuid-type.js +7 -10
- package/esm/data-types/varchar-type.js +7 -10
- package/esm/index.js +25 -28
- package/esm/interfaces/command-result.js +1 -2
- package/esm/interfaces/data-mapping-options.js +1 -2
- package/esm/interfaces/data-type.js +1 -2
- package/esm/interfaces/database-connection-params.js +1 -2
- package/esm/interfaces/field-info.js +1 -2
- package/esm/interfaces/query-options.js +1 -2
- package/esm/interfaces/query-result.js +1 -2
- package/esm/interfaces/script-execute-options.js +1 -2
- package/esm/interfaces/script-result.js +1 -2
- package/esm/interfaces/statement-prepare-options.js +1 -2
- package/esm/protocol/backend.js +24 -28
- package/esm/protocol/buffer-reader.js +1 -5
- package/esm/protocol/database-error.js +1 -5
- package/esm/protocol/frontend.js +27 -31
- package/esm/protocol/pg-socket.js +48 -53
- package/esm/protocol/protocol.js +2 -5
- package/esm/protocol/sasl.js +7 -11
- package/esm/protocol/smart-buffer.js +5 -10
- package/esm/safe-event-emitter.js +2 -6
- package/esm/types.js +1 -2
- package/esm/util/array-calculatedim.js +1 -4
- package/esm/util/bigint-methods.js +2 -6
- package/esm/util/config-from-env.js +5 -8
- package/esm/util/connection-config.js +8 -13
- package/esm/util/convert-row-to-object.js +1 -4
- package/esm/util/decode-binaryarray.js +3 -6
- package/esm/util/encode-binaryarray.js +5 -8
- package/esm/util/escape-literal.js +1 -4
- package/esm/util/fast-parseint.js +1 -4
- package/esm/util/get-parsers.js +8 -11
- package/esm/util/parse-array.js +1 -4
- package/esm/util/parse-datetime.js +5 -8
- package/esm/util/parse-row.js +1 -4
- package/esm/util/parse-time.js +8 -12
- package/esm/util/stringify-arrayliteral.js +3 -6
- package/esm/util/stringify-for-sql.js +8 -12
- package/esm/util/wrap-row-description.js +6 -9
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
# v2.17.0
|
|
2
|
+
[2024-08-11]
|
|
3
|
+
|
|
4
|
+
### Changes
|
|
5
|
+
|
|
6
|
+
* Rollback to ES2020 ([`279aed6`](https://github.com/panates/postgrejs/commit/279aed60b05bf7785f3785df2d43ab8c194abab2))
|
|
7
|
+
* Rollback to ES2020 ([`1c0b78e`](https://github.com/panates/postgrejs/commit/1c0b78e6c0dd963f0e120031b3a5c2fe7a7b621c))
|
|
8
|
+
|
|
1
9
|
# v2.16.0
|
|
2
10
|
[2024-08-09]
|
|
3
11
|
|
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BindParam = void 0;
|
|
4
|
-
class BindParam {
|
|
1
|
+
export class BindParam {
|
|
5
2
|
constructor(oid, value) {
|
|
6
3
|
this.oid = oid;
|
|
7
4
|
this.value = value;
|
|
8
5
|
}
|
|
9
6
|
}
|
|
10
|
-
exports.BindParam = BindParam;
|
|
@@ -1,25 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const intl_connection_js_1 = require("./intl-connection.js");
|
|
9
|
-
const prepared_statement_js_1 = require("./prepared-statement.js");
|
|
10
|
-
class Connection extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
1
|
+
import { ConnectionState } from '../constants.js';
|
|
2
|
+
import { GlobalTypeMap } from '../data-type-map.js';
|
|
3
|
+
import { SafeEventEmitter } from '../safe-event-emitter.js';
|
|
4
|
+
import { BindParam } from './bind-param.js';
|
|
5
|
+
import { IntlConnection } from './intl-connection.js';
|
|
6
|
+
import { PreparedStatement } from './prepared-statement.js';
|
|
7
|
+
export class Connection extends SafeEventEmitter {
|
|
11
8
|
constructor(arg0, arg1) {
|
|
12
9
|
super();
|
|
13
|
-
this._notificationListeners = new
|
|
10
|
+
this._notificationListeners = new SafeEventEmitter();
|
|
14
11
|
this._closing = false;
|
|
15
12
|
if (arg0 && typeof arg0 === 'object' && typeof arg0.acquire === 'function') {
|
|
16
|
-
if (!(arg1 instanceof
|
|
13
|
+
if (!(arg1 instanceof IntlConnection))
|
|
17
14
|
throw new TypeError('Invalid argument');
|
|
18
15
|
this._pool = arg0;
|
|
19
16
|
this._intlCon = arg1;
|
|
20
17
|
}
|
|
21
18
|
else {
|
|
22
|
-
this._intlCon = new
|
|
19
|
+
this._intlCon = new IntlConnection(arg0);
|
|
23
20
|
}
|
|
24
21
|
this._intlCon.on('ready', (...args) => this.emit('ready', ...args));
|
|
25
22
|
this._intlCon.on('error', (...args) => this.emit('error', ...args));
|
|
@@ -70,7 +67,7 @@ class Connection extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
|
70
67
|
*/
|
|
71
68
|
async connect() {
|
|
72
69
|
await this._captureErrorStack(this._intlCon.connect());
|
|
73
|
-
if (this.state ===
|
|
70
|
+
if (this.state === ConnectionState.READY)
|
|
74
71
|
this._closing = false;
|
|
75
72
|
}
|
|
76
73
|
/**
|
|
@@ -83,7 +80,7 @@ class Connection extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
|
83
80
|
async close(terminateWait) {
|
|
84
81
|
this._notificationListeners.removeAllListeners();
|
|
85
82
|
this._intlCon.statementQueue.clearQueue();
|
|
86
|
-
if (this.state ===
|
|
83
|
+
if (this.state === ConnectionState.CLOSED || this._closing)
|
|
87
84
|
return;
|
|
88
85
|
/* istanbul ignore next */
|
|
89
86
|
if (this.listenerCount('debug')) {
|
|
@@ -148,13 +145,13 @@ class Connection extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
|
148
145
|
sql,
|
|
149
146
|
});
|
|
150
147
|
}
|
|
151
|
-
const typeMap = options?.typeMap ||
|
|
152
|
-
const paramTypes = options?.params?.map(prm => prm instanceof
|
|
148
|
+
const typeMap = options?.typeMap || GlobalTypeMap;
|
|
149
|
+
const paramTypes = options?.params?.map(prm => prm instanceof BindParam ? prm.oid : typeMap.determine(prm));
|
|
153
150
|
const statement = await this.prepare(sql, { paramTypes, typeMap }).catch((e) => {
|
|
154
151
|
throw this._handleError(e, sql);
|
|
155
152
|
});
|
|
156
153
|
try {
|
|
157
|
-
const params = options?.params?.map(prm => (prm instanceof
|
|
154
|
+
const params = options?.params?.map(prm => (prm instanceof BindParam ? prm.value : prm));
|
|
158
155
|
return await this._captureErrorStack(statement.execute({ ...options, params }));
|
|
159
156
|
}
|
|
160
157
|
finally {
|
|
@@ -176,7 +173,7 @@ class Connection extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
|
176
173
|
sql,
|
|
177
174
|
});
|
|
178
175
|
}
|
|
179
|
-
return await this._captureErrorStack(
|
|
176
|
+
return await this._captureErrorStack(PreparedStatement.prepare(this, sql, options));
|
|
180
177
|
}
|
|
181
178
|
/**
|
|
182
179
|
* Starts a transaction
|
|
@@ -281,4 +278,3 @@ class Connection extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
|
281
278
|
return this.close();
|
|
282
279
|
}
|
|
283
280
|
}
|
|
284
|
-
exports.Connection = Connection;
|
package/esm/connection/cursor.js
CHANGED
|
@@ -1,17 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const safe_event_emitter_js_1 = require("../safe-event-emitter.js");
|
|
8
|
-
const convert_row_to_object_js_1 = require("../util/convert-row-to-object.js");
|
|
9
|
-
const parse_row_js_1 = require("../util/parse-row.js");
|
|
10
|
-
class Cursor extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
1
|
+
import DoublyLinked from 'doublylinked';
|
|
2
|
+
import { TaskQueue } from 'power-tasks';
|
|
3
|
+
import { SafeEventEmitter } from '../safe-event-emitter.js';
|
|
4
|
+
import { convertRowToObject } from '../util/convert-row-to-object.js';
|
|
5
|
+
import { parseRow } from '../util/parse-row.js';
|
|
6
|
+
export class Cursor extends SafeEventEmitter {
|
|
11
7
|
constructor(statement, portal, fields, parsers, queryOptions) {
|
|
12
8
|
super();
|
|
13
|
-
this._taskQueue = new
|
|
14
|
-
this._rows = new
|
|
9
|
+
this._taskQueue = new TaskQueue({ concurrency: 1 });
|
|
10
|
+
this._rows = new DoublyLinked();
|
|
15
11
|
this._closed = false;
|
|
16
12
|
this._statement = statement;
|
|
17
13
|
this._portal = portal;
|
|
@@ -70,9 +66,9 @@ class Cursor extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
|
70
66
|
const rows = r.rows;
|
|
71
67
|
for (let i = 0; i < rows.length; i++) {
|
|
72
68
|
const row = rows[i];
|
|
73
|
-
|
|
69
|
+
parseRow(this._parsers, row, this._queryOptions);
|
|
74
70
|
if (objectRows)
|
|
75
|
-
rows[i] =
|
|
71
|
+
rows[i] = convertRowToObject(fields, row);
|
|
76
72
|
}
|
|
77
73
|
}
|
|
78
74
|
this._rows.push(...r.rows);
|
|
@@ -88,4 +84,3 @@ class Cursor extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
|
88
84
|
return this.close();
|
|
89
85
|
}
|
|
90
86
|
}
|
|
91
|
-
exports.Cursor = Cursor;
|
|
@@ -1,29 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
const parse_row_js_1 = require("../util/parse-row.js");
|
|
17
|
-
const wrap_row_description_js_1 = require("../util/wrap-row-description.js");
|
|
18
|
-
const DataFormat = protocol_js_1.Protocol.DataFormat;
|
|
19
|
-
class IntlConnection extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
1
|
+
import { TaskQueue } from 'power-tasks';
|
|
2
|
+
import { coerceToBoolean } from 'putil-varhelpers';
|
|
3
|
+
import { ConnectionState } from '../constants.js';
|
|
4
|
+
import { GlobalTypeMap } from '../data-type-map.js';
|
|
5
|
+
import { PgSocket } from '../protocol/pg-socket.js';
|
|
6
|
+
import { Protocol } from '../protocol/protocol.js';
|
|
7
|
+
import { SafeEventEmitter } from '../safe-event-emitter.js';
|
|
8
|
+
import { getConnectionConfig } from '../util/connection-config.js';
|
|
9
|
+
import { convertRowToObject } from '../util/convert-row-to-object.js';
|
|
10
|
+
import { escapeLiteral } from '../util/escape-literal.js';
|
|
11
|
+
import { getParsers } from '../util/get-parsers.js';
|
|
12
|
+
import { parseRow } from '../util/parse-row.js';
|
|
13
|
+
import { wrapRowDescription } from '../util/wrap-row-description.js';
|
|
14
|
+
const DataFormat = Protocol.DataFormat;
|
|
15
|
+
export class IntlConnection extends SafeEventEmitter {
|
|
20
16
|
constructor(config) {
|
|
21
17
|
super();
|
|
22
18
|
this._refCount = 0;
|
|
23
19
|
this.transactionStatus = 'I';
|
|
24
|
-
this.statementQueue = new
|
|
25
|
-
this._config = Object.freeze(
|
|
26
|
-
this.socket = new
|
|
20
|
+
this.statementQueue = new TaskQueue({ concurrency: 1 });
|
|
21
|
+
this._config = Object.freeze(getConnectionConfig(config));
|
|
22
|
+
this.socket = new PgSocket(this._config);
|
|
27
23
|
this.socket.on('error', err => this._onError(err));
|
|
28
24
|
this.socket.on('close', () => this.emit('close'));
|
|
29
25
|
this.socket.on('notification', payload => this.emit('notification', payload));
|
|
@@ -52,7 +48,7 @@ class IntlConnection extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
|
52
48
|
return this.socket.sessionParameters;
|
|
53
49
|
}
|
|
54
50
|
async connect() {
|
|
55
|
-
if (this.socket.state ===
|
|
51
|
+
if (this.socket.state === ConnectionState.READY)
|
|
56
52
|
return;
|
|
57
53
|
await new Promise((resolve, reject) => {
|
|
58
54
|
const handleConnectError = err => reject(err);
|
|
@@ -65,19 +61,19 @@ class IntlConnection extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
|
65
61
|
});
|
|
66
62
|
let startupCommand = '';
|
|
67
63
|
if (this.config.schema)
|
|
68
|
-
startupCommand += 'SET search_path = ' +
|
|
64
|
+
startupCommand += 'SET search_path = ' + escapeLiteral(this.config.schema) + ';';
|
|
69
65
|
if (this.config.timezone)
|
|
70
|
-
startupCommand += 'SET timezone TO ' +
|
|
66
|
+
startupCommand += 'SET timezone TO ' + escapeLiteral(this.config.timezone) + ';';
|
|
71
67
|
if (startupCommand)
|
|
72
68
|
await this.execute(startupCommand, { autoCommit: true });
|
|
73
69
|
this.emit('ready');
|
|
74
70
|
}
|
|
75
71
|
async close() {
|
|
76
|
-
if (this.state ===
|
|
72
|
+
if (this.state === ConnectionState.CLOSED)
|
|
77
73
|
return;
|
|
78
74
|
this.statementQueue.clearQueue();
|
|
79
75
|
return new Promise(resolve => {
|
|
80
|
-
if (this.socket.state ===
|
|
76
|
+
if (this.socket.state === ConnectionState.CLOSED)
|
|
81
77
|
return;
|
|
82
78
|
this.socket.once('close', resolve);
|
|
83
79
|
this.socket.sendTerminateMessage(() => {
|
|
@@ -106,7 +102,7 @@ class IntlConnection extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
|
106
102
|
const rollbackOnError = !transactionCommand &&
|
|
107
103
|
(options?.rollbackOnError != null
|
|
108
104
|
? options.rollbackOnError
|
|
109
|
-
:
|
|
105
|
+
: coerceToBoolean(this.config.rollbackOnError, true));
|
|
110
106
|
if (this.inTransaction && rollbackOnError)
|
|
111
107
|
await this._execute('SAVEPOINT ' + this._onErrorSavePoint);
|
|
112
108
|
try {
|
|
@@ -161,9 +157,9 @@ class IntlConnection extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
|
161
157
|
return !this._refCount;
|
|
162
158
|
}
|
|
163
159
|
assertConnected() {
|
|
164
|
-
if (this.state ===
|
|
160
|
+
if (this.state === ConnectionState.CLOSING)
|
|
165
161
|
throw new Error('Connection is closing');
|
|
166
|
-
if (this.state ===
|
|
162
|
+
if (this.state === ConnectionState.CLOSED)
|
|
167
163
|
throw new Error('Connection closed');
|
|
168
164
|
}
|
|
169
165
|
async _execute(sql, options, cb) {
|
|
@@ -181,33 +177,33 @@ class IntlConnection extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
|
181
177
|
let parsers;
|
|
182
178
|
let current = { command: undefined };
|
|
183
179
|
let fields;
|
|
184
|
-
const typeMap = opts.typeMap ||
|
|
180
|
+
const typeMap = opts.typeMap || GlobalTypeMap;
|
|
185
181
|
return await this.socket.capture(async (code, msg, done) => {
|
|
186
182
|
switch (code) {
|
|
187
|
-
case
|
|
188
|
-
case
|
|
189
|
-
case
|
|
190
|
-
case
|
|
183
|
+
case Protocol.BackendMessageCode.NoticeResponse:
|
|
184
|
+
case Protocol.BackendMessageCode.CopyInResponse:
|
|
185
|
+
case Protocol.BackendMessageCode.CopyOutResponse:
|
|
186
|
+
case Protocol.BackendMessageCode.EmptyQueryResponse:
|
|
191
187
|
break;
|
|
192
|
-
case
|
|
188
|
+
case Protocol.BackendMessageCode.RowDescription:
|
|
193
189
|
fields = msg.fields;
|
|
194
|
-
parsers =
|
|
195
|
-
current.fields =
|
|
190
|
+
parsers = getParsers(typeMap, fields);
|
|
191
|
+
current.fields = wrapRowDescription(typeMap, fields, DataFormat.text);
|
|
196
192
|
current.rows = [];
|
|
197
193
|
break;
|
|
198
|
-
case
|
|
194
|
+
case Protocol.BackendMessageCode.DataRow:
|
|
199
195
|
{
|
|
200
196
|
let row = msg.columns.map((x) => x.toString('utf8'));
|
|
201
|
-
|
|
197
|
+
parseRow(parsers, row, opts);
|
|
202
198
|
if (opts.objectRows && current.fields)
|
|
203
|
-
row =
|
|
199
|
+
row = convertRowToObject(current.fields, row);
|
|
204
200
|
if (cb)
|
|
205
201
|
cb('row', row);
|
|
206
202
|
current.rows = current.rows || [];
|
|
207
203
|
current.rows.push(row);
|
|
208
204
|
}
|
|
209
205
|
break;
|
|
210
|
-
case
|
|
206
|
+
case Protocol.BackendMessageCode.CommandComplete:
|
|
211
207
|
// Ignore BEGIN command that we added to sql
|
|
212
208
|
current.command = msg.command;
|
|
213
209
|
if (current.command === 'DELETE' || current.command === 'INSERT' || current.command === 'UPDATE') {
|
|
@@ -222,7 +218,7 @@ class IntlConnection extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
|
222
218
|
current = { command: undefined };
|
|
223
219
|
currentStart = Date.now();
|
|
224
220
|
break;
|
|
225
|
-
case
|
|
221
|
+
case Protocol.BackendMessageCode.ReadyForQuery:
|
|
226
222
|
this.transactionStatus = msg.status;
|
|
227
223
|
result.totalTime = Date.now() - startTime;
|
|
228
224
|
// Ignore COMMIT command that we added to sql
|
|
@@ -239,12 +235,11 @@ class IntlConnection extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
|
239
235
|
}
|
|
240
236
|
}
|
|
241
237
|
_onError(err) {
|
|
242
|
-
if (this.socket.state !==
|
|
238
|
+
if (this.socket.state !== ConnectionState.READY)
|
|
243
239
|
return;
|
|
244
240
|
this.emit('error', err);
|
|
245
241
|
}
|
|
246
242
|
}
|
|
247
|
-
|
|
248
|
-
function getIntlConnection(connection) {
|
|
243
|
+
export function getIntlConnection(connection) {
|
|
249
244
|
return connection._intlCon;
|
|
250
245
|
}
|
package/esm/connection/pool.js
CHANGED
|
@@ -1,30 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const connection_js_1 = require("./connection.js");
|
|
10
|
-
const intl_connection_js_1 = require("./intl-connection.js");
|
|
11
|
-
class Pool extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
1
|
+
import { Pool as LightningPool } from 'lightning-pool';
|
|
2
|
+
import { coerceToBoolean, coerceToInt } from 'putil-varhelpers';
|
|
3
|
+
import { ConnectionState } from '../constants.js';
|
|
4
|
+
import { SafeEventEmitter } from '../safe-event-emitter.js';
|
|
5
|
+
import { getConnectionConfig } from '../util/connection-config.js';
|
|
6
|
+
import { Connection } from './connection.js';
|
|
7
|
+
import { getIntlConnection, IntlConnection } from './intl-connection.js';
|
|
8
|
+
export class Pool extends SafeEventEmitter {
|
|
12
9
|
constructor(config) {
|
|
13
10
|
super();
|
|
14
|
-
this._notificationListeners = new
|
|
15
|
-
const cfg =
|
|
11
|
+
this._notificationListeners = new SafeEventEmitter();
|
|
12
|
+
const cfg = getConnectionConfig(config);
|
|
16
13
|
this.config = Object.freeze(cfg);
|
|
17
14
|
const poolOptions = {};
|
|
18
|
-
poolOptions.acquireMaxRetries =
|
|
19
|
-
poolOptions.acquireRetryWait =
|
|
20
|
-
poolOptions.acquireTimeoutMillis =
|
|
21
|
-
poolOptions.idleTimeoutMillis =
|
|
22
|
-
poolOptions.max =
|
|
23
|
-
poolOptions.maxQueue =
|
|
24
|
-
poolOptions.max =
|
|
25
|
-
poolOptions.min =
|
|
26
|
-
poolOptions.minIdle =
|
|
27
|
-
poolOptions.validation =
|
|
15
|
+
poolOptions.acquireMaxRetries = coerceToInt(cfg.acquireMaxRetries, 0);
|
|
16
|
+
poolOptions.acquireRetryWait = coerceToInt(cfg.acquireRetryWait, 2000);
|
|
17
|
+
poolOptions.acquireTimeoutMillis = coerceToInt(cfg.acquireTimeoutMillis, 0);
|
|
18
|
+
poolOptions.idleTimeoutMillis = coerceToInt(cfg.idleTimeoutMillis, 30000);
|
|
19
|
+
poolOptions.max = coerceToInt(cfg.max, 10);
|
|
20
|
+
poolOptions.maxQueue = coerceToInt(cfg.maxQueue, 1000);
|
|
21
|
+
poolOptions.max = coerceToInt(cfg.max, 10);
|
|
22
|
+
poolOptions.min = coerceToInt(cfg.min, 0);
|
|
23
|
+
poolOptions.minIdle = coerceToInt(cfg.minIdle, 0);
|
|
24
|
+
poolOptions.validation = coerceToBoolean(cfg.validation, false);
|
|
28
25
|
const poolFactory = {
|
|
29
26
|
create: async () => {
|
|
30
27
|
/* istanbul ignore next */
|
|
@@ -35,7 +32,7 @@ class Pool extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
|
35
32
|
message: `new connection creating`,
|
|
36
33
|
});
|
|
37
34
|
}
|
|
38
|
-
const intlCon = new
|
|
35
|
+
const intlCon = new IntlConnection(cfg);
|
|
39
36
|
await intlCon.connect();
|
|
40
37
|
intlCon.on('close', () => this._pool.destroy(intlCon));
|
|
41
38
|
/* istanbul ignore next */
|
|
@@ -69,7 +66,7 @@ class Pool extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
|
69
66
|
});
|
|
70
67
|
}
|
|
71
68
|
try {
|
|
72
|
-
if (intlCon.state ===
|
|
69
|
+
if (intlCon.state === ConnectionState.READY) {
|
|
73
70
|
await intlCon.execute('ROLLBACK;UNLISTEN *');
|
|
74
71
|
}
|
|
75
72
|
}
|
|
@@ -88,12 +85,12 @@ class Pool extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
|
88
85
|
message: `[${intlCon.processID}] connection validate`,
|
|
89
86
|
});
|
|
90
87
|
}
|
|
91
|
-
if (intlCon.state !==
|
|
88
|
+
if (intlCon.state !== ConnectionState.READY)
|
|
92
89
|
throw new Error('Connection is not active');
|
|
93
90
|
await intlCon.execute('select 1;');
|
|
94
91
|
},
|
|
95
92
|
};
|
|
96
|
-
this._pool = new
|
|
93
|
+
this._pool = new LightningPool(poolFactory, poolOptions);
|
|
97
94
|
this._pool.on('return', (...args) => this.emit('release', ...args));
|
|
98
95
|
this._pool.on('error', (...args) => this.emit('error', ...args));
|
|
99
96
|
this._pool.on('acquire', (...args) => this.emit('acquire', ...args));
|
|
@@ -131,7 +128,7 @@ class Pool extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
|
131
128
|
message: `[${intlCon.processID}] acquired`,
|
|
132
129
|
});
|
|
133
130
|
}
|
|
134
|
-
const connection = new
|
|
131
|
+
const connection = new Connection(this, intlCon);
|
|
135
132
|
/* istanbul ignore next */
|
|
136
133
|
if (this.listenerCount('debug'))
|
|
137
134
|
connection.on('debug', (...args) => this.emit('debug', ...args));
|
|
@@ -173,11 +170,11 @@ class Pool extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
|
173
170
|
async prepare(sql, options) {
|
|
174
171
|
const connection = await this.acquire();
|
|
175
172
|
const statement = await connection.prepare(sql, options);
|
|
176
|
-
statement.once('close', () => this._pool.release(
|
|
173
|
+
statement.once('close', () => this._pool.release(getIntlConnection(connection)));
|
|
177
174
|
return statement;
|
|
178
175
|
}
|
|
179
176
|
release(connection) {
|
|
180
|
-
return this._pool.releaseAsync(
|
|
177
|
+
return this._pool.releaseAsync(getIntlConnection(connection));
|
|
181
178
|
}
|
|
182
179
|
async listen(channel, callback) {
|
|
183
180
|
if (!/^[A-Z]\w+$/i.test(channel))
|
|
@@ -206,7 +203,7 @@ class Pool extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
|
206
203
|
async _initNotificationConnection() {
|
|
207
204
|
if (this._notificationConnection)
|
|
208
205
|
return;
|
|
209
|
-
const conn = (this._notificationConnection = new
|
|
206
|
+
const conn = (this._notificationConnection = new Connection(this.config));
|
|
210
207
|
// Reconnect on connection lost
|
|
211
208
|
conn.on('close', () => reConnect());
|
|
212
209
|
const registerEvents = async () => {
|
|
@@ -229,4 +226,3 @@ class Pool extends safe_event_emitter_js_1.SafeEventEmitter {
|
|
|
229
226
|
await registerEvents();
|
|
230
227
|
}
|
|
231
228
|
}
|
|
232
|
-
exports.Pool = Pool;
|
package/esm/connection/portal.js
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const protocol_js_1 = require("../protocol/protocol.js");
|
|
7
|
-
const intl_connection_js_1 = require("./intl-connection.js");
|
|
8
|
-
class Portal {
|
|
1
|
+
import { DEFAULT_COLUMN_FORMAT } from '../constants.js';
|
|
2
|
+
import { GlobalTypeMap } from '../data-type-map.js';
|
|
3
|
+
import { Protocol } from '../protocol/protocol.js';
|
|
4
|
+
import { getIntlConnection } from './intl-connection.js';
|
|
5
|
+
export class Portal {
|
|
9
6
|
constructor(statement, name) {
|
|
10
|
-
this._columnFormat =
|
|
7
|
+
this._columnFormat = DEFAULT_COLUMN_FORMAT;
|
|
11
8
|
this._statement = statement;
|
|
12
9
|
this._name = name;
|
|
13
10
|
}
|
|
@@ -18,13 +15,13 @@ class Portal {
|
|
|
18
15
|
return this._name;
|
|
19
16
|
}
|
|
20
17
|
async bind(params, queryOptions) {
|
|
21
|
-
const intoCon =
|
|
18
|
+
const intoCon = getIntlConnection(this.connection);
|
|
22
19
|
intoCon.ref();
|
|
23
20
|
try {
|
|
24
21
|
const socket = intoCon.socket;
|
|
25
|
-
this._columnFormat = queryOptions.columnFormat != null ? queryOptions.columnFormat :
|
|
22
|
+
this._columnFormat = queryOptions.columnFormat != null ? queryOptions.columnFormat : Protocol.DataFormat.binary;
|
|
26
23
|
socket.sendBindMessage({
|
|
27
|
-
typeMap: queryOptions.typeMap ||
|
|
24
|
+
typeMap: queryOptions.typeMap || GlobalTypeMap,
|
|
28
25
|
statement: this._statement.name,
|
|
29
26
|
portal: this.name,
|
|
30
27
|
paramTypes: this._statement.paramTypes,
|
|
@@ -34,10 +31,10 @@ class Portal {
|
|
|
34
31
|
socket.sendFlushMessage();
|
|
35
32
|
return await socket.capture(async (code, msg, done) => {
|
|
36
33
|
switch (code) {
|
|
37
|
-
case
|
|
34
|
+
case Protocol.BackendMessageCode.BindComplete:
|
|
38
35
|
done();
|
|
39
36
|
break;
|
|
40
|
-
case
|
|
37
|
+
case Protocol.BackendMessageCode.NoticeResponse:
|
|
41
38
|
break;
|
|
42
39
|
default:
|
|
43
40
|
done(new Error(`Server returned unexpected response message (${String.fromCharCode(code)})`));
|
|
@@ -49,7 +46,7 @@ class Portal {
|
|
|
49
46
|
}
|
|
50
47
|
}
|
|
51
48
|
async retrieveFields() {
|
|
52
|
-
const intoCon =
|
|
49
|
+
const intoCon = getIntlConnection(this.connection);
|
|
53
50
|
intoCon.ref();
|
|
54
51
|
try {
|
|
55
52
|
const socket = intoCon.socket;
|
|
@@ -57,12 +54,12 @@ class Portal {
|
|
|
57
54
|
socket.sendFlushMessage();
|
|
58
55
|
return await socket.capture(async (code, msg, done) => {
|
|
59
56
|
switch (code) {
|
|
60
|
-
case
|
|
57
|
+
case Protocol.BackendMessageCode.NoticeResponse:
|
|
61
58
|
break;
|
|
62
|
-
case
|
|
59
|
+
case Protocol.BackendMessageCode.NoData:
|
|
63
60
|
done();
|
|
64
61
|
break;
|
|
65
|
-
case
|
|
62
|
+
case Protocol.BackendMessageCode.RowDescription:
|
|
66
63
|
done(undefined, msg.fields);
|
|
67
64
|
break;
|
|
68
65
|
default:
|
|
@@ -75,7 +72,7 @@ class Portal {
|
|
|
75
72
|
}
|
|
76
73
|
}
|
|
77
74
|
async execute(fetchCount) {
|
|
78
|
-
const intoCon =
|
|
75
|
+
const intoCon = getIntlConnection(this.connection);
|
|
79
76
|
intoCon.ref();
|
|
80
77
|
try {
|
|
81
78
|
const socket = intoCon.socket;
|
|
@@ -84,24 +81,24 @@ class Portal {
|
|
|
84
81
|
const rows = [];
|
|
85
82
|
return await socket.capture(async (code, msg, done) => {
|
|
86
83
|
switch (code) {
|
|
87
|
-
case
|
|
84
|
+
case Protocol.BackendMessageCode.NoticeResponse:
|
|
88
85
|
break;
|
|
89
|
-
case
|
|
86
|
+
case Protocol.BackendMessageCode.NoData:
|
|
90
87
|
done(undefined, { code });
|
|
91
88
|
break;
|
|
92
|
-
case
|
|
89
|
+
case Protocol.BackendMessageCode.DataRow:
|
|
93
90
|
if (Array.isArray(this._columnFormat)) {
|
|
94
|
-
rows.push(msg.columns.map((buf, i) => this._columnFormat[i] ===
|
|
91
|
+
rows.push(msg.columns.map((buf, i) => this._columnFormat[i] === Protocol.DataFormat.text ? buf.toString('utf8') : buf));
|
|
95
92
|
}
|
|
96
|
-
else if (this._columnFormat ===
|
|
93
|
+
else if (this._columnFormat === Protocol.DataFormat.binary)
|
|
97
94
|
rows.push(msg.columns);
|
|
98
95
|
else
|
|
99
96
|
rows.push(msg.columns.map((buf) => buf.toString('utf8')));
|
|
100
97
|
break;
|
|
101
|
-
case
|
|
98
|
+
case Protocol.BackendMessageCode.PortalSuspended:
|
|
102
99
|
done(undefined, { code, rows });
|
|
103
100
|
break;
|
|
104
|
-
case
|
|
101
|
+
case Protocol.BackendMessageCode.CommandComplete:
|
|
105
102
|
done(undefined, {
|
|
106
103
|
code,
|
|
107
104
|
rows,
|
|
@@ -119,7 +116,7 @@ class Portal {
|
|
|
119
116
|
}
|
|
120
117
|
}
|
|
121
118
|
async close() {
|
|
122
|
-
const intoCon =
|
|
119
|
+
const intoCon = getIntlConnection(this.connection);
|
|
123
120
|
intoCon.ref();
|
|
124
121
|
try {
|
|
125
122
|
const socket = intoCon.socket;
|
|
@@ -127,11 +124,11 @@ class Portal {
|
|
|
127
124
|
socket.sendSyncMessage();
|
|
128
125
|
return await socket.capture(async (code, msg, done) => {
|
|
129
126
|
switch (code) {
|
|
130
|
-
case
|
|
127
|
+
case Protocol.BackendMessageCode.NoticeResponse:
|
|
131
128
|
break;
|
|
132
|
-
case
|
|
129
|
+
case Protocol.BackendMessageCode.CloseComplete:
|
|
133
130
|
break;
|
|
134
|
-
case
|
|
131
|
+
case Protocol.BackendMessageCode.ReadyForQuery:
|
|
135
132
|
intoCon.transactionStatus = msg.status;
|
|
136
133
|
done();
|
|
137
134
|
break;
|
|
@@ -145,4 +142,3 @@ class Portal {
|
|
|
145
142
|
}
|
|
146
143
|
}
|
|
147
144
|
}
|
|
148
|
-
exports.Portal = Portal;
|