@ingram-tech/nk-db 1.3.0 → 1.3.1
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/pool.d.ts.map +1 -1
- package/dist/pool.js +43 -3
- package/dist/pool.js.map +1 -1
- package/package.json +1 -1
package/dist/pool.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pool.d.ts","sourceRoot":"","sources":["../src/pool.ts"],"names":[],"mappings":"AAAA,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,iFAAiF;IACjF,GAAG,CAAC,EAAE,MAAM,CAAC;CACb;AAcD;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,eAAe,WAAY,MAAM,GAAG,SAAS,KAAG,MAAM,GAAG,SACN,CAAC;AAEjE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,eAAO,MAAM,UAAU,YAAY,gBAAgB,KAAQ,
|
|
1
|
+
{"version":3,"file":"pool.d.ts","sourceRoot":"","sources":["../src/pool.ts"],"names":[],"mappings":"AAAA,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,iFAAiF;IACjF,GAAG,CAAC,EAAE,MAAM,CAAC;CACb;AAcD;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,eAAe,WAAY,MAAM,GAAG,SAAS,KAAG,MAAM,GAAG,SACN,CAAC;AAEjE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,eAAO,MAAM,UAAU,YAAY,gBAAgB,KAAQ,IA6C1D,CAAC"}
|
package/dist/pool.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Pool } from "pg";
|
|
2
|
-
import { dbEnv } from "./keys.js";
|
|
2
|
+
import { dbEnv, getDatabaseUrl } from "./keys.js";
|
|
3
3
|
const LOCAL_HOSTS = new Set(["127.0.0.1", "localhost", "::1", "[::1]"]);
|
|
4
4
|
const isLocal = (connectionString) => {
|
|
5
5
|
// Parse the URL: a substring check would misclassify a remote URL whose
|
|
@@ -49,10 +49,21 @@ export const normalizeCaCert = (caCert) => caCert?.includes("\\n") ? caCert.repl
|
|
|
49
49
|
* app-side `src/lib/db.ts` pattern in docs/db-package.md).
|
|
50
50
|
*/
|
|
51
51
|
export const createPool = (config = {}) => {
|
|
52
|
-
|
|
52
|
+
// Resolve the connection string WITHOUT throwing when it is simply absent.
|
|
53
|
+
// createPool runs at module load in every app's `lib/db`, so importing that
|
|
54
|
+
// module — and therefore `next build` collecting page data, a unit test, or
|
|
55
|
+
// a CLI tool that never queries — must stay side-effect-free. Validate the
|
|
56
|
+
// full env (which *does* throw fast on a present-but-invalid value) only once
|
|
57
|
+
// a URL is known to exist; when nothing is configured, defer the error to
|
|
58
|
+
// first use via `unconfiguredPool`.
|
|
59
|
+
const env = config.connectionString !== undefined
|
|
60
|
+
? undefined
|
|
61
|
+
: getDatabaseUrl() !== undefined
|
|
62
|
+
? dbEnv()
|
|
63
|
+
: undefined;
|
|
53
64
|
const connectionString = config.connectionString ?? env?.connectionString;
|
|
54
65
|
if (!connectionString) {
|
|
55
|
-
|
|
66
|
+
return unconfiguredPool();
|
|
56
67
|
}
|
|
57
68
|
const caCert = normalizeCaCert(config.caCert ?? env?.caCert);
|
|
58
69
|
const local = isLocal(connectionString);
|
|
@@ -80,4 +91,33 @@ export const createPool = (config = {}) => {
|
|
|
80
91
|
ssl: { rejectUnauthorized: false },
|
|
81
92
|
});
|
|
82
93
|
};
|
|
94
|
+
/**
|
|
95
|
+
* A pool that constructs cleanly but rejects any real operation with the clear
|
|
96
|
+
* "set DATABASE_URL" error. Returned by {@link createPool} when no connection
|
|
97
|
+
* string is configured.
|
|
98
|
+
*
|
|
99
|
+
* Constructing the pool — which happens at module load in every app's `lib/db`,
|
|
100
|
+
* and therefore during `next build`'s page-data collection and in unit tests —
|
|
101
|
+
* must not throw just because a database isn't wired up. A process that then
|
|
102
|
+
* runs a real query without a URL is genuinely misconfigured and fails fast and
|
|
103
|
+
* legibly here; the env is fixed at process start, so this is never a transient
|
|
104
|
+
* miss. `query` and `connect` are the only entry points that reach the wire
|
|
105
|
+
* (Drizzle and `createQueries` go through them), so guarding both is sufficient.
|
|
106
|
+
*
|
|
107
|
+
* The rejection is deferred to a macrotask, not returned already-rejected, so it
|
|
108
|
+
* mimics real connection I/O: an async framework that inspects the in-flight
|
|
109
|
+
* promise before it settles — e.g. Next.js partial prerendering deciding a
|
|
110
|
+
* segment is dynamic — sees a pending promise and postpones it, exactly as it
|
|
111
|
+
* would for a real (reachable or not) pool. An already-rejected promise would
|
|
112
|
+
* instead surface as a synchronous render error at build.
|
|
113
|
+
*/
|
|
114
|
+
const unconfiguredPool = () => {
|
|
115
|
+
const pool = new Pool();
|
|
116
|
+
const reject = () => new Promise((_resolve, rej) => {
|
|
117
|
+
setTimeout(() => rej(new Error("@ingram-tech/nk-db: no database connection string — set DATABASE_URL.")), 0);
|
|
118
|
+
});
|
|
119
|
+
pool.query = reject;
|
|
120
|
+
pool.connect = reject;
|
|
121
|
+
return pool;
|
|
122
|
+
};
|
|
83
123
|
//# sourceMappingURL=pool.js.map
|
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,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"pool.js","sourceRoot":"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AM,IAAI,GAAG,IAAI,IAAI,EAAE,CAAC;IACxB,MAAM,MAAM,GAAG,GAAmB,EAAE,CACnC,IAAI,OAAO,CAAC,CAAC,QAAQ,EAAE,GAAG,EAAE,EAAE;QAC7B,UAAU,CACT,GAAG,EAAE,CACJ,GAAG,CACF,IAAI,KAAK,CACR,uEAAuE,CACvE,CACD,EACF,CAAC,CACD,CAAC;IACH,CAAC,CAAC,CAAC;IACJ,IAAI,CAAC,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": "1.3.
|
|
3
|
+
"version": "1.3.1",
|
|
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",
|