@ingram-tech/nk-db 2.6.2 → 2.7.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 +7 -0
- package/dist/keys.d.ts +6 -0
- package/dist/keys.d.ts.map +1 -1
- package/dist/keys.js +12 -0
- package/dist/keys.js.map +1 -1
- package/dist/pool.d.ts +25 -2
- package/dist/pool.d.ts.map +1 -1
- package/dist/pool.js +37 -9
- package/dist/pool.js.map +1 -1
- package/package.json +11 -10
- package/src/keys.ts +17 -0
- package/src/pool.ts +62 -14
package/README.md
CHANGED
|
@@ -26,6 +26,13 @@ DATABASE_SSL=true # accepted for compatibility but inert — TLS is deci
|
|
|
26
26
|
DATABASE_POOL_MAX=5 # optional; keep small on serverless
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
+
`DATABASE_POOL_MAX` caps every pool `createPool` builds, including one given its
|
|
30
|
+
own `connectionString`; an explicit `max` in code wins. Remote pools close idle
|
|
31
|
+
clients after 5 s (`idleTimeoutMillis` overrides) and are registered with
|
|
32
|
+
Vercel's `attachDatabasePool`, so a Fluid instance releases its connections
|
|
33
|
+
before it is suspended instead of pinning them (a no-op off Vercel). Pass
|
|
34
|
+
`applicationName` to tell several pools in one process apart on the server.
|
|
35
|
+
|
|
29
36
|
TLS is determined by the connection string and the CA cert, never by a flag: a
|
|
30
37
|
local host (`127.0.0.1`/`localhost`) gets no TLS (local detection wins over a
|
|
31
38
|
pulled `DATABASE_CA_CERT`), and the PGlite socket also gets a `max: 1` pool
|
package/dist/keys.d.ts
CHANGED
|
@@ -36,6 +36,12 @@ export declare const getDatabaseUrl: (env?: NodeJS.ProcessEnv) => string | undef
|
|
|
36
36
|
* rather than at first query.
|
|
37
37
|
*/
|
|
38
38
|
export declare const dbEnv: () => DbEnv;
|
|
39
|
+
/**
|
|
40
|
+
* The validated DATABASE_POOL_MAX alone. `createPool` applies it even when the
|
|
41
|
+
* caller passes its own connection string: the cap is about the process's
|
|
42
|
+
* share of the server's connections, whichever url the pool dials.
|
|
43
|
+
*/
|
|
44
|
+
export declare const poolMaxFromEnv: () => number | undefined;
|
|
39
45
|
/** Whether a database connection string is present (degrade gracefully if not). */
|
|
40
46
|
export declare const isConfigured: () => boolean;
|
|
41
47
|
//# sourceMappingURL=keys.d.ts.map
|
package/dist/keys.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keys.d.ts","sourceRoot":"","sources":["../src/keys.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAcH,MAAM,WAAW,KAAK;IACrB,2DAA2D;IAC3D,gBAAgB,EAAE,MAAM,CAAC;IACzB,2EAA2E;IAC3E,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,6DAA6D;IAC7D,GAAG,EAAE,OAAO,CAAC;IACb,oCAAoC;IACpC,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;;;GAIG;AACH,eAAO,MAAM,cAAc,SACrB,MAAM,CAAC,UAAU,KACpB,MAAM,GAAG,SAA6B,CAAC;AAE1C;;;;GAIG;AACH,eAAO,MAAM,KAAK,QAAO,KAoBxB,CAAC;AAEF,mFAAmF;AACnF,eAAO,MAAM,YAAY,QAAO,OAAyC,CAAC"}
|
|
1
|
+
{"version":3,"file":"keys.d.ts","sourceRoot":"","sources":["../src/keys.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAcH,MAAM,WAAW,KAAK;IACrB,2DAA2D;IAC3D,gBAAgB,EAAE,MAAM,CAAC;IACzB,2EAA2E;IAC3E,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,6DAA6D;IAC7D,GAAG,EAAE,OAAO,CAAC;IACb,oCAAoC;IACpC,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;;;GAIG;AACH,eAAO,MAAM,cAAc,SACrB,MAAM,CAAC,UAAU,KACpB,MAAM,GAAG,SAA6B,CAAC;AAE1C;;;;GAIG;AACH,eAAO,MAAM,KAAK,QAAO,KAoBxB,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,cAAc,QAAO,MAAM,GAAG,SAU1C,CAAC;AAEF,mFAAmF;AACnF,eAAO,MAAM,YAAY,QAAO,OAAyC,CAAC"}
|
package/dist/keys.js
CHANGED
|
@@ -54,6 +54,18 @@ export const dbEnv = () => {
|
|
|
54
54
|
poolMax: result.data.DATABASE_POOL_MAX,
|
|
55
55
|
};
|
|
56
56
|
};
|
|
57
|
+
/**
|
|
58
|
+
* The validated DATABASE_POOL_MAX alone. `createPool` applies it even when the
|
|
59
|
+
* caller passes its own connection string: the cap is about the process's
|
|
60
|
+
* share of the server's connections, whichever url the pool dials.
|
|
61
|
+
*/
|
|
62
|
+
export const poolMaxFromEnv = () => {
|
|
63
|
+
const result = schema.shape.DATABASE_POOL_MAX.safeParse(process.env.DATABASE_POOL_MAX);
|
|
64
|
+
if (!result.success) {
|
|
65
|
+
throw new Error(`@ingram-tech/nk-db: invalid environment — DATABASE_POOL_MAX: ${result.error.issues[0]?.message ?? "invalid"}`);
|
|
66
|
+
}
|
|
67
|
+
return result.data;
|
|
68
|
+
};
|
|
57
69
|
/** Whether a database connection string is present (degrade gracefully if not). */
|
|
58
70
|
export const isConfigured = () => getDatabaseUrl() !== undefined;
|
|
59
71
|
//# sourceMappingURL=keys.js.map
|
package/dist/keys.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keys.js","sourceRoot":"","sources":["../src/keys.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC;IACvB,YAAY,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE;IAC1C,yEAAyE;IACzE,wEAAwE;IACxE,wEAAwE;IACxE,YAAY,EAAE,CAAC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AAO,EAAE,CAAC;aAC3D,IAAI,CAAC,IAAI,CAAC,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,6CAA6C,MAAM,EAAE,CAAC,CAAC;IACxE,CAAC;IACD,MAAM,gBAAgB,GAAG,cAAc,EAAE,CAAC;IAC1C,IAAI,CAAC,gBAAgB,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CACd,uEAAuE,CACvE,CAAC;IACH,CAAC;IACD,OAAO;QACN,gBAAgB;QAChB,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,gBAAgB;QACpC,GAAG,EAAE,MAAM,CAAC,IAAI,CAAC,YAAY,KAAK,MAAM;QACxC,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,iBAAiB;KACtC,CAAC;AACH,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,GAAuB,EAAE;IACtD,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,iBAAiB,CAAC,SAAS,CACtD,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAC7B,CAAC;IACF,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QACrB,MAAM,IAAI,KAAK,CACd,gEAAgE,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,OAAO,IAAI,SAAS,EAAE,CAC9G,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC,IAAI,CAAC;AACpB,CAAC,CAAC;AAEF,mFAAmF;AACnF,MAAM,CAAC,MAAM,YAAY,GAAG,GAAY,EAAE,CAAC,cAAc,EAAE,KAAK,SAAS,CAAC"}
|
package/dist/pool.d.ts
CHANGED
|
@@ -4,9 +4,29 @@ export interface CreatePoolConfig {
|
|
|
4
4
|
connectionString?: string;
|
|
5
5
|
/** PEM CA cert; when set, the server cert + hostname are verified. */
|
|
6
6
|
caCert?: string;
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Pool size cap. Defaults to env DATABASE_POOL_MAX (applied whether or not
|
|
9
|
+
* `connectionString` is passed), or 1 for the PGlite socket.
|
|
10
|
+
*/
|
|
8
11
|
max?: number;
|
|
12
|
+
/**
|
|
13
|
+
* How long an idle client stays open. Defaults to
|
|
14
|
+
* {@link REMOTE_IDLE_TIMEOUT_MS} on a remote server, pg's 10 s locally.
|
|
15
|
+
*/
|
|
16
|
+
idleTimeoutMillis?: number;
|
|
17
|
+
/**
|
|
18
|
+
* `application_name` the server sees for this pool's connections, so a
|
|
19
|
+
* process with several pools can tell them apart in `pg_stat_activity` or
|
|
20
|
+
* a pooler's `SHOW CLIENTS`. One set in the url wins.
|
|
21
|
+
*/
|
|
22
|
+
applicationName?: string;
|
|
9
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Idle timeout for remote pools. Short, because a serverless instance that
|
|
26
|
+
* goes quiet should hand its connections back to the server rather than pin
|
|
27
|
+
* them until it is reaped.
|
|
28
|
+
*/
|
|
29
|
+
export declare const REMOTE_IDLE_TIMEOUT_MS = 5000;
|
|
10
30
|
/**
|
|
11
31
|
* The `application_name` that marks a URL as the PGlite socket. The harness
|
|
12
32
|
* puts it on every URL it hands out; a harness that builds its own
|
|
@@ -50,7 +70,10 @@ export declare const normalizeCaCert: (caCert: string | undefined) => string | u
|
|
|
50
70
|
*
|
|
51
71
|
* On Vercel Fluid / serverless, hold the pool on `globalThis` so warm
|
|
52
72
|
* invocations reuse it instead of opening a connection per request (see the
|
|
53
|
-
* app-side `src/lib/db.ts` pattern in docs/db-package.md).
|
|
73
|
+
* app-side `src/lib/db.ts` pattern in docs/db-package.md). A remote pool is
|
|
74
|
+
* registered with Vercel's `attachDatabasePool`, which keeps the instance alive
|
|
75
|
+
* until idle clients close: a suspended instance's idle timer does not run, so
|
|
76
|
+
* without it every warm instance pins its sockets. Off Vercel that is a no-op.
|
|
54
77
|
*/
|
|
55
78
|
export declare const createPool: (config?: CreatePoolConfig) => Pool;
|
|
56
79
|
//# sourceMappingURL=pool.d.ts.map
|
package/dist/pool.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pool.d.ts","sourceRoot":"","sources":["../src/pool.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"pool.d.ts","sourceRoot":"","sources":["../src/pool.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAmB,MAAM,IAAI,CAAC;AAG3C,MAAM,WAAW,gBAAgB;IAChC,8EAA8E;IAC9E,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,sEAAsE;IACtE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;;OAGG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B;;;;OAIG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;CACzB;AAED;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,OAAO,CAAC;AAI3C;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,cAAc,CAAC;AAmBnD;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,eAAe,WAAY,MAAM,GAAG,SAAS,KAAG,MAAM,GAAG,SACN,CAAC;AAEjE;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,eAAO,MAAM,UAAU,YAAY,gBAAgB,KAAQ,IAgE1D,CAAC"}
|
package/dist/pool.js
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
+
import { attachDatabasePool } from "@vercel/functions/db-connections";
|
|
1
2
|
import { Pool } from "pg";
|
|
2
|
-
import { dbEnv, getDatabaseUrl } from "./keys.js";
|
|
3
|
+
import { dbEnv, getDatabaseUrl, poolMaxFromEnv } from "./keys.js";
|
|
4
|
+
/**
|
|
5
|
+
* Idle timeout for remote pools. Short, because a serverless instance that
|
|
6
|
+
* goes quiet should hand its connections back to the server rather than pin
|
|
7
|
+
* them until it is reaped.
|
|
8
|
+
*/
|
|
9
|
+
export const REMOTE_IDLE_TIMEOUT_MS = 5000;
|
|
3
10
|
const LOCAL_HOSTS = new Set(["127.0.0.1", "localhost", "::1", "[::1]"]);
|
|
4
11
|
/**
|
|
5
12
|
* The `application_name` that marks a URL as the PGlite socket. The harness
|
|
@@ -57,7 +64,10 @@ export const normalizeCaCert = (caCert) => caCert?.includes("\\n") ? caCert.repl
|
|
|
57
64
|
*
|
|
58
65
|
* On Vercel Fluid / serverless, hold the pool on `globalThis` so warm
|
|
59
66
|
* invocations reuse it instead of opening a connection per request (see the
|
|
60
|
-
* app-side `src/lib/db.ts` pattern in docs/db-package.md).
|
|
67
|
+
* app-side `src/lib/db.ts` pattern in docs/db-package.md). A remote pool is
|
|
68
|
+
* registered with Vercel's `attachDatabasePool`, which keeps the instance alive
|
|
69
|
+
* until idle clients close: a suspended instance's idle timer does not run, so
|
|
70
|
+
* without it every warm instance pins its sockets. Off Vercel that is a no-op.
|
|
61
71
|
*/
|
|
62
72
|
export const createPool = (config = {}) => {
|
|
63
73
|
// Resolve the connection string WITHOUT throwing when it is simply absent.
|
|
@@ -83,25 +93,43 @@ export const createPool = (config = {}) => {
|
|
|
83
93
|
// production DATABASE_CA_CERT, and a local server speaks no TLS. The PGlite
|
|
84
94
|
// socket also ignores DATABASE_POOL_MAX: it serves one session. An explicit
|
|
85
95
|
// `config.max` in code still applies.
|
|
86
|
-
const max = config.max ?? (isPglite(url) ? 1 : env?.poolMax);
|
|
87
|
-
const base =
|
|
96
|
+
const max = config.max ?? (isPglite(url) ? 1 : (env?.poolMax ?? poolMaxFromEnv()));
|
|
97
|
+
const base = {
|
|
98
|
+
...(max === undefined ? {} : { max }),
|
|
99
|
+
...(config.applicationName === undefined
|
|
100
|
+
? {}
|
|
101
|
+
: { application_name: config.applicationName }),
|
|
102
|
+
};
|
|
88
103
|
if (local) {
|
|
89
|
-
return new Pool({
|
|
104
|
+
return new Pool({
|
|
105
|
+
...base,
|
|
106
|
+
...(config.idleTimeoutMillis === undefined
|
|
107
|
+
? {}
|
|
108
|
+
: { idleTimeoutMillis: config.idleTimeoutMillis }),
|
|
109
|
+
connectionString,
|
|
110
|
+
});
|
|
90
111
|
}
|
|
112
|
+
const idleTimeoutMillis = config.idleTimeoutMillis ?? REMOTE_IDLE_TIMEOUT_MS;
|
|
91
113
|
if (caCert) {
|
|
92
|
-
return new Pool({
|
|
114
|
+
return attached(new Pool({
|
|
93
115
|
...base,
|
|
116
|
+
idleTimeoutMillis,
|
|
94
117
|
connectionString,
|
|
95
118
|
ssl: { ca: caCert, rejectUnauthorized: true },
|
|
96
|
-
});
|
|
119
|
+
}));
|
|
97
120
|
}
|
|
98
121
|
const remote = new URL(connectionString);
|
|
99
122
|
remote.searchParams.delete("sslmode");
|
|
100
|
-
return new Pool({
|
|
123
|
+
return attached(new Pool({
|
|
101
124
|
...base,
|
|
125
|
+
idleTimeoutMillis,
|
|
102
126
|
connectionString: remote.toString(),
|
|
103
127
|
ssl: { rejectUnauthorized: false },
|
|
104
|
-
});
|
|
128
|
+
}));
|
|
129
|
+
};
|
|
130
|
+
const attached = (pool) => {
|
|
131
|
+
attachDatabasePool(pool);
|
|
132
|
+
return pool;
|
|
105
133
|
};
|
|
106
134
|
/**
|
|
107
135
|
* A pool that constructs cleanly but rejects any real operation with the clear
|
package/dist/pool.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pool.js","sourceRoot":"","sources":["../src/pool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAmB,MAAM,IAAI,CAAC;AAC3C,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"pool.js","sourceRoot":"","sources":["../src/pool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC;AACtE,OAAO,EAAE,IAAI,EAAmB,MAAM,IAAI,CAAC;AAC3C,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,MAA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KAAK,GAAG,MAA2B,CAAC;IACzC,IAAI,CAAC,OAAO,GAAG,MAA6B,CAAC;IAC7C,OAAO,IAAI,CAAC;AACb,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ingram-tech/nk-db",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.7.0",
|
|
4
4
|
"description": "The Ingram Postgres data layer: one TLS-aware pg pool, raw-SQL helpers, Drizzle wiring, and a PGlite (no-Docker) dev/test harness for Next.js sites.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -19,9 +19,9 @@
|
|
|
19
19
|
"!src/**/*.test.tsx"
|
|
20
20
|
],
|
|
21
21
|
"bin": {
|
|
22
|
-
"nk-pglite-dev": "
|
|
23
|
-
"nk-pg-migrate": "
|
|
24
|
-
"nk-pg-fingerprint": "
|
|
22
|
+
"nk-pglite-dev": "dist/pglite/dev.js",
|
|
23
|
+
"nk-pg-migrate": "dist/migrate-bin.js",
|
|
24
|
+
"nk-pg-fingerprint": "dist/fingerprint-bin.js"
|
|
25
25
|
},
|
|
26
26
|
"exports": {
|
|
27
27
|
".": {
|
|
@@ -63,8 +63,9 @@
|
|
|
63
63
|
"test": "vitest run"
|
|
64
64
|
},
|
|
65
65
|
"dependencies": {
|
|
66
|
+
"@vercel/functions": "^3.12.1",
|
|
66
67
|
"id758": "^1.1.0",
|
|
67
|
-
"zod": "^4.6.
|
|
68
|
+
"zod": "^4.6.7"
|
|
68
69
|
},
|
|
69
70
|
"peerDependencies": {
|
|
70
71
|
"@electric-sql/pglite": ">=0.5.0",
|
|
@@ -81,14 +82,14 @@
|
|
|
81
82
|
}
|
|
82
83
|
},
|
|
83
84
|
"devDependencies": {
|
|
84
|
-
"@electric-sql/pglite": "^0.5.
|
|
85
|
-
"@electric-sql/pglite-socket": "^0.2.
|
|
86
|
-
"@ingram-tech/nk-dev": "0.
|
|
87
|
-
"@types/node": "^26.6.
|
|
85
|
+
"@electric-sql/pglite": "^0.5.17",
|
|
86
|
+
"@electric-sql/pglite-socket": "^0.2.20",
|
|
87
|
+
"@ingram-tech/nk-dev": "0.28.0",
|
|
88
|
+
"@types/node": "^26.6.11",
|
|
88
89
|
"@types/pg": "^8.23.1",
|
|
89
90
|
"drizzle-orm": "^0.45.4",
|
|
90
91
|
"pg": "^8.23.1",
|
|
91
92
|
"typescript": "^7.0.2",
|
|
92
|
-
"vitest": "^5.
|
|
93
|
+
"vitest": "^5.1.2"
|
|
93
94
|
}
|
|
94
95
|
}
|
package/src/keys.ts
CHANGED
|
@@ -74,5 +74,22 @@ export const dbEnv = (): DbEnv => {
|
|
|
74
74
|
};
|
|
75
75
|
};
|
|
76
76
|
|
|
77
|
+
/**
|
|
78
|
+
* The validated DATABASE_POOL_MAX alone. `createPool` applies it even when the
|
|
79
|
+
* caller passes its own connection string: the cap is about the process's
|
|
80
|
+
* share of the server's connections, whichever url the pool dials.
|
|
81
|
+
*/
|
|
82
|
+
export const poolMaxFromEnv = (): number | undefined => {
|
|
83
|
+
const result = schema.shape.DATABASE_POOL_MAX.safeParse(
|
|
84
|
+
process.env.DATABASE_POOL_MAX,
|
|
85
|
+
);
|
|
86
|
+
if (!result.success) {
|
|
87
|
+
throw new Error(
|
|
88
|
+
`@ingram-tech/nk-db: invalid environment — DATABASE_POOL_MAX: ${result.error.issues[0]?.message ?? "invalid"}`,
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
return result.data;
|
|
92
|
+
};
|
|
93
|
+
|
|
77
94
|
/** Whether a database connection string is present (degrade gracefully if not). */
|
|
78
95
|
export const isConfigured = (): boolean => getDatabaseUrl() !== undefined;
|
package/src/pool.ts
CHANGED
|
@@ -1,15 +1,37 @@
|
|
|
1
|
+
import { attachDatabasePool } from "@vercel/functions/db-connections";
|
|
1
2
|
import { Pool, type PoolConfig } from "pg";
|
|
2
|
-
import { dbEnv, getDatabaseUrl } from "./keys.js";
|
|
3
|
+
import { dbEnv, getDatabaseUrl, poolMaxFromEnv } from "./keys.js";
|
|
3
4
|
|
|
4
5
|
export interface CreatePoolConfig {
|
|
5
6
|
/** Override the resolved connection string (defaults to the env contract). */
|
|
6
7
|
connectionString?: string;
|
|
7
8
|
/** PEM CA cert; when set, the server cert + hostname are verified. */
|
|
8
9
|
caCert?: string;
|
|
9
|
-
/**
|
|
10
|
+
/**
|
|
11
|
+
* Pool size cap. Defaults to env DATABASE_POOL_MAX (applied whether or not
|
|
12
|
+
* `connectionString` is passed), or 1 for the PGlite socket.
|
|
13
|
+
*/
|
|
10
14
|
max?: number;
|
|
15
|
+
/**
|
|
16
|
+
* How long an idle client stays open. Defaults to
|
|
17
|
+
* {@link REMOTE_IDLE_TIMEOUT_MS} on a remote server, pg's 10 s locally.
|
|
18
|
+
*/
|
|
19
|
+
idleTimeoutMillis?: number;
|
|
20
|
+
/**
|
|
21
|
+
* `application_name` the server sees for this pool's connections, so a
|
|
22
|
+
* process with several pools can tell them apart in `pg_stat_activity` or
|
|
23
|
+
* a pooler's `SHOW CLIENTS`. One set in the url wins.
|
|
24
|
+
*/
|
|
25
|
+
applicationName?: string;
|
|
11
26
|
}
|
|
12
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Idle timeout for remote pools. Short, because a serverless instance that
|
|
30
|
+
* goes quiet should hand its connections back to the server rather than pin
|
|
31
|
+
* them until it is reaped.
|
|
32
|
+
*/
|
|
33
|
+
export const REMOTE_IDLE_TIMEOUT_MS = 5000;
|
|
34
|
+
|
|
13
35
|
const LOCAL_HOSTS = new Set(["127.0.0.1", "localhost", "::1", "[::1]"]);
|
|
14
36
|
|
|
15
37
|
/**
|
|
@@ -75,7 +97,10 @@ export const normalizeCaCert = (caCert: string | undefined): string | undefined
|
|
|
75
97
|
*
|
|
76
98
|
* On Vercel Fluid / serverless, hold the pool on `globalThis` so warm
|
|
77
99
|
* invocations reuse it instead of opening a connection per request (see the
|
|
78
|
-
* app-side `src/lib/db.ts` pattern in docs/db-package.md).
|
|
100
|
+
* app-side `src/lib/db.ts` pattern in docs/db-package.md). A remote pool is
|
|
101
|
+
* registered with Vercel's `attachDatabasePool`, which keeps the instance alive
|
|
102
|
+
* until idle clients close: a suspended instance's idle timer does not run, so
|
|
103
|
+
* without it every warm instance pins its sockets. Off Vercel that is a no-op.
|
|
79
104
|
*/
|
|
80
105
|
export const createPool = (config: CreatePoolConfig = {}): Pool => {
|
|
81
106
|
// Resolve the connection string WITHOUT throwing when it is simply absent.
|
|
@@ -102,27 +127,50 @@ export const createPool = (config: CreatePoolConfig = {}): Pool => {
|
|
|
102
127
|
// production DATABASE_CA_CERT, and a local server speaks no TLS. The PGlite
|
|
103
128
|
// socket also ignores DATABASE_POOL_MAX: it serves one session. An explicit
|
|
104
129
|
// `config.max` in code still applies.
|
|
105
|
-
const max = config.max ?? (isPglite(url) ? 1 : env?.poolMax);
|
|
130
|
+
const max = config.max ?? (isPglite(url) ? 1 : (env?.poolMax ?? poolMaxFromEnv()));
|
|
106
131
|
|
|
107
|
-
const base: PoolConfig =
|
|
132
|
+
const base: PoolConfig = {
|
|
133
|
+
...(max === undefined ? {} : { max }),
|
|
134
|
+
...(config.applicationName === undefined
|
|
135
|
+
? {}
|
|
136
|
+
: { application_name: config.applicationName }),
|
|
137
|
+
};
|
|
108
138
|
|
|
109
139
|
if (local) {
|
|
110
|
-
return new Pool({ ...base, connectionString });
|
|
111
|
-
}
|
|
112
|
-
if (caCert) {
|
|
113
140
|
return new Pool({
|
|
114
141
|
...base,
|
|
142
|
+
...(config.idleTimeoutMillis === undefined
|
|
143
|
+
? {}
|
|
144
|
+
: { idleTimeoutMillis: config.idleTimeoutMillis }),
|
|
115
145
|
connectionString,
|
|
116
|
-
ssl: { ca: caCert, rejectUnauthorized: true },
|
|
117
146
|
});
|
|
118
147
|
}
|
|
148
|
+
const idleTimeoutMillis = config.idleTimeoutMillis ?? REMOTE_IDLE_TIMEOUT_MS;
|
|
149
|
+
if (caCert) {
|
|
150
|
+
return attached(
|
|
151
|
+
new Pool({
|
|
152
|
+
...base,
|
|
153
|
+
idleTimeoutMillis,
|
|
154
|
+
connectionString,
|
|
155
|
+
ssl: { ca: caCert, rejectUnauthorized: true },
|
|
156
|
+
}),
|
|
157
|
+
);
|
|
158
|
+
}
|
|
119
159
|
const remote = new URL(connectionString);
|
|
120
160
|
remote.searchParams.delete("sslmode");
|
|
121
|
-
return
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
161
|
+
return attached(
|
|
162
|
+
new Pool({
|
|
163
|
+
...base,
|
|
164
|
+
idleTimeoutMillis,
|
|
165
|
+
connectionString: remote.toString(),
|
|
166
|
+
ssl: { rejectUnauthorized: false },
|
|
167
|
+
}),
|
|
168
|
+
);
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const attached = (pool: Pool): Pool => {
|
|
172
|
+
attachDatabasePool(pool);
|
|
173
|
+
return pool;
|
|
126
174
|
};
|
|
127
175
|
|
|
128
176
|
/**
|