@m2k-5f/pgtx 2.6.1 → 2.6.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/README.md +237 -487
- package/dist/batch.d.ts +2 -1
- package/dist/batch.d.ts.map +1 -1
- package/dist/batch.js +8 -7
- package/dist/connection.d.ts +39 -150
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +184 -273
- package/dist/pool.d.ts +29 -181
- package/dist/pool.d.ts.map +1 -1
- package/dist/pool.js +49 -180
- package/dist/protocol/connection-response-reader.d.ts +1 -0
- package/dist/protocol/connection-response-reader.d.ts.map +1 -1
- package/dist/protocol/connection-response-reader.js +3 -0
- package/dist/protocol/socket-connector.d.ts +5 -5
- package/dist/protocol/socket-connector.d.ts.map +1 -1
- package/dist/protocol/socket-connector.js +24 -15
- package/dist/query.d.ts +17 -17
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +22 -17
- package/dist/transaction.d.ts +9 -1
- package/dist/transaction.d.ts.map +1 -1
- package/dist/transaction.js +11 -0
- package/dist/types.d.ts +9 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/batch.d.ts
CHANGED
|
@@ -5,10 +5,11 @@ export declare class Batch {
|
|
|
5
5
|
private _queryQueue;
|
|
6
6
|
private _buffer;
|
|
7
7
|
constructor(buffer: ConnectionRequestWriter);
|
|
8
|
+
registerParse(query: PostgresQuery): void;
|
|
8
9
|
registerQuery(query: PostgresQuery): void;
|
|
9
10
|
end(): ConnectionRequestWriter;
|
|
10
11
|
get current(): PostgresQuery;
|
|
11
12
|
next(): void;
|
|
12
|
-
reject(cause: PostgresError):
|
|
13
|
+
reject(cause: PostgresError): number;
|
|
13
14
|
}
|
|
14
15
|
//# sourceMappingURL=batch.d.ts.map
|
package/dist/batch.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"batch.d.ts","sourceRoot":"","sources":["../src/batch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACxC,OAAO,EAAE,uBAAuB,EAAE,MAAM,sCAAsC,CAAC;AAC/E,OAAO,
|
|
1
|
+
{"version":3,"file":"batch.d.ts","sourceRoot":"","sources":["../src/batch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACxC,OAAO,EAAE,uBAAuB,EAAE,MAAM,sCAAsC,CAAC;AAC/E,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAGxC,qBAAa,KAAK;IACd,OAAO,CAAC,WAAW,CAAsB;IACzC,OAAO,CAAC,OAAO,CAAyB;gBAE5B,MAAM,EAAE,uBAAuB;IAM3C,aAAa,CAAC,KAAK,EAAE,aAAa;IAOlC,aAAa,CAAC,KAAK,EAAE,aAAa;IAQlC,GAAG;IAIH,IAAI,OAAO,kBAEV;IAED,IAAI;IAIJ,MAAM,CAAC,KAAK,EAAE,aAAa;CAS9B"}
|
package/dist/batch.js
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
|
-
import { ParseQuery } from "./query";
|
|
2
1
|
import { Queue } from "./queue";
|
|
3
2
|
export class Batch {
|
|
4
3
|
constructor(buffer) {
|
|
5
4
|
this._queryQueue = new Queue();
|
|
6
5
|
this._buffer = buffer;
|
|
7
6
|
}
|
|
7
|
+
registerParse(query) {
|
|
8
|
+
this._buffer
|
|
9
|
+
.writeParse(query.statement, query.text)
|
|
10
|
+
.writeDescribe(query.statement);
|
|
11
|
+
}
|
|
8
12
|
registerQuery(query) {
|
|
9
13
|
this._queryQueue.push(query);
|
|
10
|
-
if (query instanceof ParseQuery) {
|
|
11
|
-
this._buffer
|
|
12
|
-
.writeParse(query.statement, query.text)
|
|
13
|
-
.writeDescribe(query.statement);
|
|
14
|
-
return;
|
|
15
|
-
}
|
|
16
14
|
this._buffer
|
|
17
15
|
.writeBind("", query.statement, query.args)
|
|
18
16
|
.writeExecute("");
|
|
@@ -27,8 +25,11 @@ export class Batch {
|
|
|
27
25
|
this._queryQueue.next();
|
|
28
26
|
}
|
|
29
27
|
reject(cause) {
|
|
28
|
+
let counter = 0;
|
|
30
29
|
while (this._queryQueue.hasMore) {
|
|
30
|
+
counter++;
|
|
31
31
|
this._queryQueue.shift.reject(cause);
|
|
32
32
|
}
|
|
33
|
+
return counter;
|
|
33
34
|
}
|
|
34
35
|
}
|
package/dist/connection.d.ts
CHANGED
|
@@ -1,42 +1,23 @@
|
|
|
1
|
-
import { ConnectionPartialConfig } from "./types";
|
|
1
|
+
import { ConnectionPartialConfig, Row } from "./types";
|
|
2
2
|
import { Transaction } from "./transaction";
|
|
3
3
|
import { Future } from 'fluent-future';
|
|
4
4
|
import { PostgresError } from "./error";
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* Supports:
|
|
9
|
-
* - Tagged template queries with automatic parameter binding
|
|
10
|
-
* - Prepared statements with caching
|
|
11
|
-
* - Transaction management with savepoints
|
|
12
|
-
* - Pipeline execution for concurrent queries
|
|
6
|
+
* A dedicated connection to PostgreSQL: tagged-template queries, prepared
|
|
7
|
+
* statement caching, transactions with savepoints, and pipelined execution.
|
|
13
8
|
*
|
|
14
9
|
* @example
|
|
15
|
-
*
|
|
16
|
-
* const conn = await Connection.new({
|
|
17
|
-
* host: 'localhost',
|
|
18
|
-
* user: 'postgres',
|
|
19
|
-
* password: 'postgres',
|
|
20
|
-
* database: 'test'
|
|
21
|
-
* })
|
|
22
|
-
*
|
|
23
|
-
* // Simple query
|
|
10
|
+
* const conn = await Connection.new({ host: 'localhost', user: 'postgres', password: 'postgres', database: 'test' })
|
|
24
11
|
* const users = await conn.query`SELECT * FROM users WHERE id = ${1}`
|
|
25
|
-
*
|
|
26
|
-
* // Transaction
|
|
27
|
-
* await conn.begin(async tx => {
|
|
28
|
-
* await tx.query`INSERT INTO users ...`
|
|
29
|
-
* })
|
|
30
|
-
*
|
|
31
|
-
* // Close connection
|
|
12
|
+
* await conn.begin(async tx => tx.query`INSERT INTO users ...`)
|
|
32
13
|
* conn.close()
|
|
33
|
-
* ```
|
|
34
14
|
*/
|
|
35
15
|
export declare class Connection {
|
|
36
16
|
private readonly config;
|
|
37
17
|
private _activeBatch;
|
|
38
|
-
private
|
|
39
|
-
private
|
|
18
|
+
private _closing;
|
|
19
|
+
private _closed;
|
|
20
|
+
private _reconnecting;
|
|
40
21
|
private _cachedBuffer;
|
|
41
22
|
private _socket;
|
|
42
23
|
private _batchQueue;
|
|
@@ -47,158 +28,66 @@ export declare class Connection {
|
|
|
47
28
|
private _nextStatement;
|
|
48
29
|
private constructor();
|
|
49
30
|
/**
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
* @param params - Connection parameters
|
|
53
|
-
* @returns A new Connection instance
|
|
54
|
-
* @throws {PostgresError} If authentication fails or connection cannot be established
|
|
55
|
-
*
|
|
56
|
-
* @example
|
|
57
|
-
* ```ts
|
|
58
|
-
* const conn = await Connection.new({
|
|
59
|
-
* host: 'localhost',
|
|
60
|
-
* port: 5432,
|
|
61
|
-
* user: 'postgres',
|
|
62
|
-
* password: 'secret',
|
|
63
|
-
* database: 'myapp'
|
|
64
|
-
* })
|
|
65
|
-
* ```
|
|
31
|
+
* Opens a new connection and authenticates.
|
|
32
|
+
* @throws {PostgresError} if authentication fails or the connection can't be established
|
|
66
33
|
*/
|
|
67
34
|
static new(config: ConnectionPartialConfig): Future<Connection, PostgresError>;
|
|
68
35
|
private _registerBatch;
|
|
69
36
|
private _sync;
|
|
70
37
|
/**
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
* Parameters are automatically bound to `$1, $2, ...` placeholders.
|
|
74
|
-
* Queries with parameters use prepared statements for performance.
|
|
75
|
-
*
|
|
76
|
-
* @param templates - Tagged template string with SQL
|
|
77
|
-
* @param args - Query parameters
|
|
78
|
-
* @returns Array of rows with proper typing
|
|
79
|
-
* @throws {Error} If connection is dead or query fails
|
|
38
|
+
* Runs a query, binding template values as `$1, $2, ...`.
|
|
39
|
+
* Parameterized queries are cached as prepared statements.
|
|
80
40
|
*
|
|
81
41
|
* @example
|
|
82
|
-
*
|
|
83
|
-
* // Simple query
|
|
84
|
-
* const users = await conn.query`SELECT * FROM users`
|
|
85
|
-
*
|
|
86
|
-
* // With parameters
|
|
87
|
-
* const user = await conn.query`SELECT * FROM users WHERE id = ${1}`
|
|
88
|
-
*
|
|
89
|
-
* // With typed result
|
|
90
|
-
* type User = { id: number, name: string }
|
|
91
|
-
* const users = await conn.query<User>`SELECT * FROM users`
|
|
92
|
-
* ```
|
|
42
|
+
* const users = await conn.query<User>`SELECT * FROM users WHERE id = ${1}`
|
|
93
43
|
*/
|
|
94
|
-
query<T extends
|
|
44
|
+
query<T extends Row>(templates: TemplateStringsArray, ...params: any[]): Future<T[], PostgresError>;
|
|
45
|
+
private _performQuery;
|
|
95
46
|
/**
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
* Automatically handles:
|
|
99
|
-
* - `BEGIN` before the callback
|
|
100
|
-
* - `COMMIT` on successful completion
|
|
101
|
-
* - `ROLLBACK` if an error is thrown
|
|
102
|
-
*
|
|
103
|
-
* @param txCallback - Async function that receives a `Transaction` instance
|
|
104
|
-
* @returns The value returned from the callback
|
|
105
|
-
* @throws {Error} If connection is dead or transaction fails
|
|
47
|
+
* Like {@link query}, but for statements that don't return rows (INSERT/UPDATE/DDL/etc).
|
|
106
48
|
*
|
|
107
49
|
* @example
|
|
108
|
-
*
|
|
109
|
-
* const result = await conn.begin(async tx => {
|
|
110
|
-
* await tx.query`INSERT INTO accounts (id, balance) VALUES (1, 100)`
|
|
111
|
-
* await tx.query`UPDATE accounts SET balance = balance - 10 WHERE id = 1`
|
|
112
|
-
* return { success: true }
|
|
113
|
-
* })
|
|
114
|
-
* ```
|
|
50
|
+
* await conn.execute`UPDATE users SET name = ${name} WHERE id = ${id}`
|
|
115
51
|
*/
|
|
116
|
-
|
|
52
|
+
execute(templates: TemplateStringsArray, ...params: any[]): Future<void, PostgresError>;
|
|
53
|
+
private _performExecute;
|
|
117
54
|
/**
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
* @param channelName - The channel identifier
|
|
121
|
-
* @param payload - Optional string data (max 8000 bytes)
|
|
55
|
+
* Runs `txCallback` inside `BEGIN`/`COMMIT`, rolling back on error.
|
|
122
56
|
*
|
|
123
57
|
* @example
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
58
|
+
* await conn.begin(async tx => {
|
|
59
|
+
* await tx.query`UPDATE accounts SET balance = balance - 10 WHERE id = 1`
|
|
60
|
+
* })
|
|
127
61
|
*/
|
|
62
|
+
begin<T>(txCallback: (transaction: Transaction) => Promise<T>): Future<T, unknown>;
|
|
63
|
+
/** Sends a `pg_notify` message on `channelName` (payload ≤ 8000 bytes). */
|
|
128
64
|
notify(channelName: string, payload?: string): Future<void, PostgresError>;
|
|
129
|
-
/**
|
|
130
|
-
* Subscribes a callback to a channel. Sends `LISTEN` on the first subscription.
|
|
131
|
-
*
|
|
132
|
-
* @param channelName - The channel identifier
|
|
133
|
-
* @param callback - Function invoked when a notification arrives
|
|
134
|
-
*
|
|
135
|
-
* @example
|
|
136
|
-
* ```ts
|
|
137
|
-
* await conn.listen('events', data => console.log(data))
|
|
138
|
-
* ```
|
|
139
|
-
*/
|
|
65
|
+
/** Subscribes `callback` to `channelName`, issuing `LISTEN` on first subscription. */
|
|
140
66
|
listen(channelName: string, callback: (payload: string) => void): Future<void, PostgresError>;
|
|
141
|
-
/**
|
|
142
|
-
* Unsubscribes a callback. Sends `UNLISTEN` if no callbacks remain for the channel.
|
|
143
|
-
*
|
|
144
|
-
* @param channelName - The channel identifier
|
|
145
|
-
* @param callback - The registered callback to remove
|
|
146
|
-
*
|
|
147
|
-
* @example
|
|
148
|
-
* ```ts
|
|
149
|
-
* await conn.unlisten('events', callback)
|
|
150
|
-
* ```
|
|
151
|
-
*/
|
|
67
|
+
/** Unsubscribes `callback`, issuing `UNLISTEN` once no callbacks remain. */
|
|
152
68
|
unlisten(channelName: string, callback: (payload: string) => void): Future<void, PostgresError>;
|
|
153
69
|
/**
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
* Data is streamed directly from the PostgreSQL binary network buffer into the Web Streams API
|
|
157
|
-
* (`ReadableStream`), bypassing any intermediate array allocation or row accumulation in the JS heap.
|
|
158
|
-
* This pattern provides a true Zero-Memory Footprint and is ideal for exporting massive tables
|
|
159
|
-
* or piping database payloads directly into HTTP responses (e.g., via `Bun.serve` or fetch `Response`).
|
|
160
|
-
*
|
|
161
|
-
* @template T The expected shape of a single row interface.
|
|
162
|
-
* @param {TemplateStringsArray} templates The SQL string parts from the tagged template literal.
|
|
163
|
-
* @param {...any} args The parameterized query arguments.
|
|
164
|
-
* @returns {ReadableStream<T>} Synchronously returns a native Web ReadableStream instance.
|
|
165
|
-
*
|
|
166
|
-
* @example
|
|
167
|
-
* // Streaming a giant table directly to an HTTP response (Bun.serve)
|
|
168
|
-
* const userStream = conn.stream<User>`SELECT id, name FROM users`;
|
|
169
|
-
* return new Response(userStream, { headers: { 'Content-Type': 'application/json' } });
|
|
70
|
+
* Streams query results as a `ReadableStream`, without buffering rows in memory.
|
|
71
|
+
* Ideal for large result sets or piping straight into an HTTP response.
|
|
170
72
|
*
|
|
171
73
|
* @example
|
|
172
|
-
*
|
|
173
|
-
* const stream = conn.stream<User>`SELECT * FROM orders WHERE status = ${'processed'}`;
|
|
174
|
-
* for await (const row of stream) {
|
|
175
|
-
* console.log(row.id, row.amount); // Row object is eligible for GC immediately after iteration
|
|
176
|
-
* }
|
|
74
|
+
* for await (const row of conn.stream<User>`SELECT * FROM orders`) { ... }
|
|
177
75
|
*/
|
|
178
|
-
stream<T extends
|
|
179
|
-
private
|
|
180
|
-
private _registerReconnect;
|
|
181
|
-
private _resetConnectionState;
|
|
76
|
+
stream<T extends Row>(templates: TemplateStringsArray, ...params: any[]): ReadableStream<T>;
|
|
77
|
+
private _performStream;
|
|
182
78
|
private _reconnect;
|
|
79
|
+
private _performReconnect;
|
|
183
80
|
private _restoreSubscriptions;
|
|
184
81
|
private _getCurrentQuery;
|
|
185
|
-
private _rejectAllBatches;
|
|
186
82
|
private _handlePacket;
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
* Returns `false` if the socket is destroyed or connection is dead.
|
|
190
|
-
*/
|
|
83
|
+
private get _hasQueries();
|
|
84
|
+
/** Whether the connection is alive and usable. */
|
|
191
85
|
get isOpened(): boolean;
|
|
86
|
+
/** Whether the connection is closed or closing. */
|
|
87
|
+
get isClosed(): boolean;
|
|
192
88
|
/**
|
|
193
|
-
* Closes the connection
|
|
194
|
-
* All pending queries will be rejected with an error.
|
|
195
|
-
* The connection cannot be used after this call.
|
|
196
|
-
*
|
|
197
|
-
* @example
|
|
198
|
-
* ```ts
|
|
199
|
-
* conn.close()
|
|
200
|
-
* ```
|
|
89
|
+
* Closes the connection, awaiting for all pending queries. Not usable afterward.
|
|
201
90
|
*/
|
|
202
|
-
close(): void
|
|
91
|
+
close(): Future<void, PostgresError>;
|
|
203
92
|
}
|
|
204
93
|
//# sourceMappingURL=connection.d.ts.map
|
package/dist/connection.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../src/connection.ts"],"names":[],"mappings":"AAMA,OAAO,
|
|
1
|
+
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../src/connection.ts"],"names":[],"mappings":"AAMA,OAAO,EAAiC,uBAAuB,EAAmC,GAAG,EAAiB,MAAM,SAAS,CAAA;AACrI,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAK3C,OAAO,EAAS,MAAM,EAAM,MAAM,eAAe,CAAA;AACjD,OAAO,EAAkD,aAAa,EAAE,MAAM,SAAS,CAAA;AAavF;;;;;;;;;GASG;AACH,qBAAa,UAAU;IACnB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAkB;IAEzC,O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