@vibeorm/adapter-pg 1.1.3 → 1.1.5

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.
Files changed (3) hide show
  1. package/README.md +33 -1
  2. package/package.json +2 -2
  3. package/src/index.ts +43 -2
package/README.md CHANGED
@@ -39,11 +39,13 @@ When you pass an existing pool, the adapter will **not** call `pool.end()` on di
39
39
  type PgAdapterOptions = {
40
40
  connectionString?: string; // Connection string (defaults to DATABASE_URL env)
41
41
  max?: number; // Pool size (default: 10)
42
+ statementTimeout?: number; // Default query timeout in ms (default: none)
43
+ connectionTimeout?: number; // Max time to acquire a connection in ms (default: none)
42
44
  preparedStatements?: boolean; // Enable prepared statement caching (default: false)
43
45
  stmtCacheMax?: number; // Max cached prepared statements (default: 1000)
44
46
  };
45
47
 
46
- // Or use an existing pool
48
+ // Or use an existing pool (BYOP — you manage pool config)
47
49
  type PgAdapterOptions = {
48
50
  pool: PgPool; // Existing pg.Pool instance
49
51
  preparedStatements?: boolean;
@@ -51,6 +53,36 @@ type PgAdapterOptions = {
51
53
  };
52
54
  ```
53
55
 
56
+ ### Statement timeout
57
+
58
+ Set a default timeout for all queries. Any query exceeding this duration is cancelled by PostgreSQL (SQLSTATE `57014`), surfaced as a `VibeTransientError` with code `STATEMENT_TIMEOUT`.
59
+
60
+ ```ts
61
+ const adapter = pgAdapter({
62
+ connectionString: "postgres://...",
63
+ statementTimeout: 30000, // 30 seconds
64
+ });
65
+ ```
66
+
67
+ Passed as `statement_timeout` in the pg Pool config, so every connection inherits it automatically with zero per-query overhead.
68
+
69
+ Transaction-level `timeout` (via `db.$transaction(fn, { timeout })`) overrides this default for that transaction using `SET LOCAL statement_timeout`.
70
+
71
+ > **Note:** `statementTimeout` and `connectionTimeout` are only available when creating a new pool. When you pass an existing `pool`, you manage these settings yourself via your pool config.
72
+
73
+ ### Connection timeout
74
+
75
+ Limit how long pool checkout (acquiring a connection) can take:
76
+
77
+ ```ts
78
+ const adapter = pgAdapter({
79
+ connectionString: "postgres://...",
80
+ connectionTimeout: 5000, // 5 seconds
81
+ });
82
+ ```
83
+
84
+ Mapped to pg Pool's `connectionTimeoutMillis` option. Throws when the pool is exhausted and no connection becomes available within the limit.
85
+
54
86
  ## License
55
87
 
56
88
  [MIT](../../LICENSE)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vibeorm/adapter-pg",
3
- "version": "1.1.3",
3
+ "version": "1.1.5",
4
4
  "description": "node-postgres adapter for VibeORM — use VibeORM with pg on Bun or Node.js",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -31,7 +31,7 @@
31
31
  "url": "https://github.com/vibeorm/vibeorm/issues"
32
32
  },
33
33
  "dependencies": {
34
- "@vibeorm/runtime": "1.1.3"
34
+ "@vibeorm/runtime": "1.1.5"
35
35
  },
36
36
  "peerDependencies": {
37
37
  "pg": ">=8"
package/src/index.ts CHANGED
@@ -16,6 +16,31 @@ export type PgAdapterOptions = {
16
16
  connectionString?: string;
17
17
  /** Maximum number of connections in the pool (default: 10). */
18
18
  max?: number;
19
+ /**
20
+ * Default statement timeout in milliseconds for all queries (default: none).
21
+ *
22
+ * Sets PostgreSQL's `statement_timeout` at the pool level via the pg driver's
23
+ * `statement_timeout` option, so every query on every connection inherits it
24
+ * automatically. Queries exceeding this duration are cancelled by PostgreSQL
25
+ * with SQLSTATE 57014, which VibeORM surfaces as a `VibeTransientError` with
26
+ * code `STATEMENT_TIMEOUT`.
27
+ *
28
+ * Transaction-level `timeout` (via `$transaction` options) overrides this
29
+ * default for the duration of that transaction using `SET LOCAL statement_timeout`.
30
+ *
31
+ * Recommended: Set to a value appropriate for your workload (e.g. 30000 for
32
+ * general use, 250–1000 for latency-sensitive OLTP services).
33
+ */
34
+ statementTimeout?: number;
35
+ /**
36
+ * Maximum time in milliseconds to wait for a connection from the pool (default: none).
37
+ *
38
+ * When set, pool checkout that exceeds this duration throws an error.
39
+ * Prevents indefinite blocking when the pool is exhausted under load.
40
+ *
41
+ * Maps to pg Pool's `connectionTimeoutMillis` option.
42
+ */
43
+ connectionTimeout?: number;
19
44
  /**
20
45
  * Whether to use named prepared statements for read queries (default: false).
21
46
  *
@@ -194,11 +219,27 @@ export function pgAdapter(options?: PgAdapterOptions): DatabaseAdapter {
194
219
  (options && "connectionString" in options ? options.connectionString : undefined) ??
195
220
  process.env.DATABASE_URL;
196
221
  const max = (options && "max" in options ? options.max : undefined) ?? 10;
222
+ const statementTimeout =
223
+ options && "statementTimeout" in options ? options.statementTimeout : undefined;
224
+ const connectionTimeout =
225
+ options && "connectionTimeout" in options ? options.connectionTimeout : undefined;
197
226
 
198
- pool = new Pool({
227
+ const poolConfig: Record<string, unknown> = {
199
228
  connectionString,
200
229
  max,
201
- }) as PgPool;
230
+ };
231
+
232
+ // Set statement_timeout at the pool level — every connection inherits it
233
+ if (statementTimeout !== undefined) {
234
+ poolConfig.statement_timeout = Number(statementTimeout);
235
+ }
236
+
237
+ // Set connectionTimeoutMillis — pool.connect() throws if exceeded
238
+ if (connectionTimeout !== undefined) {
239
+ poolConfig.connectionTimeoutMillis = Number(connectionTimeout);
240
+ }
241
+
242
+ pool = new Pool(poolConfig) as PgPool;
202
243
  ownsPool = true;
203
244
  }
204
245