@m2k-5f/pgtx 2.5.4 → 2.6.1
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/README.md +65 -81
- package/dist/batch.d.ts +14 -0
- package/dist/batch.d.ts.map +1 -0
- package/dist/batch.js +34 -0
- package/dist/clauses/abstract.clause.d.ts.map +1 -1
- package/dist/connection.d.ts +10 -34
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +162 -215
- package/dist/error.d.ts +8 -0
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +8 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/pool.d.ts +7 -12
- package/dist/pool.d.ts.map +1 -1
- package/dist/pool.js +23 -24
- package/dist/protocol/connection-request-writer.d.ts.map +1 -1
- package/dist/protocol/connection-request-writer.js +1 -0
- package/dist/protocol/connection-response-reader.d.ts +2 -4
- package/dist/protocol/connection-response-reader.d.ts.map +1 -1
- package/dist/protocol/connection-response-reader.js +1 -2
- package/dist/protocol/socket-authorization.d.ts +1 -10
- package/dist/protocol/socket-authorization.d.ts.map +1 -1
- package/dist/protocol/socket-authorization.js +1 -4
- package/dist/protocol/socket-connector.d.ts.map +1 -1
- package/dist/protocol/socket-connector.js +0 -2
- package/dist/query.d.ts +25 -27
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +36 -35
- package/dist/transaction.d.ts +3 -4
- package/dist/transaction.d.ts.map +1 -1
- package/dist/transaction.js +13 -11
- package/dist/types.d.ts +52 -11
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/template-compiler.d.ts.map +1 -1
- package/package.json +2 -2
package/dist/connection.js
CHANGED
|
@@ -1,19 +1,16 @@
|
|
|
1
|
-
import { nextTick } from "process";
|
|
2
1
|
import { ConnectionRequestWriter } from "./protocol/connection-request-writer";
|
|
3
2
|
import { createAuthorizedSocket } from "./protocol/socket-authorization";
|
|
4
3
|
import { ResponseTypes } from "./protocol/constants";
|
|
5
4
|
import { compileSqlTemplate } from "./utils/template-compiler";
|
|
6
5
|
import { Transaction } from "./transaction";
|
|
7
6
|
import { SocketConnector } from "./protocol/socket-connector";
|
|
8
|
-
import { Queue
|
|
9
|
-
import {
|
|
7
|
+
import { Queue } from "./queue";
|
|
8
|
+
import { ParseQuery, SimpleQuery, StreamQuery } from "./query";
|
|
10
9
|
import { sql } from ".";
|
|
11
10
|
import { Begin, Future, Ok } from 'fluent-future';
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const ErrConnectionClosed = new PostgresError("Connection is closed", 'connection_closed', "", "ERROR");
|
|
16
|
-
const ErrConnectionReconnecring = new PostgresError("Connection are reconnecting", "connection_reconnecting", "", "ERROR");
|
|
11
|
+
import { ErrConnectionClosed, ErrConnectionReconnecting } from "./error";
|
|
12
|
+
import { Batch } from "./batch";
|
|
13
|
+
import { nextTick } from "process";
|
|
17
14
|
/**
|
|
18
15
|
* Represents a single dedicated connection to the PostgreSQL database.
|
|
19
16
|
*
|
|
@@ -48,28 +45,18 @@ export class Connection {
|
|
|
48
45
|
_nextStatement() {
|
|
49
46
|
return `s-${this._stmtCounter++}`;
|
|
50
47
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
throw ErrConnectionClosed;
|
|
54
|
-
}
|
|
55
|
-
constructor(socket, writer, logLevel, params) {
|
|
56
|
-
this._isFlushing = false;
|
|
48
|
+
constructor(config, socket) {
|
|
49
|
+
this._activeBatch = null;
|
|
57
50
|
this._isOpened = true;
|
|
58
51
|
this._isReconnecting = false;
|
|
52
|
+
this._cachedBuffer = ConnectionRequestWriter.new();
|
|
59
53
|
this._batchQueue = new Queue();
|
|
60
|
-
this._described = new Map();
|
|
61
|
-
this._describingPending = new Set();
|
|
62
54
|
this._parsed = new Map();
|
|
63
|
-
this.
|
|
55
|
+
this._parsing = new Map();
|
|
64
56
|
this._listeningCallbacks = new Map();
|
|
65
57
|
this._stmtCounter = 0;
|
|
66
|
-
this.
|
|
67
|
-
this.
|
|
68
|
-
this._socket = new SocketConnector(socket, (type, _, reader) => this._handlePacket(type, reader), (err) => {
|
|
69
|
-
// console.log(err)
|
|
70
|
-
this._registerReconnect();
|
|
71
|
-
});
|
|
72
|
-
this._writer = writer;
|
|
58
|
+
this.config = config;
|
|
59
|
+
this._socket = new SocketConnector(socket, (type, _, reader) => this._handlePacket(type, reader), () => this._registerReconnect());
|
|
73
60
|
}
|
|
74
61
|
/**
|
|
75
62
|
* Creates a new database connection.
|
|
@@ -89,10 +76,49 @@ export class Connection {
|
|
|
89
76
|
* })
|
|
90
77
|
* ```
|
|
91
78
|
*/
|
|
92
|
-
static new(
|
|
79
|
+
static new(config) {
|
|
80
|
+
const conf = {
|
|
81
|
+
...config,
|
|
82
|
+
logLevel: config.logLevel || 'error',
|
|
83
|
+
int8toBigint: config.int8toBigint || false,
|
|
84
|
+
queryTimeout: config.queryTimeout || 30000,
|
|
85
|
+
syncShedule: config.syncShedule || 'Immediate'
|
|
86
|
+
};
|
|
93
87
|
const writer = ConnectionRequestWriter.new();
|
|
94
|
-
return createAuthorizedSocket(writer,
|
|
95
|
-
.andThen(socket => Ok(new Connection(
|
|
88
|
+
return createAuthorizedSocket(writer, conf)
|
|
89
|
+
.andThen(socket => Ok(new Connection(conf, socket)));
|
|
90
|
+
}
|
|
91
|
+
_registerBatch() {
|
|
92
|
+
if (!this._activeBatch) {
|
|
93
|
+
const batch = new Batch(this._cachedBuffer.clear());
|
|
94
|
+
this._activeBatch = batch;
|
|
95
|
+
(this.config.syncShedule === 'Immediate' ? setImmediate : nextTick)(() => {
|
|
96
|
+
this._sync(batch);
|
|
97
|
+
});
|
|
98
|
+
return batch;
|
|
99
|
+
}
|
|
100
|
+
return this._activeBatch;
|
|
101
|
+
}
|
|
102
|
+
_sync(batch) {
|
|
103
|
+
if (!this._isOpened) {
|
|
104
|
+
batch.reject(ErrConnectionClosed);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (this._isReconnecting) {
|
|
108
|
+
this._reconnect()
|
|
109
|
+
.then(() => {
|
|
110
|
+
this._activeBatch = null;
|
|
111
|
+
void this._restoreSubscriptions();
|
|
112
|
+
this._socket.write(this._registerBatch().end());
|
|
113
|
+
this._batchQueue.push(this._registerBatch());
|
|
114
|
+
})
|
|
115
|
+
.then(() => {
|
|
116
|
+
this._isReconnecting = false;
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
this._socket.write(batch.end());
|
|
120
|
+
this._activeBatch = null;
|
|
121
|
+
this._batchQueue.push(batch);
|
|
96
122
|
}
|
|
97
123
|
/**
|
|
98
124
|
* Executes a query using tagged template literals.
|
|
@@ -119,16 +145,31 @@ export class Connection {
|
|
|
119
145
|
* ```
|
|
120
146
|
*/
|
|
121
147
|
query(templates, ...params) {
|
|
122
|
-
this.
|
|
148
|
+
if (!this._isOpened)
|
|
149
|
+
return Future.reject(ErrConnectionClosed);
|
|
150
|
+
if (this._isReconnecting)
|
|
151
|
+
return Future.reject(ErrConnectionReconnecting);
|
|
123
152
|
const { text, args } = compileSqlTemplate(templates, params);
|
|
124
|
-
if (this.
|
|
153
|
+
if (this.config.logLevel === 'query') {
|
|
125
154
|
console.log(`\nQUERY: ${text}\n${args.length !== 0 ? `ARGUMENTS: [${args}]\n` : ""}`);
|
|
126
155
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
156
|
+
if (this._parsed.has(text)) {
|
|
157
|
+
const meta = this._parsed.get(text);
|
|
158
|
+
const query = new SimpleQuery(meta.statement, text, args, meta.columns, this.config.queryTimeout);
|
|
159
|
+
this._registerBatch().registerQuery(query);
|
|
160
|
+
return query.future;
|
|
161
|
+
}
|
|
162
|
+
if (!this._parsing.has(text)) {
|
|
163
|
+
const parseQuery = new ParseQuery(this._nextStatement(), text, this.config.queryTimeout);
|
|
164
|
+
this._parsing.set(text, parseQuery.future);
|
|
165
|
+
this._registerBatch().registerQuery(parseQuery);
|
|
166
|
+
}
|
|
167
|
+
const future = this._parsing.get(text);
|
|
168
|
+
return future.andThen(meta => {
|
|
169
|
+
const query = new SimpleQuery(meta.statement, text, args, meta.columns, this.config.queryTimeout);
|
|
170
|
+
this._registerBatch().registerQuery(query);
|
|
171
|
+
return query.future;
|
|
172
|
+
});
|
|
132
173
|
}
|
|
133
174
|
/**
|
|
134
175
|
* Starts a managed transaction on this connection.
|
|
@@ -152,7 +193,10 @@ export class Connection {
|
|
|
152
193
|
* ```
|
|
153
194
|
*/
|
|
154
195
|
begin(txCallback) {
|
|
155
|
-
this.
|
|
196
|
+
if (!this._isOpened)
|
|
197
|
+
return Future.reject(ErrConnectionClosed);
|
|
198
|
+
if (this._isReconnecting)
|
|
199
|
+
return Future.reject(ErrConnectionReconnecting);
|
|
156
200
|
const tx = new Transaction(this);
|
|
157
201
|
return Begin()
|
|
158
202
|
.andThen(() => tx.query `BEGIN`)
|
|
@@ -178,8 +222,11 @@ export class Connection {
|
|
|
178
222
|
* ```
|
|
179
223
|
*/
|
|
180
224
|
notify(channelName, payload = "") {
|
|
181
|
-
this.
|
|
182
|
-
|
|
225
|
+
if (!this._isOpened)
|
|
226
|
+
return Future.reject(ErrConnectionClosed);
|
|
227
|
+
if (this._isReconnecting)
|
|
228
|
+
return Future.reject(ErrConnectionReconnecting);
|
|
229
|
+
return this.query `select pg_notify(${channelName}, ${payload})`.map(() => { });
|
|
183
230
|
}
|
|
184
231
|
/**
|
|
185
232
|
* Subscribes a callback to a channel. Sends `LISTEN` on the first subscription.
|
|
@@ -193,7 +240,10 @@ export class Connection {
|
|
|
193
240
|
* ```
|
|
194
241
|
*/
|
|
195
242
|
listen(channelName, callback) {
|
|
196
|
-
this.
|
|
243
|
+
if (!this._isOpened)
|
|
244
|
+
return Future.reject(ErrConnectionClosed);
|
|
245
|
+
if (this._isReconnecting)
|
|
246
|
+
return Future.reject(ErrConnectionReconnecting);
|
|
197
247
|
if (!this._listeningCallbacks.has(channelName)) {
|
|
198
248
|
this._listeningCallbacks.set(channelName, new Set());
|
|
199
249
|
}
|
|
@@ -213,7 +263,10 @@ export class Connection {
|
|
|
213
263
|
* ```
|
|
214
264
|
*/
|
|
215
265
|
unlisten(channelName, callback) {
|
|
216
|
-
this.
|
|
266
|
+
if (!this._isOpened)
|
|
267
|
+
return Future.reject(ErrConnectionClosed);
|
|
268
|
+
if (this._isReconnecting)
|
|
269
|
+
return Future.reject(ErrConnectionReconnecting);
|
|
217
270
|
if (!this._listeningCallbacks.has(channelName)) {
|
|
218
271
|
return Ok();
|
|
219
272
|
}
|
|
@@ -251,9 +304,12 @@ export class Connection {
|
|
|
251
304
|
* }
|
|
252
305
|
*/
|
|
253
306
|
stream(templates, ...params) {
|
|
254
|
-
this.
|
|
307
|
+
if (!this._isOpened)
|
|
308
|
+
throw ErrConnectionClosed;
|
|
309
|
+
if (this._isReconnecting)
|
|
310
|
+
throw ErrConnectionReconnecting;
|
|
255
311
|
const { text, args } = compileSqlTemplate(templates, params);
|
|
256
|
-
if (this.
|
|
312
|
+
if (this.config.logLevel === 'query') {
|
|
257
313
|
console.log(`\nQUERY: ${text}\n${args.length !== 0 ? `ARGUMENTS: [${args}]\n` : ""}`);
|
|
258
314
|
}
|
|
259
315
|
let controller;
|
|
@@ -262,150 +318,69 @@ export class Connection {
|
|
|
262
318
|
controller = c;
|
|
263
319
|
}
|
|
264
320
|
});
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
321
|
+
if (this._parsed.has(text)) {
|
|
322
|
+
const meta = this._parsed.get(text);
|
|
323
|
+
const query = new StreamQuery(meta.statement, text, args, controller, meta.columns, this.config.queryTimeout);
|
|
324
|
+
this._registerBatch().registerQuery(query);
|
|
325
|
+
return stream;
|
|
326
|
+
}
|
|
327
|
+
if (!this._parsing.has(text)) {
|
|
328
|
+
const parseQuery = new ParseQuery(this._nextStatement(), text, this.config.queryTimeout);
|
|
329
|
+
this._parsing.set(text, parseQuery.future);
|
|
330
|
+
this._registerBatch().registerQuery(parseQuery);
|
|
331
|
+
}
|
|
332
|
+
const future = this._parsing.get(text);
|
|
333
|
+
future
|
|
334
|
+
.tap(meta => {
|
|
335
|
+
const query = new StreamQuery(meta.statement, text, args, controller, meta.columns, this.config.queryTimeout);
|
|
336
|
+
this._registerBatch().registerQuery(query);
|
|
337
|
+
})
|
|
338
|
+
.catch(err => controller.error(err));
|
|
269
339
|
return stream;
|
|
270
340
|
}
|
|
271
341
|
_streamWithController(templates, params, controller) {
|
|
272
|
-
this.
|
|
342
|
+
if (!this._isOpened)
|
|
343
|
+
throw ErrConnectionClosed;
|
|
344
|
+
if (this._isReconnecting)
|
|
345
|
+
throw ErrConnectionReconnecting;
|
|
273
346
|
const { text, args } = compileSqlTemplate(templates, params);
|
|
274
|
-
if (this.
|
|
347
|
+
if (this.config.logLevel === 'query') {
|
|
275
348
|
console.log(`\nQUERY: ${text}\n${args.length !== 0 ? `ARGUMENTS: [${args}]\n` : ""}`);
|
|
276
349
|
}
|
|
277
|
-
const query = this._createStream(text, args, controller);
|
|
278
|
-
if (this._writeQuery(query))
|
|
279
|
-
throw ErrBatchOverflowed;
|
|
280
|
-
query.startTimeout(this.params.queryTimeout || 30000);
|
|
281
|
-
return query;
|
|
282
|
-
}
|
|
283
|
-
_createQuery(text, args) {
|
|
284
350
|
if (this._parsed.has(text)) {
|
|
285
|
-
const
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
return new Query(text, args, QueryState.Executing, statementName, columns);
|
|
289
|
-
}
|
|
290
|
-
else {
|
|
291
|
-
if (this._describingPending.has(statementName)) {
|
|
292
|
-
return new Query(text, args, QueryState.Executing, statementName);
|
|
293
|
-
}
|
|
294
|
-
else {
|
|
295
|
-
this._describingPending.add(statementName);
|
|
296
|
-
return new Query(text, args, QueryState.Describing, statementName);
|
|
297
|
-
}
|
|
298
|
-
}
|
|
351
|
+
const meta = this._parsed.get(text);
|
|
352
|
+
const query = new StreamQuery(meta.statement, text, args, controller, meta.columns, this.config.queryTimeout);
|
|
353
|
+
this._registerBatch().registerQuery(query);
|
|
299
354
|
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
}
|
|
305
|
-
else {
|
|
306
|
-
const statementName = this._nextStatement();
|
|
307
|
-
this._parsingPending.set(text, statementName);
|
|
308
|
-
return new Query(text, args, QueryState.Parsing, statementName);
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
_createStream(text, args, controller) {
|
|
313
|
-
if (this._parsed.has(text)) {
|
|
314
|
-
const statementName = this._parsed.get(text);
|
|
315
|
-
if (this._described.has(statementName)) {
|
|
316
|
-
const columns = this._described.get(statementName);
|
|
317
|
-
return new StreamQuery(text, args, QueryState.Executing, statementName, controller, columns);
|
|
318
|
-
}
|
|
319
|
-
else {
|
|
320
|
-
if (this._describingPending.has(statementName)) {
|
|
321
|
-
return new StreamQuery(text, args, QueryState.Executing, statementName, controller);
|
|
322
|
-
}
|
|
323
|
-
else {
|
|
324
|
-
this._describingPending.add(statementName);
|
|
325
|
-
return new StreamQuery(text, args, QueryState.Describing, statementName, controller);
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
else {
|
|
330
|
-
if (this._parsingPending.has(text)) {
|
|
331
|
-
const statementName = this._parsingPending.get(text);
|
|
332
|
-
return new StreamQuery(text, args, QueryState.Describing, statementName, controller);
|
|
333
|
-
}
|
|
334
|
-
else {
|
|
335
|
-
const statementName = this._nextStatement();
|
|
336
|
-
this._parsingPending.set(text, statementName);
|
|
337
|
-
return new StreamQuery(text, args, QueryState.Parsing, statementName, controller);
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
_writeQuery(query) {
|
|
342
|
-
const batch = this._registerFlush();
|
|
343
|
-
if (batch.isFull)
|
|
344
|
-
return true;
|
|
345
|
-
batch.push(query);
|
|
346
|
-
if (query.state === QueryState.Parsing) {
|
|
347
|
-
this._writer
|
|
348
|
-
.writeParse(query.statementName, query.text)
|
|
349
|
-
.writeDescribe(query.statementName);
|
|
350
|
-
}
|
|
351
|
-
if (query.state === QueryState.Describing) {
|
|
352
|
-
this._writer
|
|
353
|
-
.writeDescribe(query.statementName);
|
|
354
|
-
}
|
|
355
|
-
this._writer
|
|
356
|
-
.writeBind("", query.statementName, query.args)
|
|
357
|
-
.writeExecute("");
|
|
358
|
-
}
|
|
359
|
-
_registerFlush() {
|
|
360
|
-
if (!this._isFlushing) {
|
|
361
|
-
this._isFlushing = true;
|
|
362
|
-
const batch = new RingQueue(this.params.batchCapacity);
|
|
363
|
-
this._batchQueue.push(batch);
|
|
364
|
-
this.params.flushShedule === 'nextTick'
|
|
365
|
-
? nextTick(() => this._flush())
|
|
366
|
-
: setImmediate(() => this._flush());
|
|
367
|
-
return batch;
|
|
368
|
-
}
|
|
369
|
-
return this._batchQueue.last;
|
|
370
|
-
}
|
|
371
|
-
_flush() {
|
|
372
|
-
if (!this._isOpened) {
|
|
373
|
-
this._rejectPipeline(ErrConnectionClosed);
|
|
374
|
-
return;
|
|
375
|
-
}
|
|
376
|
-
if (this._isReconnecting) {
|
|
377
|
-
this._reconnect().then(() => {
|
|
378
|
-
this._socket.write(this._writer.writeSync());
|
|
379
|
-
this._writer.clear();
|
|
380
|
-
this._isFlushing = false;
|
|
381
|
-
});
|
|
382
|
-
}
|
|
383
|
-
else {
|
|
384
|
-
this._socket.write(this._writer.writeSync());
|
|
385
|
-
this._writer.clear();
|
|
386
|
-
this._isFlushing = false;
|
|
355
|
+
if (!this._parsing.has(text)) {
|
|
356
|
+
const parseQuery = new ParseQuery(this._nextStatement(), text, this.config.queryTimeout);
|
|
357
|
+
this._parsing.set(text, parseQuery.future);
|
|
358
|
+
this._registerBatch().registerQuery(parseQuery);
|
|
387
359
|
}
|
|
360
|
+
const future = this._parsing.get(text);
|
|
361
|
+
future
|
|
362
|
+
.tap(meta => {
|
|
363
|
+
const query = new StreamQuery(meta.statement, text, args, controller, meta.columns, this.config.queryTimeout);
|
|
364
|
+
this._registerBatch().registerQuery(query);
|
|
365
|
+
})
|
|
366
|
+
.catch(err => controller.error(err));
|
|
388
367
|
}
|
|
389
368
|
_registerReconnect() {
|
|
369
|
+
if (this._isReconnecting)
|
|
370
|
+
return;
|
|
390
371
|
this._isReconnecting = true;
|
|
391
|
-
this._rejectPipeline(ErrConnectionReconnecring);
|
|
392
372
|
}
|
|
393
373
|
_resetConnectionState(cause) {
|
|
394
374
|
this._parsed.clear();
|
|
395
|
-
this.
|
|
396
|
-
this.
|
|
397
|
-
this.
|
|
398
|
-
this._writer.clear();
|
|
399
|
-
this._rejectPipeline(cause);
|
|
375
|
+
this._parsing.clear();
|
|
376
|
+
this._activeBatch = null;
|
|
377
|
+
this._rejectAllBatches(cause);
|
|
400
378
|
}
|
|
401
379
|
async _reconnect() {
|
|
402
|
-
|
|
403
|
-
this._resetConnectionState(ErrConnectionReconnecring);
|
|
404
|
-
const socket = await createAuthorizedSocket(ConnectionRequestWriter.new(), this.params);
|
|
380
|
+
const socket = await createAuthorizedSocket(ConnectionRequestWriter.new(), this.config);
|
|
405
381
|
const connector = new SocketConnector(socket, (type, _, reader) => this._handlePacket(type, reader), (err) => this._registerReconnect());
|
|
406
382
|
this._socket = connector;
|
|
407
|
-
|
|
408
|
-
this._isReconnecting = false;
|
|
383
|
+
this._resetConnectionState(ErrConnectionReconnecting);
|
|
409
384
|
}
|
|
410
385
|
_restoreSubscriptions() {
|
|
411
386
|
if (this._listeningCallbacks.size === 0)
|
|
@@ -418,12 +393,9 @@ export class Connection {
|
|
|
418
393
|
_getCurrentQuery() {
|
|
419
394
|
return this._batchQueue.current.current;
|
|
420
395
|
}
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
const queue = this._batchQueue.current;
|
|
425
|
-
while (queue.hasMore) {
|
|
426
|
-
queue.shift.reject(error);
|
|
396
|
+
_rejectAllBatches(cause) {
|
|
397
|
+
while (this._batchQueue.hasMore) {
|
|
398
|
+
this._batchQueue.shift.reject(cause);
|
|
427
399
|
}
|
|
428
400
|
}
|
|
429
401
|
_handlePacket(type, reader) {
|
|
@@ -431,10 +403,6 @@ export class Connection {
|
|
|
431
403
|
case ResponseTypes.ParseComplete:
|
|
432
404
|
{
|
|
433
405
|
reader.readParseComplete();
|
|
434
|
-
const query = this._getCurrentQuery();
|
|
435
|
-
this._parsingPending.delete(query.text);
|
|
436
|
-
this._parsed.set(query.text, query.statementName);
|
|
437
|
-
query.state = QueryState.Describing;
|
|
438
406
|
}
|
|
439
407
|
break;
|
|
440
408
|
case ResponseTypes.BindComplete:
|
|
@@ -456,65 +424,47 @@ export class Connection {
|
|
|
456
424
|
{
|
|
457
425
|
reader.readNoData();
|
|
458
426
|
const query = this._getCurrentQuery();
|
|
459
|
-
|
|
460
|
-
this.
|
|
461
|
-
query.
|
|
462
|
-
query.
|
|
427
|
+
const meta = { statement: query.statement, columns: [] };
|
|
428
|
+
this._parsing.delete(query.text);
|
|
429
|
+
query.resolve(meta);
|
|
430
|
+
this._parsed.set(query.text, meta);
|
|
431
|
+
this._batchQueue.current.next();
|
|
463
432
|
}
|
|
464
433
|
break;
|
|
465
434
|
case ResponseTypes.RowDescription:
|
|
466
435
|
{
|
|
467
436
|
const columns = reader.readRowDescription();
|
|
468
437
|
const query = this._getCurrentQuery();
|
|
469
|
-
|
|
470
|
-
this.
|
|
471
|
-
query.
|
|
472
|
-
query.
|
|
438
|
+
const meta = { statement: query.statement, columns };
|
|
439
|
+
this._parsing.delete(query.text);
|
|
440
|
+
query.resolve(meta);
|
|
441
|
+
this._parsed.set(query.text, meta);
|
|
442
|
+
this._batchQueue.current.next();
|
|
473
443
|
}
|
|
474
444
|
break;
|
|
475
445
|
case ResponseTypes.DataRow:
|
|
476
446
|
{
|
|
477
447
|
let query = this._getCurrentQuery();
|
|
478
|
-
|
|
479
|
-
query.columns = this._described.get(query.statementName);
|
|
480
|
-
}
|
|
481
|
-
query.push(reader.readDataRow(query.columns, this.params.int8toBigint));
|
|
448
|
+
query.push(reader.readDataRow(query.columns, this.config.int8toBigint));
|
|
482
449
|
}
|
|
483
450
|
break;
|
|
484
451
|
case ResponseTypes.ComandComplete:
|
|
485
452
|
{
|
|
486
453
|
reader.readCommandComplete();
|
|
487
|
-
if (this._batchQueue.isFree) {
|
|
488
|
-
this.close();
|
|
489
|
-
break;
|
|
490
|
-
}
|
|
491
454
|
const query = this._getCurrentQuery();
|
|
492
|
-
if (!query) {
|
|
493
|
-
this.close();
|
|
494
|
-
break;
|
|
495
|
-
}
|
|
496
|
-
query.state = QueryState.Completed;
|
|
497
|
-
this._batchQueue.current.next();
|
|
498
455
|
query.resolve();
|
|
456
|
+
this._batchQueue.current.next();
|
|
499
457
|
}
|
|
500
458
|
break;
|
|
501
459
|
case ResponseTypes.ErrorResponse:
|
|
502
460
|
{
|
|
503
461
|
const error = reader.readErrorResponse();
|
|
504
|
-
if (this.
|
|
462
|
+
if (this.config.logLevel === 'error' || this.config.logLevel === 'notice' || this.config.logLevel === 'query') {
|
|
505
463
|
console.log(`\nError: ${error}\n`);
|
|
506
464
|
}
|
|
507
465
|
const query = this._getCurrentQuery();
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
this._parsingPending.delete(query.text);
|
|
511
|
-
break;
|
|
512
|
-
case QueryState.Describing:
|
|
513
|
-
this._describingPending.delete(query.statementName);
|
|
514
|
-
break;
|
|
515
|
-
}
|
|
516
|
-
query.state = QueryState.Failed;
|
|
517
|
-
this._rejectPipeline(error);
|
|
466
|
+
this._parsing.delete(query.text);
|
|
467
|
+
this._batchQueue.current.reject(error);
|
|
518
468
|
}
|
|
519
469
|
break;
|
|
520
470
|
case ResponseTypes.ReadyForQuery:
|
|
@@ -526,7 +476,7 @@ export class Connection {
|
|
|
526
476
|
case ResponseTypes.Notice:
|
|
527
477
|
{
|
|
528
478
|
const message = reader.readErrorResponse();
|
|
529
|
-
if (this.
|
|
479
|
+
if (this.config.logLevel === 'notice' || this.config.logLevel === 'query') {
|
|
530
480
|
console.log(`\nError: ${message}\n`);
|
|
531
481
|
}
|
|
532
482
|
}
|
|
@@ -540,7 +490,7 @@ export class Connection {
|
|
|
540
490
|
callbackSet.forEach(cb => cb(payload));
|
|
541
491
|
}
|
|
542
492
|
break;
|
|
543
|
-
default: console.
|
|
493
|
+
default: console.log('Undeclared response type: ', type);
|
|
544
494
|
}
|
|
545
495
|
}
|
|
546
496
|
/**
|
|
@@ -563,9 +513,6 @@ export class Connection {
|
|
|
563
513
|
close() {
|
|
564
514
|
this._isOpened = false;
|
|
565
515
|
this._socket.destroy();
|
|
566
|
-
|
|
567
|
-
this._rejectPipeline(ErrConnectionClosed);
|
|
568
|
-
this._batchQueue.next();
|
|
569
|
-
}
|
|
516
|
+
this._rejectAllBatches(ErrConnectionClosed);
|
|
570
517
|
}
|
|
571
518
|
}
|
package/dist/error.d.ts
CHANGED
|
@@ -15,4 +15,12 @@ export declare class PostgresError extends Error {
|
|
|
15
15
|
get isTimeout(): boolean;
|
|
16
16
|
get isConnectionFailure(): boolean;
|
|
17
17
|
}
|
|
18
|
+
export declare const ErrNonceMismatch: PostgresError;
|
|
19
|
+
export declare const ErrPasswordRequired: PostgresError;
|
|
20
|
+
export declare const ErrSocketFailedDuringAuth: PostgresError;
|
|
21
|
+
export declare const ErrQueryTimeout: PostgresError;
|
|
22
|
+
export declare const ErrPoolClosed: PostgresError;
|
|
23
|
+
export declare const ErrTransactionClosed: PostgresError;
|
|
24
|
+
export declare const ErrConnectionClosed: PostgresError;
|
|
25
|
+
export declare const ErrConnectionReconnecting: PostgresError;
|
|
18
26
|
//# sourceMappingURL=error.d.ts.map
|
package/dist/error.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAAA,qBAAa,aAAc,SAAQ,KAAK;IAEhB,OAAO,EAAE,MAAM;IACxB,IAAI,EAAE,MAAM;IACZ,MAAM,EAAE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D,IAAI,SAAS,IAAI,OAAO,CAEvB;IAGD,IAAI,mBAAmB,IAAI,OAAO,CAEjC;CACJ;AAED,eAAO,MAAM,gBAAgB,eAAmF,CAAA;AAChH,eAAO,MAAM,mBAAmB,eAAqE,CAAA;AACrG,eAAO,MAAM,yBAAyB,eAAiD,CAAA;AAEvF,eAAO,MAAM,eAAe,eAA8C,CAAA;AAE1E,eAAO,MAAM,aAAa,eAAkE,CAAA;AAE5F,eAAO,MAAM,oBAAoB,eAA6E,CAAA;AAE9G,eAAO,MAAM,mBAAmB,eAA8E,CAAA;AAC9G,eAAO,MAAM,yBAAyB,eAA2F,CAAA"}
|
package/dist/error.js
CHANGED
|
@@ -31,3 +31,11 @@ export class PostgresError extends Error {
|
|
|
31
31
|
return this.code.startsWith('08') || this.code.startsWith('57') && this.code !== '57014';
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
|
+
export const ErrNonceMismatch = new PostgresError("Protocol violation: server nonce doesn't match client nonce");
|
|
35
|
+
export const ErrPasswordRequired = new PostgresError('The authorization method requires a password.');
|
|
36
|
+
export const ErrSocketFailedDuringAuth = new PostgresError("Socket failed during auth");
|
|
37
|
+
export const ErrQueryTimeout = new PostgresError('Query timeout', '57014');
|
|
38
|
+
export const ErrPoolClosed = new PostgresError("Pool is closed", 'pool_closed', '', "ERROR");
|
|
39
|
+
export const ErrTransactionClosed = new PostgresError("Transaction closed", "transaction_closed", '', "ERROR");
|
|
40
|
+
export const ErrConnectionClosed = new PostgresError("Connection is closed", 'connection_closed', "", "ERROR");
|
|
41
|
+
export const ErrConnectionReconnecting = new PostgresError("Connection are reconnecting", "connection_reconnecting", "", "ERROR");
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,IAAI,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAC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;;OAOG;;IAGH;;;;;;;;OAQG;;IAGH;;;;;;;;;;;;;;;;;;;OAmBG;;CAEN,CAAA;AAGD;;;GAGG;AAEH,OAAO,EAAE,UAAU,IAAI,UAAU,EAAE,CAAA;AAEnC,OAAO,EAAE,WAAW,IAAI,WAAW,EAAE,CAAA;AAErC,OAAO,EAAE,QAAQ,IAAI,IAAI,EAAE,CAAA;AAG3B,cAAc,WAAW,CAAA;AACzB,cAAc,SAAS,CAAA"}
|
package/dist/index.js
CHANGED
package/dist/pool.d.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import { Connection
|
|
1
|
+
import { Connection } from "./connection";
|
|
2
2
|
import { Transaction } from "./transaction";
|
|
3
3
|
import { Future } from "fluent-future";
|
|
4
4
|
import { PostgresError } from "./error";
|
|
5
|
-
|
|
6
|
-
max?: number;
|
|
7
|
-
};
|
|
5
|
+
import { PoolPartialConfig } from "./types";
|
|
8
6
|
/**
|
|
9
7
|
* The main entry point for Pgtx.
|
|
10
8
|
* Manages a connection pool and provides high-level API for queries and transactions.
|
|
@@ -42,13 +40,11 @@ type PoolParams = ConnectionParams & {
|
|
|
42
40
|
*/
|
|
43
41
|
export declare class Pool {
|
|
44
42
|
private _available;
|
|
45
|
-
private
|
|
46
|
-
private _max;
|
|
43
|
+
private config;
|
|
47
44
|
private _total;
|
|
48
45
|
private _waiting;
|
|
49
|
-
private
|
|
50
|
-
|
|
51
|
-
constructor(params: PoolParams);
|
|
46
|
+
private _isOpened;
|
|
47
|
+
constructor(config: PoolPartialConfig);
|
|
52
48
|
/**
|
|
53
49
|
* Acquires a dedicated connection from the pool.
|
|
54
50
|
*
|
|
@@ -186,7 +182,7 @@ export declare class Pool {
|
|
|
186
182
|
* await pool.notify('events', 'hello')
|
|
187
183
|
* ```
|
|
188
184
|
*/
|
|
189
|
-
notify(channelName: string, payload?: string): Future<
|
|
185
|
+
notify(channelName: string, payload?: string): Future<void, PostgresError>;
|
|
190
186
|
/**
|
|
191
187
|
* Asynchronously subscribes to pub/sub events on a specific PostgreSQL channel (LISTEN).
|
|
192
188
|
*
|
|
@@ -211,7 +207,7 @@ export declare class Pool {
|
|
|
211
207
|
* // When the subscription is no longer needed (e.g., during teardown or server stop):
|
|
212
208
|
* await unlisten(); // The socket cleanly issues UNLISTEN and returns to the pool of free connections.
|
|
213
209
|
*/
|
|
214
|
-
listen(channel: string, callback: (payload: string) => void): Future<() =>
|
|
210
|
+
listen(channel: string, callback: (payload: string) => void): Future<() => Future<void, PostgresError>, PostgresError>;
|
|
215
211
|
/**
|
|
216
212
|
* Number of available (idle) connections in the pool.
|
|
217
213
|
*/
|
|
@@ -234,5 +230,4 @@ export declare class Pool {
|
|
|
234
230
|
*/
|
|
235
231
|
close(): void;
|
|
236
232
|
}
|
|
237
|
-
export {};
|
|
238
233
|
//# sourceMappingURL=pool.d.ts.map
|
package/dist/pool.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pool.d.ts","sourceRoot":"","sources":["../src/pool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,
|
|
1
|
+
{"version":3,"file":"pool.d.ts","sourceRoot":"","sources":["../src/pool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AACzC,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAE3C,OAAO,EAAS,MAAM,EAAM,MAAM,eAAe,CAAC;AAClD,OAAO,EAAiB,aAAa,EAAE,MAAM,SAAS,CAAC;AACvD,OAAO,EAAc,iBAAiB,EAAU,MAAM,SAAS,CAAC;AAGhE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,qBAAa,IAAI;IACb,OAAO,CAAC,UAAU,CAAuB;IACzC,OAAO,CAAC,MAAM,CAAY;IAC1B,OAAO,CAAC,MAAM,CAAI;IAClB,OAAO,CAAC,QAAQ,CAAsB;IACtC,OAAO,CAAC,SAAS,CAAO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