@agentuity/postgres 1.0.1 → 1.0.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/dist/index.d.ts CHANGED
@@ -32,10 +32,11 @@
32
32
  */
33
33
  export { postgres, default } from './postgres';
34
34
  export { PostgresClient, createCallableClient, type CallablePostgresClient } from './client';
35
+ export { PostgresPool, Pool, createPool } from './pool';
35
36
  export { Transaction, Savepoint, ReservedConnection } from './transaction';
36
37
  export { patchBunSQL, isPatched, SQL } from './patch';
37
- export type { PostgresConfig, ReconnectConfig, ConnectionStats, TLSConfig, TransactionOptions, ReserveOptions, } from './types';
38
+ export type { PostgresConfig, ReconnectConfig, ConnectionStats, TLSConfig, TransactionOptions, ReserveOptions, PoolConfig, PoolStats, PoolSSLConfig, } from './types';
38
39
  export { PostgresError, ConnectionClosedError, ReconnectFailedError, QueryTimeoutError, TransactionError, UnsupportedOperationError, isRetryableError, } from './errors';
39
40
  export { computeBackoff, sleep, mergeReconnectConfig, DEFAULT_RECONNECT_CONFIG } from './reconnect';
40
- export { shutdownAll, getClientCount, getClients, hasActiveClients } from './registry';
41
+ export { shutdownAll, getClientCount, getClients, hasActiveClients, type Registrable, } from './registry';
41
42
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAGH,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAG/C,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAE,KAAK,sBAAsB,EAAE,MAAM,UAAU,CAAC;AAG7F,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAG3E,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAGtD,YAAY,EAC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AM,aAAa,CAAC;AAGpG,OAAO,EACN,WAAW,EACX,cAAc,EACd,UAAU,EACV,gBAAgB,EAChB,KAAK,WAAW,GAChB,MAAM,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -34,6 +34,8 @@
34
34
  export { postgres, default } from './postgres';
35
35
  // Client class for advanced usage
36
36
  export { PostgresClient, createCallableClient } from './client';
37
+ // Pool class for pg.Pool-based connections
38
+ export { PostgresPool, Pool, createPool } from './pool';
37
39
  // Transaction and reserved connection classes
38
40
  export { Transaction, Savepoint, ReservedConnection } from './transaction';
39
41
  // Patch function for modifying Bun.SQL globally
