@agentuity/postgres 0.1.41
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/AGENTS.md +124 -0
- package/README.md +297 -0
- package/dist/client.d.ts +224 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +670 -0
- package/dist/client.js.map +1 -0
- package/dist/errors.d.ts +109 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +115 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +47 -0
- package/dist/index.js.map +1 -0
- package/dist/patch.d.ts +65 -0
- package/dist/patch.d.ts.map +1 -0
- package/dist/patch.js +111 -0
- package/dist/patch.js.map +1 -0
- package/dist/postgres.d.ts +62 -0
- package/dist/postgres.d.ts.map +1 -0
- package/dist/postgres.js +63 -0
- package/dist/postgres.js.map +1 -0
- package/dist/reconnect.d.ts +31 -0
- package/dist/reconnect.d.ts.map +1 -0
- package/dist/reconnect.js +60 -0
- package/dist/reconnect.js.map +1 -0
- package/dist/registry.d.ts +71 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +175 -0
- package/dist/registry.js.map +1 -0
- package/dist/transaction.d.ts +147 -0
- package/dist/transaction.d.ts.map +1 -0
- package/dist/transaction.js +287 -0
- package/dist/transaction.js.map +1 -0
- package/dist/types.d.ts +213 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +55 -0
- package/src/client.ts +776 -0
- package/src/errors.ts +154 -0
- package/src/index.ts +71 -0
- package/src/patch.ts +123 -0
- package/src/postgres.ts +65 -0
- package/src/reconnect.ts +74 -0
- package/src/registry.ts +194 -0
- package/src/transaction.ts +312 -0
- package/src/types.ts +250 -0
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import type { SQL, SQLQuery } from 'bun';
|
|
2
|
+
import { TransactionError } from './errors';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Represents a PostgreSQL transaction with support for savepoints.
|
|
6
|
+
*
|
|
7
|
+
* Transactions are created via `PostgresClient.begin()` and support
|
|
8
|
+
* tagged template literal syntax for queries.
|
|
9
|
+
*/
|
|
10
|
+
export class Transaction {
|
|
11
|
+
private _sql: SQL;
|
|
12
|
+
private _connection: SQLQuery;
|
|
13
|
+
private _committed = false;
|
|
14
|
+
private _rolledBack = false;
|
|
15
|
+
private _savepointCounter = 0;
|
|
16
|
+
|
|
17
|
+
constructor(sql: SQL, connection: SQLQuery) {
|
|
18
|
+
this._sql = sql;
|
|
19
|
+
this._connection = connection;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Whether the transaction has been committed.
|
|
24
|
+
*/
|
|
25
|
+
get committed(): boolean {
|
|
26
|
+
return this._committed;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Whether the transaction has been rolled back.
|
|
31
|
+
*/
|
|
32
|
+
get rolledBack(): boolean {
|
|
33
|
+
return this._rolledBack;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Whether the transaction is still active (not committed or rolled back).
|
|
38
|
+
*/
|
|
39
|
+
get active(): boolean {
|
|
40
|
+
return !this._committed && !this._rolledBack;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Execute a query within this transaction using tagged template literal syntax.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* const tx = await client.begin();
|
|
49
|
+
* const result = await tx`SELECT * FROM users WHERE id = ${userId}`;
|
|
50
|
+
* await tx.commit();
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
query(strings: TemplateStringsArray, ...values: unknown[]): SQLQuery {
|
|
54
|
+
this._ensureActive('query');
|
|
55
|
+
return this._sql(strings, ...values);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Create a savepoint within this transaction.
|
|
60
|
+
*
|
|
61
|
+
* @param name - Optional name for the savepoint. If not provided, a unique name is generated.
|
|
62
|
+
* If provided, must be a valid SQL identifier (alphanumeric and underscores only,
|
|
63
|
+
* starting with a letter or underscore).
|
|
64
|
+
* @returns A Savepoint object that can be used to rollback to this point.
|
|
65
|
+
* @throws {TransactionError} If the provided name is not a valid SQL identifier.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* const tx = await client.begin();
|
|
70
|
+
* await tx`INSERT INTO users (name) VALUES ('Alice')`;
|
|
71
|
+
*
|
|
72
|
+
* const sp = await tx.savepoint();
|
|
73
|
+
* await tx`INSERT INTO users (name) VALUES ('Bob')`;
|
|
74
|
+
*
|
|
75
|
+
* // Oops, rollback Bob but keep Alice
|
|
76
|
+
* await sp.rollback();
|
|
77
|
+
*
|
|
78
|
+
* await tx.commit(); // Only Alice is committed
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
async savepoint(name?: string): Promise<Savepoint> {
|
|
82
|
+
this._ensureActive('savepoint');
|
|
83
|
+
|
|
84
|
+
const savepointName = name ?? `sp_${++this._savepointCounter}`;
|
|
85
|
+
|
|
86
|
+
// Validate savepoint name to prevent SQL injection
|
|
87
|
+
// Must be a valid SQL identifier: starts with letter or underscore, contains only alphanumeric and underscores
|
|
88
|
+
const validIdentifierPattern = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
89
|
+
if (!validIdentifierPattern.test(savepointName)) {
|
|
90
|
+
throw new TransactionError({
|
|
91
|
+
message: `Invalid savepoint name "${savepointName}": must be a valid SQL identifier (alphanumeric and underscores only, starting with a letter or underscore)`,
|
|
92
|
+
phase: 'savepoint',
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
try {
|
|
97
|
+
await this._sql`SAVEPOINT ${this._sql.unsafe(savepointName)}`;
|
|
98
|
+
return new Savepoint(this._sql, savepointName);
|
|
99
|
+
} catch (error) {
|
|
100
|
+
throw new TransactionError({
|
|
101
|
+
message: `Failed to create savepoint: ${error instanceof Error ? error.message : String(error)}`,
|
|
102
|
+
phase: 'savepoint',
|
|
103
|
+
cause: error,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Commit the transaction.
|
|
110
|
+
*
|
|
111
|
+
* @throws {TransactionError} If the transaction is not active or commit fails.
|
|
112
|
+
*/
|
|
113
|
+
async commit(): Promise<void> {
|
|
114
|
+
this._ensureActive('commit');
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
await this._sql`COMMIT`;
|
|
118
|
+
this._committed = true;
|
|
119
|
+
} catch (error) {
|
|
120
|
+
throw new TransactionError({
|
|
121
|
+
message: `Failed to commit transaction: ${error instanceof Error ? error.message : String(error)}`,
|
|
122
|
+
phase: 'commit',
|
|
123
|
+
cause: error,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Rollback the transaction.
|
|
130
|
+
*
|
|
131
|
+
* @throws {TransactionError} If the transaction is not active or rollback fails.
|
|
132
|
+
*/
|
|
133
|
+
async rollback(): Promise<void> {
|
|
134
|
+
this._ensureActive('rollback');
|
|
135
|
+
|
|
136
|
+
try {
|
|
137
|
+
await this._sql`ROLLBACK`;
|
|
138
|
+
this._rolledBack = true;
|
|
139
|
+
} catch (error) {
|
|
140
|
+
throw new TransactionError({
|
|
141
|
+
message: `Failed to rollback transaction: ${error instanceof Error ? error.message : String(error)}`,
|
|
142
|
+
phase: 'rollback',
|
|
143
|
+
cause: error,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Ensures the transaction is still active.
|
|
150
|
+
*/
|
|
151
|
+
private _ensureActive(operation: string): void {
|
|
152
|
+
if (this._committed) {
|
|
153
|
+
throw new TransactionError({
|
|
154
|
+
message: `Cannot ${operation}: transaction has been committed`,
|
|
155
|
+
phase: operation as 'begin' | 'commit' | 'rollback' | 'savepoint',
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
if (this._rolledBack) {
|
|
159
|
+
throw new TransactionError({
|
|
160
|
+
message: `Cannot ${operation}: transaction has been rolled back`,
|
|
161
|
+
phase: operation as 'begin' | 'commit' | 'rollback' | 'savepoint',
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Represents a savepoint within a transaction.
|
|
169
|
+
*/
|
|
170
|
+
export class Savepoint {
|
|
171
|
+
private _sql: SQL;
|
|
172
|
+
private _name: string;
|
|
173
|
+
private _released = false;
|
|
174
|
+
private _rolledBack = false;
|
|
175
|
+
|
|
176
|
+
constructor(sql: SQL, name: string) {
|
|
177
|
+
this._sql = sql;
|
|
178
|
+
this._name = name;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* The name of this savepoint.
|
|
183
|
+
*/
|
|
184
|
+
get name(): string {
|
|
185
|
+
return this._name;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Whether the savepoint has been released.
|
|
190
|
+
*/
|
|
191
|
+
get released(): boolean {
|
|
192
|
+
return this._released;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Whether the savepoint has been rolled back to.
|
|
197
|
+
*/
|
|
198
|
+
get rolledBack(): boolean {
|
|
199
|
+
return this._rolledBack;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Rollback to this savepoint.
|
|
204
|
+
* All changes made after this savepoint was created will be undone.
|
|
205
|
+
*
|
|
206
|
+
* @throws {TransactionError} If the savepoint has been released or already rolled back.
|
|
207
|
+
*/
|
|
208
|
+
async rollback(): Promise<void> {
|
|
209
|
+
if (this._released) {
|
|
210
|
+
throw new TransactionError({
|
|
211
|
+
message: `Cannot rollback: savepoint "${this._name}" has been released`,
|
|
212
|
+
phase: 'savepoint',
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (this._rolledBack) {
|
|
217
|
+
throw new TransactionError({
|
|
218
|
+
message: `Cannot rollback: savepoint "${this._name}" has already been rolled back`,
|
|
219
|
+
phase: 'savepoint',
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
try {
|
|
224
|
+
await this._sql`ROLLBACK TO SAVEPOINT ${this._sql.unsafe(this._name)}`;
|
|
225
|
+
this._rolledBack = true;
|
|
226
|
+
} catch (error) {
|
|
227
|
+
throw new TransactionError({
|
|
228
|
+
message: `Failed to rollback to savepoint: ${error instanceof Error ? error.message : String(error)}`,
|
|
229
|
+
phase: 'savepoint',
|
|
230
|
+
cause: error,
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Release this savepoint.
|
|
237
|
+
* The savepoint is destroyed but changes are kept.
|
|
238
|
+
*/
|
|
239
|
+
async release(): Promise<void> {
|
|
240
|
+
if (this._released) {
|
|
241
|
+
return; // Already released, no-op
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
try {
|
|
245
|
+
await this._sql`RELEASE SAVEPOINT ${this._sql.unsafe(this._name)}`;
|
|
246
|
+
this._released = true;
|
|
247
|
+
} catch (error) {
|
|
248
|
+
throw new TransactionError({
|
|
249
|
+
message: `Failed to release savepoint: ${error instanceof Error ? error.message : String(error)}`,
|
|
250
|
+
phase: 'savepoint',
|
|
251
|
+
cause: error,
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Represents a reserved (exclusive) connection from the pool.
|
|
259
|
+
*
|
|
260
|
+
* Reserved connections are created via `PostgresClient.reserve()` and support
|
|
261
|
+
* tagged template literal syntax for queries.
|
|
262
|
+
*/
|
|
263
|
+
export class ReservedConnection {
|
|
264
|
+
private _sql: SQL;
|
|
265
|
+
private _released = false;
|
|
266
|
+
|
|
267
|
+
constructor(sql: SQL) {
|
|
268
|
+
this._sql = sql;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Whether the connection has been released back to the pool.
|
|
273
|
+
*/
|
|
274
|
+
get released(): boolean {
|
|
275
|
+
return this._released;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Execute a query on this reserved connection using tagged template literal syntax.
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* ```typescript
|
|
283
|
+
* const conn = await client.reserve();
|
|
284
|
+
* try {
|
|
285
|
+
* await conn`SET LOCAL timezone = 'UTC'`;
|
|
286
|
+
* const result = await conn`SELECT NOW()`;
|
|
287
|
+
* } finally {
|
|
288
|
+
* conn.release();
|
|
289
|
+
* }
|
|
290
|
+
* ```
|
|
291
|
+
*/
|
|
292
|
+
query(strings: TemplateStringsArray, ...values: unknown[]): SQLQuery {
|
|
293
|
+
if (this._released) {
|
|
294
|
+
throw new TransactionError({
|
|
295
|
+
message: 'Cannot query: connection has been released',
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
return this._sql(strings, ...values);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Release the connection back to the pool.
|
|
303
|
+
*/
|
|
304
|
+
release(): void {
|
|
305
|
+
if (this._released) {
|
|
306
|
+
return; // Already released, no-op
|
|
307
|
+
}
|
|
308
|
+
this._released = true;
|
|
309
|
+
// The underlying SQL connection will be returned to the pool
|
|
310
|
+
// when it goes out of scope or is explicitly closed
|
|
311
|
+
}
|
|
312
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TLS configuration options for PostgreSQL connections.
|
|
3
|
+
*/
|
|
4
|
+
export interface TLSConfig {
|
|
5
|
+
/**
|
|
6
|
+
* Whether to require TLS for the connection.
|
|
7
|
+
* - `true`: Require TLS (fail if not available)
|
|
8
|
+
* - `false`: Disable TLS
|
|
9
|
+
* - `'prefer'`: Use TLS if available, fall back to unencrypted
|
|
10
|
+
*/
|
|
11
|
+
require?: boolean | 'prefer';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Whether to reject unauthorized certificates.
|
|
15
|
+
* Set to `false` to allow self-signed certificates.
|
|
16
|
+
*/
|
|
17
|
+
rejectUnauthorized?: boolean;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* CA certificate(s) for verifying the server certificate.
|
|
21
|
+
*/
|
|
22
|
+
ca?: string | Buffer | (string | Buffer)[];
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Client certificate for mutual TLS authentication.
|
|
26
|
+
*/
|
|
27
|
+
cert?: string | Buffer;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Client private key for mutual TLS authentication.
|
|
31
|
+
*/
|
|
32
|
+
key?: string | Buffer;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Configuration for automatic reconnection behavior.
|
|
37
|
+
*/
|
|
38
|
+
export interface ReconnectConfig {
|
|
39
|
+
/**
|
|
40
|
+
* Maximum number of reconnection attempts before giving up.
|
|
41
|
+
* @default 10
|
|
42
|
+
*/
|
|
43
|
+
maxAttempts?: number;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Initial delay in milliseconds before the first reconnection attempt.
|
|
47
|
+
* @default 100
|
|
48
|
+
*/
|
|
49
|
+
initialDelayMs?: number;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Maximum delay in milliseconds between reconnection attempts.
|
|
53
|
+
* @default 30000
|
|
54
|
+
*/
|
|
55
|
+
maxDelayMs?: number;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Multiplier for exponential backoff.
|
|
59
|
+
* @default 2
|
|
60
|
+
*/
|
|
61
|
+
multiplier?: number;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Maximum random jitter in milliseconds to add to the delay.
|
|
65
|
+
* Helps prevent thundering herd problems.
|
|
66
|
+
* @default 1000
|
|
67
|
+
*/
|
|
68
|
+
jitterMs?: number;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Whether to automatically reconnect on connection loss.
|
|
72
|
+
* @default true
|
|
73
|
+
*/
|
|
74
|
+
enabled?: boolean;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Statistics about the connection state and reconnection history.
|
|
79
|
+
*/
|
|
80
|
+
export interface ConnectionStats {
|
|
81
|
+
/**
|
|
82
|
+
* Whether the client is currently connected.
|
|
83
|
+
*/
|
|
84
|
+
connected: boolean;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Whether a reconnection attempt is in progress.
|
|
88
|
+
*/
|
|
89
|
+
reconnecting: boolean;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Total number of successful connections (including reconnections).
|
|
93
|
+
*/
|
|
94
|
+
totalConnections: number;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Total number of reconnection attempts.
|
|
98
|
+
*/
|
|
99
|
+
reconnectAttempts: number;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Total number of failed reconnection attempts.
|
|
103
|
+
*/
|
|
104
|
+
failedReconnects: number;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Timestamp of the last successful connection.
|
|
108
|
+
*/
|
|
109
|
+
lastConnectedAt: Date | null;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Timestamp of the last disconnection.
|
|
113
|
+
*/
|
|
114
|
+
lastDisconnectedAt: Date | null;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Timestamp of the last reconnection attempt.
|
|
118
|
+
*/
|
|
119
|
+
lastReconnectAttemptAt: Date | null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* PostgreSQL connection configuration options.
|
|
124
|
+
* Extends Bun.SQL options with reconnection configuration.
|
|
125
|
+
*/
|
|
126
|
+
export interface PostgresConfig {
|
|
127
|
+
/**
|
|
128
|
+
* PostgreSQL connection URL.
|
|
129
|
+
* If not provided, uses `process.env.DATABASE_URL`.
|
|
130
|
+
*/
|
|
131
|
+
url?: string;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Database hostname.
|
|
135
|
+
*/
|
|
136
|
+
hostname?: string;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Database port.
|
|
140
|
+
* @default 5432
|
|
141
|
+
*/
|
|
142
|
+
port?: number;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Database username.
|
|
146
|
+
*/
|
|
147
|
+
username?: string;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Database password.
|
|
151
|
+
*/
|
|
152
|
+
password?: string;
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Database name.
|
|
156
|
+
*/
|
|
157
|
+
database?: string;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* TLS configuration.
|
|
161
|
+
*/
|
|
162
|
+
tls?: TLSConfig | boolean;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Maximum number of connections in the pool.
|
|
166
|
+
* @default 10
|
|
167
|
+
*/
|
|
168
|
+
max?: number;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Connection timeout in milliseconds.
|
|
172
|
+
* @default 30000
|
|
173
|
+
*/
|
|
174
|
+
connectionTimeout?: number;
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Idle timeout in milliseconds.
|
|
178
|
+
* @default 0 (no timeout)
|
|
179
|
+
*/
|
|
180
|
+
idleTimeout?: number;
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Reconnection configuration.
|
|
184
|
+
*/
|
|
185
|
+
reconnect?: ReconnectConfig;
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Whether to establish a connection immediately on client creation.
|
|
189
|
+
* If true, the client will execute a test query (SELECT 1) to verify
|
|
190
|
+
* the connection is working. If false (default), the connection is
|
|
191
|
+
* established lazily on first query.
|
|
192
|
+
*
|
|
193
|
+
* Note: Even with preconnect=false, the underlying Bun.SQL client is
|
|
194
|
+
* created immediately, but the actual TCP connection is deferred.
|
|
195
|
+
*
|
|
196
|
+
* @default false
|
|
197
|
+
*/
|
|
198
|
+
preconnect?: boolean;
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Callback invoked when the connection is closed.
|
|
202
|
+
*/
|
|
203
|
+
onclose?: (error?: Error) => void;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Callback invoked when reconnection starts.
|
|
207
|
+
*/
|
|
208
|
+
onreconnect?: (attempt: number) => void;
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Callback invoked when reconnection succeeds.
|
|
212
|
+
*/
|
|
213
|
+
onreconnected?: () => void;
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Callback invoked when reconnection fails permanently.
|
|
217
|
+
*/
|
|
218
|
+
onreconnectfailed?: (error: Error) => void;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Options for creating a transaction.
|
|
223
|
+
*/
|
|
224
|
+
export interface TransactionOptions {
|
|
225
|
+
/**
|
|
226
|
+
* Transaction isolation level.
|
|
227
|
+
*/
|
|
228
|
+
isolationLevel?: 'read uncommitted' | 'read committed' | 'repeatable read' | 'serializable';
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Whether the transaction is read-only.
|
|
232
|
+
*/
|
|
233
|
+
readOnly?: boolean;
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Whether the transaction is deferrable.
|
|
237
|
+
* Only applicable for serializable read-only transactions.
|
|
238
|
+
*/
|
|
239
|
+
deferrable?: boolean;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Options for reserving a connection.
|
|
244
|
+
*/
|
|
245
|
+
export interface ReserveOptions {
|
|
246
|
+
/**
|
|
247
|
+
* Timeout in milliseconds for acquiring a connection from the pool.
|
|
248
|
+
*/
|
|
249
|
+
timeout?: number;
|
|
250
|
+
}
|