@m2k-5f/pgtx 2.2.0 → 2.3.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/README.md +36 -17
- package/dist/clauses/abstract.clause.js +1 -5
- package/dist/clauses/array.clause.js +3 -7
- package/dist/clauses/empty.clause.js +3 -7
- package/dist/clauses/exclude.clause.js +2 -6
- package/dist/clauses/fragment.clause.js +4 -8
- package/dist/clauses/iden.caluse.js +2 -6
- package/dist/clauses/index.js +9 -25
- package/dist/clauses/insert.clause.js +2 -6
- package/dist/clauses/literal.clause.js +2 -6
- package/dist/clauses/update.clause.js +2 -6
- package/dist/clauses/where.clause.js +2 -6
- package/dist/connection.d.ts +11 -7
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +108 -111
- package/dist/error.d.ts +1 -1
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +2 -6
- package/dist/index.js +24 -37
- package/dist/pool.d.ts +9 -7
- package/dist/pool.d.ts.map +1 -1
- package/dist/pool.js +23 -38
- package/dist/protocol/connection-request-writer.js +13 -18
- package/dist/protocol/connection-response-reader.js +4 -9
- package/dist/protocol/constants.js +4 -7
- package/dist/protocol/socket-authorization.d.ts +5 -1
- package/dist/protocol/socket-authorization.d.ts.map +1 -1
- package/dist/protocol/socket-authorization.js +38 -34
- package/dist/protocol/socket-connector.js +3 -7
- package/dist/query.js +2 -6
- package/dist/queue.js +1 -5
- package/dist/security/md5.js +4 -8
- package/dist/security/sasl.js +7 -11
- package/dist/transaction.d.ts +7 -5
- package/dist/transaction.d.ts.map +1 -1
- package/dist/transaction.js +18 -26
- package/dist/types.js +0 -2
- package/dist/utils/template-compiler.js +3 -6
- package/dist/utils/value-parser.js +2 -7
- package/package.json +5 -2
package/dist/connection.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
const
|
|
1
|
+
import { nextTick } from "process";
|
|
2
|
+
import { ConnectionRequestWriter } from "./protocol/connection-request-writer";
|
|
3
|
+
import { createAuthorizedSocket } from "./protocol/socket-authorization";
|
|
4
|
+
import { ResponseTypes } from "./protocol/constants";
|
|
5
|
+
import { parseRowValues } from "./utils/value-parser";
|
|
6
|
+
import { compileSqlTemplate } from "./utils/template-compiler";
|
|
7
|
+
import { Transaction } from "./transaction";
|
|
8
|
+
import { SocketConnector } from "./protocol/socket-connector";
|
|
9
|
+
import { Queue } from "./queue";
|
|
10
|
+
import { Query, QueryState } from "./query";
|
|
11
|
+
import { sql } from ".";
|
|
12
|
+
import { Begin, Future, Resolve } from 'fluent-future';
|
|
13
|
+
import { PostgresError } from "./error";
|
|
14
|
+
const ErrConnectionClosed = new PostgresError("Connection is closed", 'connection_closed', "", "ERROR");
|
|
15
|
+
const ErrConnectionReconnecring = new PostgresError("Connection are reconnecting", "connection_reconnecting", "", "ERROR");
|
|
16
16
|
/**
|
|
17
17
|
* Represents a single dedicated connection to the PostgreSQL database.
|
|
18
18
|
*
|
|
@@ -43,15 +43,19 @@ const ErrConnectionDead = new Error("Connection is Dead");
|
|
|
43
43
|
* conn.close()
|
|
44
44
|
* ```
|
|
45
45
|
*/
|
|
46
|
-
class Connection {
|
|
46
|
+
export class Connection {
|
|
47
47
|
_nextStatement() {
|
|
48
48
|
return `s-${this._stmtCounter++}`;
|
|
49
49
|
}
|
|
50
|
+
_checkOpened() {
|
|
51
|
+
if (!this.isOpened)
|
|
52
|
+
throw ErrConnectionClosed;
|
|
53
|
+
}
|
|
50
54
|
constructor(socket, writer, logLevel, params) {
|
|
51
55
|
this._isFlushing = false;
|
|
52
56
|
this._isOpened = true;
|
|
53
57
|
this._isReconnecting = false;
|
|
54
|
-
this._pipelinesQueue = new
|
|
58
|
+
this._pipelinesQueue = new Queue();
|
|
55
59
|
this._described = new Map();
|
|
56
60
|
this._describingPending = new Set();
|
|
57
61
|
this._parsed = new Map();
|
|
@@ -60,10 +64,7 @@ class Connection {
|
|
|
60
64
|
this._stmtCounter = 0;
|
|
61
65
|
this.params = params;
|
|
62
66
|
this._logLevel = logLevel;
|
|
63
|
-
this._socket = new
|
|
64
|
-
this._isReconnecting = true;
|
|
65
|
-
this._rejectPipeline(ErrConnectionDead);
|
|
66
|
-
});
|
|
67
|
+
this._socket = new SocketConnector(socket, (type, reader) => this._handlePacket(type, reader), (err) => this._registerReconnect());
|
|
67
68
|
this._writer = writer;
|
|
68
69
|
}
|
|
69
70
|
/**
|
|
@@ -84,10 +85,10 @@ class Connection {
|
|
|
84
85
|
* })
|
|
85
86
|
* ```
|
|
86
87
|
*/
|
|
87
|
-
static
|
|
88
|
-
const writer =
|
|
89
|
-
|
|
90
|
-
|
|
88
|
+
static new(params) {
|
|
89
|
+
const writer = ConnectionRequestWriter.new();
|
|
90
|
+
return createAuthorizedSocket(writer, params)
|
|
91
|
+
.andThen(socket => Resolve(new Connection(socket, writer, params.logLevel || 'error', params)));
|
|
91
92
|
}
|
|
92
93
|
/**
|
|
93
94
|
* Executes a query using tagged template literals.
|
|
@@ -114,16 +115,19 @@ class Connection {
|
|
|
114
115
|
* ```
|
|
115
116
|
*/
|
|
116
117
|
query(templates, ...params) {
|
|
118
|
+
this._checkOpened();
|
|
117
119
|
this._registerFlush();
|
|
118
|
-
|
|
119
|
-
throw ErrConnectionDead;
|
|
120
|
-
const { text, args } = (0, template_compiler_1.compileSqlTemplate)({ templates, args: params });
|
|
120
|
+
const { text, args } = compileSqlTemplate({ templates, args: params });
|
|
121
121
|
if (this._logLevel === 'query') {
|
|
122
122
|
console.log(`\nQUERY: ${text}\n${args.length !== 0 ? `ARGUMENTS: [${args}]\n` : ""}`);
|
|
123
123
|
}
|
|
124
124
|
const query = this._createQuery(text, args);
|
|
125
125
|
this._writeQuery(query);
|
|
126
|
-
return query.promise
|
|
126
|
+
return Future.of(query.promise, error => {
|
|
127
|
+
if (error instanceof PostgresError)
|
|
128
|
+
return error;
|
|
129
|
+
return new PostgresError(error.message);
|
|
130
|
+
});
|
|
127
131
|
}
|
|
128
132
|
/**
|
|
129
133
|
* Starts a managed transaction on this connection.
|
|
@@ -146,22 +150,20 @@ class Connection {
|
|
|
146
150
|
* })
|
|
147
151
|
* ```
|
|
148
152
|
*/
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
153
|
+
begin(txCallback) {
|
|
154
|
+
this._checkOpened();
|
|
155
|
+
const tx = new Transaction(this);
|
|
156
|
+
return Begin()
|
|
157
|
+
.andThen(() => tx.query `BEGIN`)
|
|
158
|
+
.andThen(() => Future.of(txCallback(tx))
|
|
159
|
+
.tap(() => {
|
|
156
160
|
if (tx.isActive)
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
catch (err) {
|
|
161
|
+
return tx.commit();
|
|
162
|
+
})
|
|
163
|
+
.tapErr(() => {
|
|
161
164
|
if (tx.isActive)
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
165
|
+
return tx.rollback();
|
|
166
|
+
}));
|
|
165
167
|
}
|
|
166
168
|
/**
|
|
167
169
|
* Sends an asynchronous notification to a channel via `pg_notify`.
|
|
@@ -175,6 +177,7 @@ class Connection {
|
|
|
175
177
|
* ```
|
|
176
178
|
*/
|
|
177
179
|
notify(channelName, payload = "") {
|
|
180
|
+
this._checkOpened();
|
|
178
181
|
return this.query `select pg_notify(${channelName}, ${payload})`;
|
|
179
182
|
}
|
|
180
183
|
/**
|
|
@@ -189,12 +192,13 @@ class Connection {
|
|
|
189
192
|
* ```
|
|
190
193
|
*/
|
|
191
194
|
listen(channelName, callback) {
|
|
195
|
+
this._checkOpened();
|
|
192
196
|
if (!this._listeningCallbacks.has(channelName)) {
|
|
193
197
|
this._listeningCallbacks.set(channelName, new Set());
|
|
194
198
|
}
|
|
195
199
|
const callbackSet = this._listeningCallbacks.get(channelName);
|
|
196
200
|
callbackSet.add(callback);
|
|
197
|
-
return this.query `listen ${
|
|
201
|
+
return this.query `listen ${sql.ident(channelName)};`;
|
|
198
202
|
}
|
|
199
203
|
/**
|
|
200
204
|
* Unsubscribes a callback. Sends `UNLISTEN` if no callbacks remain for the channel.
|
|
@@ -208,61 +212,62 @@ class Connection {
|
|
|
208
212
|
* ```
|
|
209
213
|
*/
|
|
210
214
|
unlisten(channelName, callback) {
|
|
215
|
+
this._checkOpened();
|
|
211
216
|
if (!this._listeningCallbacks.has(channelName)) {
|
|
212
|
-
return
|
|
217
|
+
return Resolve([]);
|
|
213
218
|
}
|
|
214
219
|
const callbackSet = this._listeningCallbacks.get(channelName);
|
|
215
220
|
callbackSet.delete(callback);
|
|
216
221
|
if (callbackSet.size === 0) {
|
|
217
222
|
this._listeningCallbacks.delete(channelName);
|
|
218
|
-
return this.query `unlisten ${
|
|
223
|
+
return this.query `unlisten ${sql.ident(channelName)};`;
|
|
219
224
|
}
|
|
220
|
-
return
|
|
225
|
+
return Resolve([]);
|
|
221
226
|
}
|
|
222
227
|
_createQuery(text, args) {
|
|
223
228
|
if (this._parsed.has(text)) {
|
|
224
229
|
const statementName = this._parsed.get(text);
|
|
225
230
|
if (this._described.has(statementName)) {
|
|
226
231
|
const columns = this._described.get(statementName);
|
|
227
|
-
return new
|
|
232
|
+
return new Query(text, args, QueryState.Executing, statementName, columns);
|
|
228
233
|
}
|
|
229
234
|
else {
|
|
230
235
|
if (this._describingPending.has(statementName)) {
|
|
231
|
-
return new
|
|
236
|
+
return new Query(text, args, QueryState.Executing, statementName);
|
|
232
237
|
}
|
|
233
238
|
else {
|
|
234
239
|
this._describingPending.add(statementName);
|
|
235
|
-
return new
|
|
240
|
+
return new Query(text, args, QueryState.Describing, statementName);
|
|
236
241
|
}
|
|
237
242
|
}
|
|
238
243
|
}
|
|
239
244
|
else {
|
|
240
245
|
if (this._parsingPending.has(text)) {
|
|
241
246
|
const statementName = this._parsingPending.get(text);
|
|
242
|
-
return new
|
|
247
|
+
return new Query(text, args, QueryState.Describing, statementName);
|
|
243
248
|
}
|
|
244
249
|
else {
|
|
245
250
|
const statementName = this._nextStatement();
|
|
246
251
|
this._parsingPending.set(text, statementName);
|
|
247
|
-
return new
|
|
252
|
+
return new Query(text, args, QueryState.Parsing, statementName);
|
|
248
253
|
}
|
|
249
254
|
}
|
|
250
255
|
}
|
|
251
256
|
_writeQuery(query) {
|
|
252
|
-
if (query.state ===
|
|
257
|
+
if (query.state === QueryState.Parsing) {
|
|
253
258
|
this._writer
|
|
254
259
|
.writeParse(query.statementName, query.text)
|
|
255
260
|
.writeDescribe(query.statementName)
|
|
256
261
|
.writeBind("", query.statementName, query.args)
|
|
257
262
|
.writeExecute("");
|
|
258
263
|
}
|
|
259
|
-
if (query.state ===
|
|
264
|
+
if (query.state === QueryState.Describing) {
|
|
260
265
|
this._writer
|
|
261
266
|
.writeDescribe(query.statementName)
|
|
262
267
|
.writeBind("", query.statementName, query.args)
|
|
263
268
|
.writeExecute("");
|
|
264
269
|
}
|
|
265
|
-
if (query.state ===
|
|
270
|
+
if (query.state === QueryState.Executing) {
|
|
266
271
|
this._writer
|
|
267
272
|
.writeBind("", query.statementName, query.args)
|
|
268
273
|
.writeExecute("");
|
|
@@ -272,21 +277,21 @@ class Connection {
|
|
|
272
277
|
_registerFlush() {
|
|
273
278
|
if (!this._isFlushing) {
|
|
274
279
|
this._isFlushing = true;
|
|
275
|
-
this._pipelinesQueue.push(new
|
|
276
|
-
|
|
280
|
+
this._pipelinesQueue.push(new Queue());
|
|
281
|
+
nextTick(() => {
|
|
277
282
|
this._flush();
|
|
278
283
|
});
|
|
279
284
|
}
|
|
280
285
|
}
|
|
281
286
|
_flush() {
|
|
282
287
|
if (!this._isOpened) {
|
|
283
|
-
this._rejectPipeline(
|
|
288
|
+
this._rejectPipeline(ErrConnectionClosed);
|
|
284
289
|
return;
|
|
285
290
|
}
|
|
286
291
|
if (this._isReconnecting) {
|
|
287
|
-
this._reconnect().then(
|
|
292
|
+
this._reconnect().then(() => {
|
|
288
293
|
this._isFlushing = false;
|
|
289
|
-
|
|
294
|
+
this._socket.write(this._writer.writeSync());
|
|
290
295
|
this._writer.clear();
|
|
291
296
|
});
|
|
292
297
|
}
|
|
@@ -296,35 +301,31 @@ class Connection {
|
|
|
296
301
|
this._writer.clear();
|
|
297
302
|
}
|
|
298
303
|
}
|
|
299
|
-
|
|
304
|
+
_registerReconnect() {
|
|
305
|
+
this._isReconnecting = true;
|
|
306
|
+
this._rejectPipeline(ErrConnectionReconnecring);
|
|
307
|
+
}
|
|
308
|
+
_resetConnectionState(cause) {
|
|
300
309
|
this._parsed.clear();
|
|
301
310
|
this._described.clear();
|
|
302
311
|
this._describingPending.clear();
|
|
303
312
|
this._parsingPending.clear();
|
|
304
313
|
this._writer.clear();
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
this.
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
this._isReconnecting = true;
|
|
315
|
-
this._rejectPipeline(ErrConnectionDead);
|
|
316
|
-
});
|
|
317
|
-
this._socket = connector;
|
|
318
|
-
this._restoreSubscriptions();
|
|
319
|
-
this._isReconnecting = false;
|
|
320
|
-
return connector;
|
|
321
|
-
});
|
|
314
|
+
this._rejectPipeline(cause);
|
|
315
|
+
}
|
|
316
|
+
async _reconnect() {
|
|
317
|
+
this._resetConnectionState(ErrConnectionReconnecring);
|
|
318
|
+
const socket = await createAuthorizedSocket(ConnectionRequestWriter.new(), this.params);
|
|
319
|
+
const connector = new SocketConnector(socket, (type, reader) => this._handlePacket(type, reader), (err) => this._registerReconnect());
|
|
320
|
+
this._socket = connector;
|
|
321
|
+
void this._restoreSubscriptions();
|
|
322
|
+
this._isReconnecting = false;
|
|
322
323
|
}
|
|
323
324
|
_restoreSubscriptions() {
|
|
324
325
|
if (this._listeningCallbacks.size === 0)
|
|
325
326
|
return Promise.resolve();
|
|
326
327
|
const promises = Array.from(this._listeningCallbacks.keys()).map(channel => {
|
|
327
|
-
return this.query `LISTEN ${
|
|
328
|
+
return this.query `LISTEN ${sql.ident(channel)};`;
|
|
328
329
|
});
|
|
329
330
|
return Promise.all(promises);
|
|
330
331
|
}
|
|
@@ -342,76 +343,76 @@ class Connection {
|
|
|
342
343
|
}
|
|
343
344
|
_handlePacket(type, reader) {
|
|
344
345
|
switch (type) {
|
|
345
|
-
case
|
|
346
|
+
case ResponseTypes.ParseComplete:
|
|
346
347
|
{
|
|
347
348
|
reader.readParseComplete();
|
|
348
349
|
const query = this._getCurrentQuery();
|
|
349
350
|
this._parsingPending.delete(query.text);
|
|
350
351
|
this._parsed.set(query.text, query.statementName);
|
|
351
|
-
query.state =
|
|
352
|
+
query.state = QueryState.Describing;
|
|
352
353
|
}
|
|
353
354
|
break;
|
|
354
|
-
case
|
|
355
|
+
case ResponseTypes.BindComplete:
|
|
355
356
|
{
|
|
356
357
|
reader.readBindComplete();
|
|
357
358
|
}
|
|
358
359
|
break;
|
|
359
|
-
case
|
|
360
|
+
case ResponseTypes.CloseComplete:
|
|
360
361
|
{
|
|
361
362
|
reader.readBindComplete();
|
|
362
363
|
}
|
|
363
364
|
break;
|
|
364
|
-
case
|
|
365
|
+
case ResponseTypes.ParameterDescription:
|
|
365
366
|
{
|
|
366
367
|
reader.readParameterDescription();
|
|
367
368
|
}
|
|
368
369
|
break;
|
|
369
|
-
case
|
|
370
|
+
case ResponseTypes.NoData:
|
|
370
371
|
{
|
|
371
372
|
reader.readNoData();
|
|
372
373
|
const query = this._getCurrentQuery();
|
|
373
374
|
this._describingPending.delete(query.statementName);
|
|
374
375
|
this._described.set(query.statementName, []);
|
|
375
|
-
query.state =
|
|
376
|
+
query.state = QueryState.Executing;
|
|
376
377
|
}
|
|
377
378
|
break;
|
|
378
|
-
case
|
|
379
|
+
case ResponseTypes.RowDescription:
|
|
379
380
|
{
|
|
380
381
|
const columns = reader.readRowDescription();
|
|
381
382
|
const query = this._getCurrentQuery();
|
|
382
383
|
this._describingPending.delete(query.statementName);
|
|
383
384
|
this._described.set(query.statementName, columns);
|
|
384
|
-
query.state =
|
|
385
|
+
query.state = QueryState.Executing;
|
|
385
386
|
query.columns = columns;
|
|
386
387
|
}
|
|
387
388
|
break;
|
|
388
|
-
case
|
|
389
|
+
case ResponseTypes.DataRow:
|
|
389
390
|
{
|
|
390
391
|
const query = this._getCurrentQuery();
|
|
391
392
|
query.rows.push(reader.readDataRow());
|
|
392
393
|
}
|
|
393
394
|
break;
|
|
394
|
-
case
|
|
395
|
+
case ResponseTypes.ComandComplete:
|
|
395
396
|
{
|
|
396
397
|
reader.readCommandComplete();
|
|
397
398
|
if (this._pipelinesQueue.isFree) {
|
|
398
|
-
this.
|
|
399
|
+
this.close();
|
|
399
400
|
break;
|
|
400
401
|
}
|
|
401
402
|
const query = this._getCurrentQuery();
|
|
402
403
|
if (!query) {
|
|
403
|
-
this.
|
|
404
|
+
this.close();
|
|
404
405
|
break;
|
|
405
406
|
}
|
|
406
407
|
if (!query.columns) {
|
|
407
408
|
query.columns = this._described.get(query.statementName);
|
|
408
409
|
}
|
|
409
|
-
query.state =
|
|
410
|
+
query.state = QueryState.Completed;
|
|
410
411
|
this._pipelinesQueue.get().next();
|
|
411
|
-
query.resolve(
|
|
412
|
+
query.resolve(parseRowValues(query.columns, query.rows));
|
|
412
413
|
}
|
|
413
414
|
break;
|
|
414
|
-
case
|
|
415
|
+
case ResponseTypes.ErrorResponse:
|
|
415
416
|
{
|
|
416
417
|
const error = reader.readErrorResponse();
|
|
417
418
|
if (this._logLevel === 'error' || this._logLevel === 'notice' || this._logLevel === 'query') {
|
|
@@ -419,18 +420,18 @@ class Connection {
|
|
|
419
420
|
}
|
|
420
421
|
const query = this._getCurrentQuery();
|
|
421
422
|
switch (query.state) {
|
|
422
|
-
case
|
|
423
|
+
case QueryState.Parsing:
|
|
423
424
|
this._parsingPending.delete(query.text);
|
|
424
425
|
break;
|
|
425
|
-
case
|
|
426
|
+
case QueryState.Describing:
|
|
426
427
|
this._describingPending.delete(query.statementName);
|
|
427
428
|
break;
|
|
428
429
|
}
|
|
429
|
-
query.state =
|
|
430
|
+
query.state = QueryState.Failed;
|
|
430
431
|
this._rejectPipeline(error);
|
|
431
432
|
}
|
|
432
433
|
break;
|
|
433
|
-
case
|
|
434
|
+
case ResponseTypes.ReadyForQuery:
|
|
434
435
|
{
|
|
435
436
|
reader.readReadyForQuery();
|
|
436
437
|
const pipeline = this._pipelinesQueue.get();
|
|
@@ -439,7 +440,7 @@ class Connection {
|
|
|
439
440
|
}
|
|
440
441
|
}
|
|
441
442
|
break;
|
|
442
|
-
case
|
|
443
|
+
case ResponseTypes.Notice:
|
|
443
444
|
{
|
|
444
445
|
const message = reader.readErrorResponse();
|
|
445
446
|
if (this._logLevel === 'notice' || this._logLevel === 'query') {
|
|
@@ -447,7 +448,7 @@ class Connection {
|
|
|
447
448
|
}
|
|
448
449
|
}
|
|
449
450
|
break;
|
|
450
|
-
case
|
|
451
|
+
case ResponseTypes.NotificationResponse:
|
|
451
452
|
{
|
|
452
453
|
const { name, payload } = reader.readNotificationResponse();
|
|
453
454
|
const callbackSet = this._listeningCallbacks.get(name);
|
|
@@ -466,14 +467,6 @@ class Connection {
|
|
|
466
467
|
get isOpened() {
|
|
467
468
|
return this._isOpened;
|
|
468
469
|
}
|
|
469
|
-
_destroyConnection() {
|
|
470
|
-
this._isOpened = false;
|
|
471
|
-
this._socket.destroy();
|
|
472
|
-
while (this._pipelinesQueue.hasMore) {
|
|
473
|
-
this._rejectPipeline(ErrConnectionDead);
|
|
474
|
-
this._pipelinesQueue.next();
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
470
|
/**
|
|
478
471
|
* Closes the connection immediately.
|
|
479
472
|
* All pending queries will be rejected with an error.
|
|
@@ -485,7 +478,11 @@ class Connection {
|
|
|
485
478
|
* ```
|
|
486
479
|
*/
|
|
487
480
|
close() {
|
|
488
|
-
this.
|
|
481
|
+
this._isOpened = false;
|
|
482
|
+
this._socket.destroy();
|
|
483
|
+
while (this._pipelinesQueue.hasMore) {
|
|
484
|
+
this._rejectPipeline(ErrConnectionClosed);
|
|
485
|
+
this._pipelinesQueue.next();
|
|
486
|
+
}
|
|
489
487
|
}
|
|
490
488
|
}
|
|
491
|
-
exports.Connection = Connection;
|
package/dist/error.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export declare class PostgresError extends Error {
|
|
|
8
8
|
position: string;
|
|
9
9
|
dataType: string;
|
|
10
10
|
constraint: string;
|
|
11
|
-
constructor(message: string, code
|
|
11
|
+
constructor(message: string, code?: string, detail?: string, severity?: string, where?: string, hint?: string, position?: string, dataType?: string, constraint?: string);
|
|
12
12
|
get isParseError(): boolean;
|
|
13
13
|
get isDeadlock(): boolean;
|
|
14
14
|
get isConstraintViolation(): boolean;
|
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,MAAM;IACd,QAAQ,EAAE,MAAM;IAChB,KAAK,EAAE,MAAM;IACb,IAAI,EAAE,MAAM;IACZ,QAAQ,EAAE,MAAM;IAChB,QAAQ,EAAE,MAAM;IAChB,UAAU,EAAE,MAAM;gBART,OAAO,EAAE,MAAM,EACxB,IAAI,
|
|
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,MAAM;IACd,QAAQ,EAAE,MAAM;IAChB,KAAK,EAAE,MAAM;IACb,IAAI,EAAE,MAAM;IACZ,QAAQ,EAAE,MAAM;IAChB,QAAQ,EAAE,MAAM;IAChB,UAAU,EAAE,MAAM;gBART,OAAO,EAAE,MAAM,EACxB,IAAI,GAAE,MAAqB,EAC3B,MAAM,GAAE,MAAW,EACnB,QAAQ,GAAE,MAAW,EACrB,KAAK,GAAE,MAAW,EAClB,IAAI,GAAE,MAAW,EACjB,QAAQ,GAAE,MAAW,EACrB,QAAQ,GAAE,MAAW,EACrB,UAAU,GAAE,MAAW;IAQlC,IAAI,YAAY,IAAI,OAAO,CAE1B;IAID,IAAI,UAAU,IAAI,OAAO,CAExB;IAID,IAAI,qBAAqB,IAAI,OAAO,CAEnC;IAGD,IAAI,SAAS,IAAI,OAAO,CAEvB;IAGD,IAAI,mBAAmB,IAAI,OAAO,CAEjC;CACJ"}
|
package/dist/error.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.PostgresError = void 0;
|
|
4
|
-
class PostgresError extends Error {
|
|
5
|
-
constructor(message, code, detail, severity, where, hint, position, dataType, constraint) {
|
|
1
|
+
export class PostgresError extends Error {
|
|
2
|
+
constructor(message, code = 'undeclared', detail = '', severity = '', where = '', hint = '', position = '', dataType = '', constraint = '') {
|
|
6
3
|
super(message);
|
|
7
4
|
this.message = message;
|
|
8
5
|
this.code = code;
|
|
@@ -34,4 +31,3 @@ class PostgresError extends Error {
|
|
|
34
31
|
return this.code.startsWith('08') || this.code.startsWith('57') && this.code !== '57014';
|
|
35
32
|
}
|
|
36
33
|
}
|
|
37
|
-
exports.PostgresError = PostgresError;
|
package/dist/index.js
CHANGED
|
@@ -1,34 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.setTypeParser = exports.Pool = exports.Transaction = exports.Connection = exports.sql = void 0;
|
|
18
|
-
const pool_1 = require("./pool");
|
|
19
|
-
Object.defineProperty(exports, "Pool", { enumerable: true, get: function () { return pool_1.Pool; } });
|
|
20
|
-
const clauses_1 = require("./clauses");
|
|
21
|
-
const connection_1 = require("./connection");
|
|
22
|
-
Object.defineProperty(exports, "Connection", { enumerable: true, get: function () { return connection_1.Connection; } });
|
|
23
|
-
const transaction_1 = require("./transaction");
|
|
24
|
-
Object.defineProperty(exports, "Transaction", { enumerable: true, get: function () { return transaction_1.Transaction; } });
|
|
25
|
-
const value_parser_1 = require("./utils/value-parser");
|
|
26
|
-
Object.defineProperty(exports, "setTypeParser", { enumerable: true, get: function () { return value_parser_1.setTypeParser; } });
|
|
1
|
+
import { Pool as PgtxPool } from "./pool";
|
|
2
|
+
import { IdentifierClause, InsertClause, ArrayClause, UpdateClause, ExcludeUpdateClause, emptyClause, WhereClause, LiteralClause, FragmentClause, } from './clauses';
|
|
3
|
+
import { Connection } from "./connection";
|
|
4
|
+
import { Transaction } from "./transaction";
|
|
5
|
+
import { setTypeParser } from "./utils/value-parser";
|
|
27
6
|
/**
|
|
28
7
|
* Core SQL tagging utility for Pgtx.
|
|
29
8
|
* Provides type-safe helpers for building dynamic queries with recursive support.
|
|
30
9
|
*/
|
|
31
|
-
|
|
10
|
+
export const sql = {
|
|
32
11
|
/**
|
|
33
12
|
* Creates a VALUES clause for INSERT queries.
|
|
34
13
|
* Supports single objects and arrays of objects.
|
|
@@ -41,7 +20,7 @@ exports.sql = {
|
|
|
41
20
|
* sql.insert([{ id: 1 }, { id: 2 }])
|
|
42
21
|
* // Result: (id) VALUES ($1), ($2)
|
|
43
22
|
*/
|
|
44
|
-
insert:
|
|
23
|
+
insert: InsertClause.create,
|
|
45
24
|
/**
|
|
46
25
|
* Generates a SET clause for UPDATE queries from a JavaScript object.
|
|
47
26
|
*
|
|
@@ -49,7 +28,7 @@ exports.sql = {
|
|
|
49
28
|
* sql.update({ status: 'active', updated_at: new Date() })
|
|
50
29
|
* // Result: status = $1, updated_at = $2
|
|
51
30
|
*/
|
|
52
|
-
update:
|
|
31
|
+
update: UpdateClause.create,
|
|
53
32
|
/**
|
|
54
33
|
* Generates an assignment list for ON CONFLICT DO UPDATE using the EXCLUDED table.
|
|
55
34
|
*
|
|
@@ -57,7 +36,7 @@ exports.sql = {
|
|
|
57
36
|
* sql`INSERT INTO users ${sql.insert(data)} ON CONFLICT (id) DO UPDATE SET ${sql.excluded(['name', 'email'])}`
|
|
58
37
|
* // Result: name = EXCLUDED.name, email = EXCLUDED.email
|
|
59
38
|
*/
|
|
60
|
-
excluded:
|
|
39
|
+
excluded: ExcludeUpdateClause.create,
|
|
61
40
|
/**
|
|
62
41
|
* Represents a safe empty SQL fragment.
|
|
63
42
|
* Useful for dynamic query building when a condition or list might be optional.
|
|
@@ -67,7 +46,7 @@ exports.sql = {
|
|
|
67
46
|
* sql`SELECT * FROM users ${filters.length ? sql.fragment`WHERE ...` : sql.empty}`
|
|
68
47
|
* // Result: SELECT * FROM users
|
|
69
48
|
*/
|
|
70
|
-
empty:
|
|
49
|
+
empty: emptyClause,
|
|
71
50
|
/**
|
|
72
51
|
* Generates a list of conditions for a WHERE clause from a JavaScript object.
|
|
73
52
|
* Works similarly to sql.update, but uses ' AND ' as a separator instead of a comma.
|
|
@@ -80,7 +59,7 @@ exports.sql = {
|
|
|
80
59
|
* sql`DELETE FROM tasks WHERE ${sql.where({ id: 10, user_id: 5 })}`
|
|
81
60
|
* // Result: id = $1 AND user_id = $2
|
|
82
61
|
*/
|
|
83
|
-
where:
|
|
62
|
+
where: WhereClause.create,
|
|
84
63
|
/**
|
|
85
64
|
* Safely escapes SQL identifiers (table or column names) using double quotes.
|
|
86
65
|
*
|
|
@@ -92,7 +71,7 @@ exports.sql = {
|
|
|
92
71
|
* sql.ident('table.column')
|
|
93
72
|
* // Result: "table.column"
|
|
94
73
|
*/
|
|
95
|
-
ident:
|
|
74
|
+
ident: IdentifierClause.create,
|
|
96
75
|
/**
|
|
97
76
|
* Injects raw, unescaped SQL strings.
|
|
98
77
|
* ⚠️ Use with caution to prevent SQL injection!
|
|
@@ -101,7 +80,7 @@ exports.sql = {
|
|
|
101
80
|
* sql.literal('DESC')
|
|
102
81
|
* // Result: DESC
|
|
103
82
|
*/
|
|
104
|
-
literal:
|
|
83
|
+
literal: LiteralClause.create,
|
|
105
84
|
/**
|
|
106
85
|
* Creates a reusable, recursive SQL fragment.
|
|
107
86
|
* Fragments can be nested within each other; argument numbering is handled automatically.
|
|
@@ -111,7 +90,7 @@ exports.sql = {
|
|
|
111
90
|
* sql`SELECT * FROM users WHERE ${filter} AND status = ${'active'}`
|
|
112
91
|
* // Result: SELECT * FROM users WHERE age > $1 AND status = $2
|
|
113
92
|
*/
|
|
114
|
-
fragment:
|
|
93
|
+
fragment: FragmentClause.create,
|
|
115
94
|
/**
|
|
116
95
|
* Formats an array for dynamic lists (IN clauses, column lists, or joined conditions).
|
|
117
96
|
* Supports recursive Clauses (fragments, idents) within the array.
|
|
@@ -132,6 +111,14 @@ exports.sql = {
|
|
|
132
111
|
* sql`SELECT ${sql.array([sql.ident('id'), sql.ident('name')])}`
|
|
133
112
|
* // Result: SELECT "id", "name"
|
|
134
113
|
*/
|
|
135
|
-
array:
|
|
114
|
+
array: ArrayClause.create,
|
|
136
115
|
};
|
|
137
|
-
|
|
116
|
+
/**
|
|
117
|
+
* Main Pgtx Connection Pool.
|
|
118
|
+
* Manages connections, transactions (including SAVEPOINTs), and prepared statements.
|
|
119
|
+
*/
|
|
120
|
+
export { Connection as Connection };
|
|
121
|
+
export { Transaction as Transaction };
|
|
122
|
+
export { PgtxPool as Pool };
|
|
123
|
+
export { setTypeParser as setTypeParser };
|
|
124
|
+
export * from "./clauses";
|