@@ -43,5 +45,5 @@ export { PostgresError, ConnectionClosedError, ReconnectFailedError, QueryTimeou
43
45
  // Reconnection utilities
44
46
  export { computeBackoff, sleep, mergeReconnectConfig, DEFAULT_RECONNECT_CONFIG } from './reconnect';
45
47
  // Global registry for coordinated shutdown
46
- export { shutdownAll, getClientCount, getClients, hasActiveClients } from './registry';
48
+ export { shutdownAll, getClientCount, getClients, hasActiveClients, } from './registry';
47
49
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,wBAAwB;AACxB,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAE/C,kCAAkC;AAClC,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAA+B,MAAM,UAAU,CAAC;AAE7F,8CAA8C;AAC9C,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAE3E,gDAAgD;AAChD,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAYtD,SAAS;AACT,OAAO,EACN,aAAa,EACb,qBAAqB,EACrB,oBAAoB,EACpB,iBAAiB,EACjB,gBAAgB,EAChB,yBAAyB,EACzB,gBAAgB,GAChB,MAAM,UAAU,CAAC;AAElB,yBAAyB;AACzB,OAAO,EAAE,cAAc,EAAE,KAAK,EAAE,oBAAoB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAEpG,2CAA2C;AAC3C,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,wBAAwB;AACxB,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAE/C,kCAAkC;AAClC,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAA+B,MAAM,UAAU,CAAC;AAE7F,2CAA2C;AAC3C,OAAO,EAAE,YAAY,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAExD,8CAA8C;AAC9C,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAE3E,gDAAgD;AAChD,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAetD,SAAS;AACT,OAAO,EACN,aAAa,EACb,qBAAqB,EACrB,oBAAoB,EACpB,iBAAiB,EACjB,gBAAgB,EAChB,yBAAyB,EACzB,gBAAgB,GAChB,MAAM,UAAU,CAAC;AAElB,yBAAyB;AACzB,OAAO,EAAE,cAAc,EAAE,KAAK,EAAE,oBAAoB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAEpG,2CAA2C;AAC3C,OAAO,EACN,WAAW,EACX,cAAc,EACd,UAAU,EACV,gBAAgB,GAEhB,MAAM,YAAY,CAAC"}
package/dist/pool.d.ts ADDED
@@ -0,0 +1,194 @@
1
+ import pg from 'pg';
2
+ import type { PoolConfig, PoolStats } from './types';
3
+ import { type Registrable } from './registry';
4
+ /**
5
+ * A resilient PostgreSQL connection pool with automatic reconnection.
6
+ *
7
+ * Wraps the `pg` package's Pool and adds:
8
+ * - Automatic reconnection with exponential backoff
9
+ * - Connection state tracking
10
+ * - Pool statistics
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * const pool = new PostgresPool({
15
+ * connectionString: process.env.DATABASE_URL,
16
+ * max: 20,
17
+ * reconnect: { maxAttempts: 5 }
18
+ * });
19
+ *
20
+ * // Execute queries
21
+ * const result = await pool.query('SELECT * FROM users WHERE id = $1', [userId]);
22
+ *
23
+ * // Close when done
24
+ * await pool.close();
25
+ * ```
26
+ */
27
+ export declare class PostgresPool implements Registrable {
28
+ private _pool;
29
+ private _config;
30
+ private _connected;
31
+ private _reconnecting;
32
+ private _closed;
33
+ private _shuttingDown;
34
+ private _signalHandlers;
35
+ private _reconnectPromise;
36
+ private _connectPromise;
37
+ private _stats;
38
+ /**
39
+ * Creates a new PostgresPool.
40
+ *
41
+ * Note: By default, the actual connection is established lazily on first query.
42
+ * Set `preconnect: true` in config to verify connectivity immediately.
43
+ *
44
+ * @param config - Connection configuration. Can be a connection URL string or a config object.
45
+ * If not provided, uses `process.env.DATABASE_URL`.
46
+ */
47
+ constructor(config?: string | PoolConfig);
48
+ /**
49
+ * Whether the pool is currently connected.
50
+ */
51
+ get connected(): boolean;
52
+ /**
53
+ * Whether the pool is shutting down (won't attempt reconnection).
54
+ */
55
+ get shuttingDown(): boolean;
56
+ /**
57
+ * Whether a reconnection attempt is in progress.
58
+ */
59
+ get reconnecting(): boolean;
60
+ /**
61
+ * Pool statistics.
62
+ */
63
+ get stats(): Readonly<PoolStats>;
64
+ /**
65
+ * Execute a query on the pool.
66
+ * If reconnection is in progress, waits for it to complete before executing.
67
+ * Automatically retries on retryable errors.
68
+ *
69
+ * @param text - The query string or query config object
70
+ * @param values - Optional array of parameter values
71
+ * @returns The query result
72
+ *
73
+ * @example
74
+ * ```typescript
75
+ * const result = await pool.query('SELECT * FROM users WHERE id = $1', [userId]);
76
+ * console.log(result.rows);
77
+ * ```
78
+ */
79
+ query<T extends pg.QueryResultRow = pg.QueryResultRow>(text: string | pg.QueryConfig<unknown[]>, values?: unknown[]): Promise<pg.QueryResult<T>>;
80
+ /**
81
+ * Acquire a client from the pool.
82
+ * The client must be released back to the pool when done.
83
+ *
84
+ * @returns A pooled client
85
+ *
86
+ * @example
87
+ * ```typescript
88
+ * const client = await pool.connect();
89
+ * try {
90
+ * await client.query('BEGIN');
91
+ * await client.query('INSERT INTO users (name) VALUES ($1)', ['Alice']);
92
+ * await client.query('COMMIT');
93
+ * } catch (error) {
94
+ * await client.query('ROLLBACK');
95
+ * throw error;
96
+ * } finally {
97
+ * client.release();
98
+ * }
99
+ * ```
100
+ */
101
+ connect(): Promise<pg.PoolClient>;
102
+ /**
103
+ * Signal that the application is shutting down.
104
+ * This prevents reconnection attempts but doesn't immediately close the pool.
105
+ * Use this when you want to gracefully drain connections before calling close().
106
+ */
107
+ shutdown(): void;
108
+ /**
109
+ * Close the pool and release all connections.
110
+ * Alias for end() for compatibility with PostgresClient.
111
+ */
112
+ close(): Promise<void>;
113
+ /**
114
+ * Close the pool and release all connections.
115
+ */
116
+ end(): Promise<void>;
117
+ /**
118
+ * Access to the raw pg.Pool instance for advanced use cases.
119
+ * Returns the underlying pg.Pool instance.
120
+ */
121
+ get raw(): pg.Pool;
122
+ /**
123
+ * Wait for the connection to be established.
124
+ * If the connection hasn't been established yet (lazy connection), this will
125
+ * warm the connection by acquiring and releasing a client.
126
+ * If reconnection is in progress, waits for it to complete.
127
+ *
128
+ * @param timeoutMs - Optional timeout in milliseconds
129
+ * @throws {ConnectionClosedError} If the pool has been closed or connection fails
130
+ */
131
+ waitForConnection(timeoutMs?: number): Promise<void>;
132
+ /**
133
+ * Registers signal handlers to detect application shutdown.
134
+ * When shutdown is detected, reconnection is disabled.
135
+ */
136
+ private _registerShutdownHandlers;
137
+ /**
138
+ * Removes signal handlers registered for shutdown detection.
139
+ */
140
+ private _removeShutdownHandlers;
141
+ /**
142
+ * Initializes the pg.Pool instance.
143
+ */
144
+ private _initializePool;
145
+ /**
146
+ * Warms the connection by acquiring and releasing a client.
147
+ * This verifies the pool can connect to the database.
148
+ */
149
+ private _warmConnection;
150
+ /**
151
+ * Re-initializes the pool for reconnection.
152
+ */
153
+ private _reinitializePool;
154
+ /**
155
+ * Handles pool error events.
156
+ */
157
+ private _handlePoolError;
158
+ /**
159
+ * Starts the reconnection process.
160
+ */
161
+ private _startReconnect;
162
+ /**
163
+ * The main reconnection loop with exponential backoff.
164
+ */
165
+ private _reconnectLoop;
166
+ /**
167
+ * Ensures the pool is initialized and returns it.
168
+ */
169
+ private _ensureConnected;
170
+ /**
171
+ * Ensures the pool is connected and returns it.
172
+ * If reconnection is in progress, waits for it to complete.
173
+ * If connection hasn't been established yet, warms it first.
174
+ */
175
+ private _ensureConnectedAsync;
176
+ /**
177
+ * Executes an operation with retry logic for retryable errors.
178
+ * Waits for reconnection if one is in progress.
179
+ */
180
+ private _executeWithRetry;
181
+ }
182
+ /**
183
+ * Creates a new PostgresPool.
184
+ * This is an alias for `new PostgresPool(config)` for convenience.
185
+ *
186
+ * @param config - Connection configuration
187
+ * @returns A new PostgresPool instance
188
+ */
189
+ export declare function createPool(config?: string | PoolConfig): PostgresPool;
190
+ /**
191
+ * Alias for PostgresPool for convenient imports.
192
+ */
193
+ export { PostgresPool as Pool };
194
+ //# sourceMappingURL=pool.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pool.d.ts","sourceRoot":"","sources":["../src/pool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AASrD,OAAO,EAAoC,KAAK,WAAW,EAAE,MAAM,YAAY,CAAC;AAEhF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,qBAAa,YAAa,YAAW,WAAW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