@wwchen/stack 0.6.2 → 0.6.4
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/blocks/env.example +1 -2
- package/dist/auth/better-auth/index.d.ts +6 -17
- package/dist/auth/better-auth/index.js +11 -33
- package/dist/auth/better-auth/tables.js +5 -14
- package/dist/auth/better-auth/users.d.ts +2 -7
- package/dist/auth/better-auth/users.js +3 -9
- package/dist/auth/index.d.ts +6 -17
- package/dist/auth/index.js +5 -19
- package/dist/auth/proxy.d.ts +1 -3
- package/dist/auth/proxy.js +6 -19
- package/dist/auth/safe-next.d.ts +2 -8
- package/dist/auth/safe-next.js +2 -8
- package/dist/auth/types.d.ts +1 -2
- package/dist/auth/types.js +1 -2
- package/dist/cli/context.d.ts +1 -3
- package/dist/cli/index.js +1 -2
- package/dist/cli/neon.d.ts +11 -44
- package/dist/cli/neon.js +15 -56
- package/dist/cli/sync-blocks.d.ts +1 -3
- package/dist/cli/sync-blocks.js +2 -6
- package/dist/cli/vercel-build.js +5 -16
- package/dist/configs/vitest.js +3 -7
- package/dist/connections/index.d.ts +2 -8
- package/dist/connections/index.js +2 -8
- package/dist/connections/providers.d.ts +1 -3
- package/dist/connections/table.d.ts +1 -2
- package/dist/connections/table.js +1 -2
- package/dist/db/explaining-pool.d.ts +1 -6
- package/dist/db/explaining-pool.js +3 -10
- package/dist/db/index.d.ts +1 -3
- package/dist/db/index.js +5 -13
- package/dist/db/migration-status.d.ts +1 -4
- package/dist/db/migration-status.js +1 -4
- package/dist/db/ssl.d.ts +1 -5
- package/dist/db/ssl.js +1 -5
- package/dist/db/target.d.ts +2 -9
- package/dist/db/target.js +2 -9
- package/dist/db/testing/index.d.ts +3 -7
- package/dist/db/testing/index.js +3 -7
- package/dist/db/testing/isolate.js +1 -2
- package/dist/db/testing/pglite.d.ts +2 -6
- package/dist/db/testing/pglite.js +2 -6
- package/dist/env/define-env.d.ts +3 -9
- package/dist/env/define-env.js +6 -16
- package/dist/env/load-env-file.d.ts +4 -9
- package/dist/env/load-env-file.js +5 -11
- package/dist/env/stack-env.d.ts +1 -3
- package/dist/env/stack-env.js +3 -7
- package/dist/env/testing.d.ts +1 -3
- package/dist/env/testing.js +2 -5
- package/dist/http/client-ip.d.ts +2 -8
- package/dist/http/client-ip.js +3 -10
- package/dist/http/guards.d.ts +3 -10
- package/dist/http/guards.js +4 -12
- package/dist/http/rate-limit.d.ts +4 -10
- package/dist/http/rate-limit.js +7 -19
- package/dist/jobs/index.d.ts +2 -9
- package/dist/jobs/index.js +2 -9
- package/dist/mail/index.d.ts +2 -9
- package/dist/mail/index.js +2 -7
- package/dist/next/api.d.ts +6 -17
- package/dist/next/api.js +6 -15
- package/dist/next/config.js +10 -32
- package/dist/next/cron.d.ts +2 -6
- package/dist/next/cron.js +3 -8
- package/dist/next/health.d.ts +3 -8
- package/dist/next/health.js +3 -8
- package/dist/testing/next.d.ts +1 -4
- package/dist/testing/next.js +2 -7
- package/dist/ui/app.css +5 -16
- package/dist/ui/globals.css +6 -20
- package/dist/ui/icons.d.ts +1 -3
- package/dist/ui/icons.js +3 -10
- package/dist/ui/index.d.ts +4 -12
- package/dist/ui/index.js +4 -12
- package/dist/ui/theme.d.ts +3 -9
- package/dist/ui/theme.js +3 -9
- package/package.json +3 -3
package/dist/cli/neon.js
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
// Your Neon branches, driven through the Neon CLI rather than a `.env` file. `stack dev-setup`
|
|
2
|
-
// makes your dev branch, `stack with-neon` hands its connection to every local command, and
|
|
3
|
-
// `stack neon seed` / `reset-preview` keep the two shared branches. Needs the Neon CLI signed in
|
|
4
|
-
// (`neon auth`) or NEON_API_KEY, and NEON_PROJECT_ID in the environment.
|
|
1
|
+
// Your Neon branches, driven through the Neon CLI rather than a `.env` file. `stack dev-setup` makes your dev branch, `stack with-neon` hands its connection to every local command, and `stack neon seed` / `reset-preview` keep the two shared branches. Needs the Neon CLI signed in (`neon auth`) or NEON_API_KEY, and NEON_PROJECT_ID in the environment.
|
|
5
2
|
//
|
|
6
3
|
// Runs before any database is set, so it reads its own `deps.env`, never the app's validated env.
|
|
7
4
|
//
|
|
8
|
-
// The branch tree: production -> `seed` (the app's migrations and seed rows, no production data)
|
|
9
|
-
// -> `dev/<user>` for each developer and one shared `preview`. Nothing local or preview is ever
|
|
10
|
-
// cut from production, so production's data never leaves it. Production is the project's default
|
|
11
|
-
// branch, whatever it is named (Neon calls a new project's `main`): `defaultBranch`.
|
|
5
|
+
// The branch tree: production -> `seed` (the app's migrations and seed rows, no production data) -> `dev/<user>` for each developer and one shared `preview`. Nothing local or preview is ever cut from production, so production's data never leaves it. Production is the project's default branch, whatever it is named (Neon calls a new project's `main`): `defaultBranch`.
|
|
12
6
|
import { endpointOf } from "../db/index.js";
|
|
13
7
|
import { CliError, importApp, loadAppEnv } from "./context.js";
|
|
14
8
|
import { readHosting } from "./doctor.js";
|
|
@@ -24,10 +18,7 @@ export function projectId(env) {
|
|
|
24
18
|
return id;
|
|
25
19
|
}
|
|
26
20
|
/**
|
|
27
|
-
* Refuses a dev branch name that is not a dev branch: production (whatever this project calls its
|
|
28
|
-
* default branch, and the name `production` itself), the shared `seed` and `preview`, and `seed-next`
|
|
29
|
-
* (the seed being built, which may briefly hold production's data). One check
|
|
30
|
-
* for every command that uses your dev branch: with-neon, dev-setup and user:create.
|
|
21
|
+
* Refuses a dev branch name that is not a dev branch: production (whatever this project calls its default branch, and the name `production` itself), the shared `seed` and `preview`, and `seed-next` (the seed being built, which may briefly hold production's data). One check for every command that uses your dev branch: with-neon, dev-setup and user:create.
|
|
31
22
|
*/
|
|
32
23
|
export function assertDevBranchName(name, production) {
|
|
33
24
|
const yours = "set NEON_DEV_BRANCH to your own branch (default dev/<os user>)";
|
|
@@ -67,8 +58,7 @@ export function defaultBranch(branches) {
|
|
|
67
58
|
return production;
|
|
68
59
|
}
|
|
69
60
|
/**
|
|
70
|
-
* What `.env` used to carry for local work: `branch`'s URLs, and the host of production (the
|
|
71
|
-
* default branch; `production` when the caller already has it, else listed).
|
|
61
|
+
* What `.env` used to carry for local work: `branch`'s URLs, and the host of production (the default branch; `production` when the caller already has it, else listed).
|
|
72
62
|
*/
|
|
73
63
|
export async function branchEnv(cli, branch, production) {
|
|
74
64
|
const productionId = (production ?? defaultBranch(await listBranches(cli))).id;
|
|
@@ -113,13 +103,9 @@ export async function migrate(cli, env) {
|
|
|
113
103
|
return (await cli.exec("pnpm", ["exec", "drizzle-kit", "migrate"], { env, inherit: true })).code;
|
|
114
104
|
}
|
|
115
105
|
/**
|
|
116
|
-
* `stack with-neon <command> [args]` — runs a local command against your Neon dev branch with no
|
|
117
|
-
* `.env`: the connection strings and production's host come from the Neon CLI on every run.
|
|
106
|
+
* `stack with-neon <command> [args]` — runs a local command against your Neon dev branch with no `.env`: the connection strings and production's host come from the Neon CLI on every run.
|
|
118
107
|
*
|
|
119
|
-
* Steps aside, and runs the command untouched, when something else already says where the database
|
|
120
|
-
* is: DATABASE_URL in the environment (Vercel, an e2e run, a local Postgres), ENV_FILE
|
|
121
|
-
* (a deliberate production command). A `.env` never decides: what this sets wins over it, in Next
|
|
122
|
-
* and in `loadEnvFile` alike.
|
|
108
|
+
* Steps aside, and runs the command untouched, when something else already says where the database is: DATABASE_URL in the environment (Vercel, an e2e run, a local Postgres), ENV_FILE (a deliberate production command). A `.env` never decides: what this sets wins over it, in Next and in `loadEnvFile` alike.
|
|
123
109
|
*/
|
|
124
110
|
export async function withNeon(cli, [command, ...args]) {
|
|
125
111
|
if (!command) {
|
|
@@ -130,8 +116,7 @@ export async function withNeon(cli, [command, ...args]) {
|
|
|
130
116
|
return (await cli.exec(command, args, { env, inherit: true })).code;
|
|
131
117
|
}
|
|
132
118
|
/**
|
|
133
|
-
* Your dev branch's `branchEnv`, as `with-neon` hands it to a command; nothing (`{}`) when
|
|
134
|
-
* DATABASE_URL or ENV_FILE already says where the database is. Refuses the default branch.
|
|
119
|
+
* Your dev branch's `branchEnv`, as `with-neon` hands it to a command; nothing (`{}`) when DATABASE_URL or ENV_FILE already says where the database is. Refuses the default branch.
|
|
135
120
|
*/
|
|
136
121
|
export async function devBranchEnv(cli) {
|
|
137
122
|
if (cli.env.DATABASE_URL || cli.env.ENV_FILE)
|
|
@@ -172,11 +157,7 @@ export async function previewBranchEnv(cli) {
|
|
|
172
157
|
return env;
|
|
173
158
|
}
|
|
174
159
|
/**
|
|
175
|
-
* `stack dev-setup` — once per developer: makes your Neon dev branch from `seed` (never from
|
|
176
|
-
* production) if it does not exist yet (`dev/<os user>`, or NEON_DEV_BRANCH) and migrates it. Safe
|
|
177
|
-
* to re-run. Sign-in needs nothing on the branch: Better Auth runs in the app, with a development
|
|
178
|
-
* BETTER_AUTH_SECRET and magic links written to the sink file. There is no `.env` to write:
|
|
179
|
-
* `stack with-neon` reads the branch from the Neon CLI on every command.
|
|
160
|
+
* `stack dev-setup` — once per developer: makes your Neon dev branch from `seed` (never from production) if it does not exist yet (`dev/<os user>`, or NEON_DEV_BRANCH) and migrates it. Safe to re-run. Sign-in needs nothing on the branch: Better Auth runs in the app, with a development BETTER_AUTH_SECRET and magic links written to the sink file. There is no `.env` to write: `stack with-neon` reads the branch from the Neon CLI on every command.
|
|
180
161
|
*/
|
|
181
162
|
export async function devSetup(cli) {
|
|
182
163
|
const name = devBranchName(cli);
|
|
@@ -205,28 +186,16 @@ export async function devSetup(cli) {
|
|
|
205
186
|
return status;
|
|
206
187
|
}
|
|
207
188
|
/**
|
|
208
|
-
* Schemas Neon's own services install, which hold functions and settings rather than anyone's rows:
|
|
209
|
-
* `auth` is the pg_session_jwt extension the Data API installs (Neon: "do not install this extension
|
|
210
|
-
* manually"), `pgrst` is the Data API's PostgREST configuration. Left in place so the branch keeps
|
|
211
|
-
* working like any other. Neon Auth's `neon_auth` is *not* here: it holds users.
|
|
189
|
+
* Schemas Neon's own services install, which hold functions and settings rather than anyone's rows: `auth` is the pg_session_jwt extension the Data API installs (Neon: "do not install this extension manually"), `pgrst` is the Data API's PostgREST configuration. Left in place so the branch keeps working like any other. Neon Auth's `neon_auth` is *not* here: it holds users.
|
|
212
190
|
*/
|
|
213
191
|
export const NEON_SCHEMAS = ["auth", "pgrst"];
|
|
214
192
|
const isKept = (schema) => schema === "information_schema" || schema.startsWith("pg_") || NEON_SCHEMAS.includes(schema);
|
|
215
193
|
const quoteIdent = (name) => `"${name.replaceAll('"', '""')}"`;
|
|
216
194
|
const quoteLiteral = (value) => `'${value.replaceAll("'", "''")}'`;
|
|
217
195
|
/**
|
|
218
|
-
* Empties a database down to an empty `public`: drops every schema but Postgres's own
|
|
219
|
-
* (`pg_catalog`, `pg_toast*`, `information_schema`, any `pg_*`) and Neon's service schemas, with
|
|
220
|
-
* everything in them — tables, rows, drizzle's journal, Neon Auth's users. Then the migrations
|
|
221
|
-
* run from nothing, as on a new database. A schema-only branch needs this as much as a full copy:
|
|
222
|
-
* it keeps drizzle's journal *table* but not its rows, so migrate would re-run 0000 into tables
|
|
223
|
-
* that already exist. Large objects belong to the database rather than to a schema, so no drop
|
|
224
|
-
* removes them: every one is unlinked too.
|
|
196
|
+
* Empties a database down to an empty `public`: drops every schema but Postgres's own (`pg_catalog`, `pg_toast*`, `information_schema`, any `pg_*`) and Neon's service schemas, with everything in them — tables, rows, drizzle's journal, Neon Auth's users. Then the migrations run from nothing, as on a new database. A schema-only branch needs this as much as a full copy: it keeps drizzle's journal *table* but not its rows, so migrate would re-run 0000 into tables that already exist. Large objects belong to the database rather than to a schema, so no drop removes them: every one is unlinked too.
|
|
225
197
|
*
|
|
226
|
-
* Neon's schemas are kept by name, so first it checks they hold what Neon puts there: functions and
|
|
227
|
-
* settings, no rows. A table, partitioned table, materialized view, sequence or foreign table in
|
|
228
|
-
* one (an app's own `auth.accounts`, say) would carry production's rows into the seed, so it
|
|
229
|
-
* refuses before dropping anything.
|
|
198
|
+
* Neon's schemas are kept by name, so first it checks they hold what Neon puts there: functions and settings, no rows. A table, partitioned table, materialized view, sequence or foreign table in one (an app's own `auth.accounts`, say) would carry production's rows into the seed, so it refuses before dropping anything.
|
|
230
199
|
*/
|
|
231
200
|
export async function emptyDatabase(sql) {
|
|
232
201
|
const held = await sql("SELECT n.nspname, c.relname FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace " +
|
|
@@ -247,17 +216,9 @@ export async function emptyDatabase(sql) {
|
|
|
247
216
|
return dropped;
|
|
248
217
|
}
|
|
249
218
|
/**
|
|
250
|
-
* `stack neon seed` — (re)creates the `seed` branch: exactly the app's migrations plus its seed
|
|
251
|
-
* rows, and none of production's data. Cuts it from production (the project's default branch) as
|
|
252
|
-
* Neon's `--schema-only` branch (a normal branch where that is unsupported), empties it of every
|
|
253
|
-
* non-system schema either way (`emptyDatabase`), migrates it from nothing and runs the app's
|
|
254
|
-
* `db/seed.ts`: `export default async function seed(db)`.
|
|
219
|
+
* `stack neon seed` — (re)creates the `seed` branch: exactly the app's migrations plus its seed rows, and none of production's data. Cuts it from production (the project's default branch) as Neon's `--schema-only` branch (a normal branch where that is unsupported), empties it of every non-system schema either way (`emptyDatabase`), migrates it from nothing and runs the app's `db/seed.ts`: `export default async function seed(db)`.
|
|
255
220
|
*
|
|
256
|
-
* The new seed is built as `seed-next` and takes the name `seed` only once it is clean, so a
|
|
257
|
-
* failure part-way never leaves a branch holding production's data under the name every dev and
|
|
258
|
-
* preview branch is cut from; `seed-next` is deleted on any failure. Neon will not delete a branch
|
|
259
|
-
* that has children, so a seed with dev/preview branches on it is refused up front: remove them
|
|
260
|
-
* yourself (this never deletes them).
|
|
221
|
+
* The new seed is built as `seed-next` and takes the name `seed` only once it is clean, so a failure part-way never leaves a branch holding production's data under the name every dev and preview branch is cut from; `seed-next` is deleted on any failure. Neon will not delete a branch that has children, so a seed with dev/preview branches on it is refused up front: remove them yourself (this never deletes them).
|
|
261
222
|
*/
|
|
262
223
|
export async function neonSeed(cli) {
|
|
263
224
|
projectId(cli.env);
|
|
@@ -305,8 +266,7 @@ export async function neonSeed(cli) {
|
|
|
305
266
|
await deleteOrWarn(cli, next);
|
|
306
267
|
throw error;
|
|
307
268
|
}
|
|
308
|
-
// Clean: now it replaces the old seed. From here `seed-next` holds only the fixtures, so a
|
|
309
|
-
// failure leaves it in place and says how to finish.
|
|
269
|
+
// Clean: now it replaces the old seed. From here `seed-next` holds only the fixtures, so a failure leaves it in place and says how to finish.
|
|
310
270
|
const rename = `\`neon branches rename ${SEED_NEXT} ${SEED}\``;
|
|
311
271
|
if (old) {
|
|
312
272
|
const deleted = await neon(cli, ["branches", "delete", old.id]);
|
|
@@ -342,8 +302,7 @@ export async function runAppSeed(cli) {
|
|
|
342
302
|
}
|
|
343
303
|
}
|
|
344
304
|
/**
|
|
345
|
-
* Deletes a half-built `seed-next`; the failure being reported comes first, so this only warns. But
|
|
346
|
-
* loudly: until it is emptied, a full-copy `seed-next` holds production's data.
|
|
305
|
+
* Deletes a half-built `seed-next`; the failure being reported comes first, so this only warns. But loudly: until it is emptied, a full-copy `seed-next` holds production's data.
|
|
347
306
|
*/
|
|
348
307
|
async function deleteOrWarn(cli, branch) {
|
|
349
308
|
const code = await neon(cli, ["branches", "delete", branch.id]).then((result) => result.code, () => -1);
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Brings the stack-owned blocks in AGENTS.md, CLAUDE.md, .gitignore and .env.example up to date and
|
|
3
|
-
* returns the files that were (or, with `check`, would be) changed. Every file is checked before
|
|
4
|
-
* any is written, so a malformed one leaves all of them untouched.
|
|
2
|
+
* Brings the stack-owned blocks in AGENTS.md, CLAUDE.md, .gitignore and .env.example up to date and returns the files that were (or, with `check`, would be) changed. Every file is checked before any is written, so a malformed one leaves all of them untouched.
|
|
5
3
|
*/
|
|
6
4
|
export declare function syncBlocks(cwd: string, opts?: {
|
|
7
5
|
check?: boolean;
|
package/dist/cli/sync-blocks.js
CHANGED
|
@@ -26,9 +26,7 @@ function linesOf(source) {
|
|
|
26
26
|
return lines;
|
|
27
27
|
}
|
|
28
28
|
/**
|
|
29
|
-
* Replaces the text between the markers, or appends a marked block when there are none. Anything
|
|
30
|
-
* ambiguous (a marker without its partner, reversed or repeated markers) throws: guessing which
|
|
31
|
-
* text the stack owns could delete the app's own.
|
|
29
|
+
* Replaces the text between the markers, or appends a marked block when there are none. Anything ambiguous (a marker without its partner, reversed or repeated markers) throws: guessing which text the stack owns could delete the app's own.
|
|
32
30
|
*/
|
|
33
31
|
function apply(file, source, body, style) {
|
|
34
32
|
const eol = source.includes("\r\n") ? "\r\n" : "\n";
|
|
@@ -60,9 +58,7 @@ function readBody(name) {
|
|
|
60
58
|
return readFileSync(join(BLOCKS_DIR, name), "utf8").replace(/\s+$/, "").split(/\r?\n/);
|
|
61
59
|
}
|
|
62
60
|
/**
|
|
63
|
-
* Brings the stack-owned blocks in AGENTS.md, CLAUDE.md, .gitignore and .env.example up to date and
|
|
64
|
-
* returns the files that were (or, with `check`, would be) changed. Every file is checked before
|
|
65
|
-
* any is written, so a malformed one leaves all of them untouched.
|
|
61
|
+
* Brings the stack-owned blocks in AGENTS.md, CLAUDE.md, .gitignore and .env.example up to date and returns the files that were (or, with `check`, would be) changed. Every file is checked before any is written, so a malformed one leaves all of them untouched.
|
|
66
62
|
*/
|
|
67
63
|
export async function syncBlocks(cwd, opts = {}) {
|
|
68
64
|
const writes = [];
|
package/dist/cli/vercel-build.js
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
// `stack vercel-build` — Vercel's build command. Neon's documented pattern: the build applies the
|
|
2
|
-
// migrations, so the deployment never goes live on a schema it was not built for.
|
|
1
|
+
// `stack vercel-build` — Vercel's build command. Neon's documented pattern: the build applies the migrations, so the deployment never goes live on a schema it was not built for.
|
|
3
2
|
//
|
|
4
|
-
// - Preview: migrates the one shared `preview` branch (see `stack neon reset-preview`), which lacks
|
|
5
|
-
//
|
|
6
|
-
// - Production: migrates production's database, then builds. A failed migration fails the build,
|
|
7
|
-
// so the new code never goes live on a schema it was not built for. The old deployment keeps
|
|
8
|
-
// serving while this runs, which is why every migration must be expand-then-contract.
|
|
3
|
+
// - Preview: migrates the one shared `preview` branch (see `stack neon reset-preview`), which lacks this PR's new migrations. Refuses if any of its database URLs turns out to be production.
|
|
4
|
+
// - Production: migrates production's database, then builds. A failed migration fails the build, so the new code never goes live on a schema it was not built for. The old deployment keeps serving while this runs, which is why every migration must be expand-then-contract.
|
|
9
5
|
// - Anything else (a local run): only builds.
|
|
10
6
|
import { endpointOf } from "../db/index.js";
|
|
11
7
|
import { CliError } from "./context.js";
|
|
@@ -21,8 +17,7 @@ const hostOf = (url) => {
|
|
|
21
17
|
}
|
|
22
18
|
};
|
|
23
19
|
/**
|
|
24
|
-
* A deployment that migrates needs a database URL. drizzle.config.ts migrates through the direct
|
|
25
|
-
* (unpooled) endpoint when it is set, bypassing PgBouncer; without it, through the pooler.
|
|
20
|
+
* A deployment that migrates needs a database URL. drizzle.config.ts migrates through the direct (unpooled) endpoint when it is set, bypassing PgBouncer; without it, through the pooler.
|
|
26
21
|
*/
|
|
27
22
|
function requireDatabaseUrl(cli, environment) {
|
|
28
23
|
if (!cli.env.DATABASE_URL_UNPOOLED && !cli.env.DATABASE_URL) {
|
|
@@ -41,13 +36,7 @@ export async function vercelBuild(cli) {
|
|
|
41
36
|
if (environment === "preview" || environment === "production") {
|
|
42
37
|
requireDatabaseUrl(cli, environment === "preview" ? "Preview" : "Production");
|
|
43
38
|
if (environment === "preview") {
|
|
44
|
-
// A backstop for a mis-scoped variable: a preview must never migrate or serve production.
|
|
45
|
-
// Every database URL is checked, not just the one migrate uses: the running preview reads
|
|
46
|
-
// DATABASE_URL, and Vercel previews run with NODE_ENV=production, so assertDevDatabase does
|
|
47
|
-
// not catch it at runtime. Pooled and direct hosts of one endpoint compare equal
|
|
48
|
-
// (endpointOf), whichever form each variable holds. Not optional: without production's host
|
|
49
|
-
// there is nothing to check against, and the host is not a secret, so it is set for every
|
|
50
|
-
// environment.
|
|
39
|
+
// A backstop for a mis-scoped variable: a preview must never migrate or serve production. Every database URL is checked, not just the one migrate uses: the running preview reads DATABASE_URL, and Vercel previews run with NODE_ENV=production, so assertDevDatabase does not catch it at runtime. Pooled and direct hosts of one endpoint compare equal (endpointOf), whichever form each variable holds. Not optional: without production's host there is nothing to check against, and the host is not a secret, so it is set for every environment.
|
|
51
40
|
const productionHost = cli.env.PRODUCTION_DATABASE_HOST ?? "";
|
|
52
41
|
if (productionHost.trim() === "") {
|
|
53
42
|
throw new CliError("[build] PRODUCTION_DATABASE_HOST is not set on Preview, so this build cannot tell the " +
|
package/dist/configs/vitest.js
CHANGED
|
@@ -11,9 +11,7 @@ export function stackVitestConfig(overrides = {}, options = {}) {
|
|
|
11
11
|
...appRest,
|
|
12
12
|
resolve: {
|
|
13
13
|
...appResolve,
|
|
14
|
-
// The `@/` alias exists for app and test code, matching `paths` in tsconfig.next.json.
|
|
15
|
-
// Worker, lib, db, server and script modules use relative `.ts` specifiers instead.
|
|
16
|
-
// `root` is the consuming project (default: the working directory Vitest runs from).
|
|
14
|
+
// The `@/` alias exists for app and test code, matching `paths` in tsconfig.next.json. Worker, lib, db, server and script modules use relative `.ts` specifiers instead. `root` is the consuming project (default: the working directory Vitest runs from).
|
|
17
15
|
alias: Array.isArray(appAlias)
|
|
18
16
|
? [{ find: "@", replacement: alias["@"] }, ...appAlias]
|
|
19
17
|
: { ...alias, ...appAlias },
|
|
@@ -22,8 +20,7 @@ export function stackVitestConfig(overrides = {}, options = {}) {
|
|
|
22
20
|
environment: "node",
|
|
23
21
|
globals: false,
|
|
24
22
|
pool: "forks",
|
|
25
|
-
// Integration tests boot an in-memory PGlite and run every migration; a cold start can take
|
|
26
|
-
// several seconds on a busy CI runner.
|
|
23
|
+
// Integration tests boot an in-memory PGlite and run every migration; a cold start can take several seconds on a busy CI runner.
|
|
27
24
|
testTimeout: 60000,
|
|
28
25
|
hookTimeout: 60000,
|
|
29
26
|
...appTestRest,
|
|
@@ -38,8 +35,7 @@ export function stackVitestConfig(overrides = {}, options = {}) {
|
|
|
38
35
|
exclude: ["node_modules/**", ".next/**", "dist/**", "tests/e2e/**", ...toArray(exclude)],
|
|
39
36
|
env: {
|
|
40
37
|
TZ: "UTC",
|
|
41
|
-
// A fixed, test-only value so the suite is self-sufficient on a clean checkout (CI has no
|
|
42
|
-
// `.env`). It is not a secret and must never be used outside tests.
|
|
38
|
+
// A fixed, test-only value so the suite is self-sufficient on a clean checkout (CI has no `.env`). It is not a secret and must never be used outside tests.
|
|
43
39
|
BETTER_AUTH_SECRET: "test-only-better-auth-secret-not-a-real-secret",
|
|
44
40
|
...env,
|
|
45
41
|
},
|
|
@@ -5,14 +5,8 @@ import type { ConnectionTable } from "./table.ts";
|
|
|
5
5
|
* Connected accounts: a signed-in person connects an account at another service so the app can work on their behalf. This is deliberately not sign-in — a connection never signs anyone in and never creates a user; people sign in by email.
|
|
6
6
|
*
|
|
7
7
|
* The flow, as two route handlers the app mounts:
|
|
8
|
-
* - `start(userId, provider, next)` (GET `/api/connections/<provider>/start`): sets a one-off
|
|
9
|
-
*
|
|
10
|
-
* person, the provider, that nonce, an expiry (10 minutes) and where to go afterwards.
|
|
11
|
-
* - `callback(request, userId)` (GET `/api/connections/<provider>/callback`): accepts the code only
|
|
12
|
-
* when the state's signature holds, it has not expired, this browser holds its nonce (so nobody
|
|
13
|
-
* can make someone else finish a flow they started) and the signed-in person is the one who
|
|
14
|
-
* started it. It then stores the token encrypted and redirects to `next` with
|
|
15
|
-
* `?connection=<provider>&result=<code>` — a short code, never free text from the URL.
|
|
8
|
+
* - `start(userId, provider, next)` (GET `/api/connections/<provider>/start`): sets a one-off cookie holding a random nonce and redirects to the provider with a signed `state` naming the person, the provider, that nonce, an expiry (10 minutes) and where to go afterwards.
|
|
9
|
+
* - `callback(request, userId)` (GET `/api/connections/<provider>/callback`): accepts the code only when the state's signature holds, it has not expired, this browser holds its nonce (so nobody can make someone else finish a flow they started) and the signed-in person is the one who started it. It then stores the token encrypted and redirects to `next` with `?connection=<provider>&result=<code>` — a short code, never free text from the URL.
|
|
16
10
|
*/
|
|
17
11
|
export declare const CONNECT_COOKIE = "stack_connect";
|
|
18
12
|
/** What the callback reports in `?result=`. */
|
|
@@ -6,14 +6,8 @@ import { decryptToken, encryptToken, parseEncryptionKey } from "./crypto.js";
|
|
|
6
6
|
* Connected accounts: a signed-in person connects an account at another service so the app can work on their behalf. This is deliberately not sign-in — a connection never signs anyone in and never creates a user; people sign in by email.
|
|
7
7
|
*
|
|
8
8
|
* The flow, as two route handlers the app mounts:
|
|
9
|
-
* - `start(userId, provider, next)` (GET `/api/connections/<provider>/start`): sets a one-off
|
|
10
|
-
*
|
|
11
|
-
* person, the provider, that nonce, an expiry (10 minutes) and where to go afterwards.
|
|
12
|
-
* - `callback(request, userId)` (GET `/api/connections/<provider>/callback`): accepts the code only
|
|
13
|
-
* when the state's signature holds, it has not expired, this browser holds its nonce (so nobody
|
|
14
|
-
* can make someone else finish a flow they started) and the signed-in person is the one who
|
|
15
|
-
* started it. It then stores the token encrypted and redirects to `next` with
|
|
16
|
-
* `?connection=<provider>&result=<code>` — a short code, never free text from the URL.
|
|
9
|
+
* - `start(userId, provider, next)` (GET `/api/connections/<provider>/start`): sets a one-off cookie holding a random nonce and redirects to the provider with a signed `state` naming the person, the provider, that nonce, an expiry (10 minutes) and where to go afterwards.
|
|
10
|
+
* - `callback(request, userId)` (GET `/api/connections/<provider>/callback`): accepts the code only when the state's signature holds, it has not expired, this browser holds its nonce (so nobody can make someone else finish a flow they started) and the signed-in person is the one who started it. It then stores the token encrypted and redirects to `next` with `?connection=<provider>&result=<code>` — a short code, never free text from the URL.
|
|
17
11
|
*/
|
|
18
12
|
export const CONNECT_COOKIE = "stack_connect";
|
|
19
13
|
const STATE_TTL_SECONDS = 10 * 60;
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* A service people connect: how to send them to approve access, and how to turn the code it hands
|
|
3
|
-
* back into a token and the account it belongs to. Errors never carry the token, the code or a URL
|
|
4
|
-
* that holds either.
|
|
2
|
+
* A service people connect: how to send them to approve access, and how to turn the code it hands back into a token and the account it belongs to. Errors never carry the token, the code or a URL that holds either.
|
|
5
3
|
*/
|
|
6
4
|
export type ConnectionProvider = {
|
|
7
5
|
id: string;
|
|
@@ -3,8 +3,7 @@ import type { AuthTables } from "../auth/better-auth/tables.ts";
|
|
|
3
3
|
* The `connection` table: an account at another service that a signed-in person connected so the app can work on their behalf. The app re-exports it from its schema next to the auth tables, so drizzle-kit generates it into the app's own migrations.
|
|
4
4
|
*
|
|
5
5
|
* - One connection per person per provider (reconnecting replaces the token).
|
|
6
|
-
* - One person per provider account (`subject`): an account already connected to someone else is
|
|
7
|
-
* refused, so two people can never share one account's data.
|
|
6
|
+
* - One person per provider account (`subject`): an account already connected to someone else is refused, so two people can never share one account's data.
|
|
8
7
|
* - Removing the person removes their connections (the foreign key cascades in the same statement).
|
|
9
8
|
* - `token` is only ever ciphertext (`crypto.ts`).
|
|
10
9
|
*/
|
|
@@ -3,8 +3,7 @@ import { pgTable, text, timestamp, unique, uuid } from "drizzle-orm/pg-core";
|
|
|
3
3
|
* The `connection` table: an account at another service that a signed-in person connected so the app can work on their behalf. The app re-exports it from its schema next to the auth tables, so drizzle-kit generates it into the app's own migrations.
|
|
4
4
|
*
|
|
5
5
|
* - One connection per person per provider (reconnecting replaces the token).
|
|
6
|
-
* - One person per provider account (`subject`): an account already connected to someone else is
|
|
7
|
-
* refused, so two people can never share one account's data.
|
|
6
|
+
* - One person per provider account (`subject`): an account already connected to someone else is refused, so two people can never share one account's data.
|
|
8
7
|
* - Removing the person removes their connections (the foreign key cascades in the same statement).
|
|
9
8
|
* - `token` is only ever ciphertext (`crypto.ts`).
|
|
10
9
|
*/
|
|
@@ -2,12 +2,7 @@ import { Pool, type PoolClient } from "pg";
|
|
|
2
2
|
/**
|
|
3
3
|
* A `pg` Pool for PGlite (a local PGlite dev database, and the PGlite test database) — never built in production.
|
|
4
4
|
*
|
|
5
|
-
* PGlite over pglite-socket serves exactly one connection: a second client is refused, not queued,
|
|
6
|
-
* and after any SQL error the server drops that connection and briefly resets the next one while it
|
|
7
|
-
* tears the old one down. So a refused or reset checkout is retried for a few seconds — the other
|
|
8
|
-
* holder (a script, the dev server, the socket being torn down) lets go within milliseconds — and
|
|
9
|
-
* then fails with a hint that names the likely cause. `pool.query` checks out through
|
|
10
|
-
* `connect(callback)`, so both call shapes go through one path.
|
|
5
|
+
* PGlite over pglite-socket serves exactly one connection: a second client is refused, not queued, and after any SQL error the server drops that connection and briefly resets the next one while it tears the old one down. So a refused or reset checkout is retried for a few seconds — the other holder (a script, the dev server, the socket being torn down) lets go within milliseconds — and then fails with a hint that names the likely cause. `pool.query` checks out through `connect(callback)`, so both call shapes go through one path.
|
|
11
6
|
*/
|
|
12
7
|
export declare const PGLITE_HINT: string;
|
|
13
8
|
export declare class ExplainingPool extends Pool {
|
|
@@ -2,28 +2,21 @@ import { Pool } from "pg";
|
|
|
2
2
|
/**
|
|
3
3
|
* A `pg` Pool for PGlite (a local PGlite dev database, and the PGlite test database) — never built in production.
|
|
4
4
|
*
|
|
5
|
-
* PGlite over pglite-socket serves exactly one connection: a second client is refused, not queued,
|
|
6
|
-
* and after any SQL error the server drops that connection and briefly resets the next one while it
|
|
7
|
-
* tears the old one down. So a refused or reset checkout is retried for a few seconds — the other
|
|
8
|
-
* holder (a script, the dev server, the socket being torn down) lets go within milliseconds — and
|
|
9
|
-
* then fails with a hint that names the likely cause. `pool.query` checks out through
|
|
10
|
-
* `connect(callback)`, so both call shapes go through one path.
|
|
5
|
+
* PGlite over pglite-socket serves exactly one connection: a second client is refused, not queued, and after any SQL error the server drops that connection and briefly resets the next one while it tears the old one down. So a refused or reset checkout is retried for a few seconds — the other holder (a script, the dev server, the socket being torn down) lets go within milliseconds — and then fails with a hint that names the likely cause. `pool.query` checks out through `connect(callback)`, so both call shapes go through one path.
|
|
11
6
|
*/
|
|
12
7
|
export const PGLITE_HINT = "Could not get the PGlite connection (it serves exactly one). Is the local PGlite database running? If it is, " +
|
|
13
8
|
"another process (the dev server, a script) holds the connection — stop it or wait; or a query " +
|
|
14
9
|
"inside a transaction used `db` instead of `tx`.";
|
|
15
10
|
const RETRY_FOR_MS = 5_000;
|
|
16
11
|
const sleep = (ms) => new Promise((done) => setTimeout(done, ms));
|
|
17
|
-
// The connection never became usable: refused, reset, or (on Linux) terminated by the server while
|
|
18
|
-
// it tears down the connection an earlier SQL error dropped. A SQL error is not one of these.
|
|
12
|
+
// The connection never became usable: refused, reset, or (on Linux) terminated by the server while it tears down the connection an earlier SQL error dropped. A SQL error is not one of these.
|
|
19
13
|
function refused(error) {
|
|
20
14
|
const code = error?.code;
|
|
21
15
|
if (code === "ECONNREFUSED" || code === "ECONNRESET")
|
|
22
16
|
return true;
|
|
23
17
|
return error instanceof Error && /Connection terminated unexpectedly/.test(error.message);
|
|
24
18
|
}
|
|
25
|
-
// A checked-out client is probed before it is handed out, so a connection lost to that teardown
|
|
26
|
-
// fails here — where it is safe to retry — rather than on the caller's statement.
|
|
19
|
+
// A checked-out client is probed before it is handed out, so a connection lost to that teardown fails here — where it is safe to retry — rather than on the caller's statement.
|
|
27
20
|
async function usable(client) {
|
|
28
21
|
try {
|
|
29
22
|
await client.query("select 1");
|
package/dist/db/index.d.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { type NodePgDatabase } from "drizzle-orm/node-postgres";
|
|
2
2
|
import { Pool } from "pg";
|
|
3
3
|
/**
|
|
4
|
-
* The one place a connection pool is created. Pass Neon's **pooled** URL (`-pooler`, PgBouncer in
|
|
5
|
-
* transaction mode); migrations use the direct endpoint and do not come through here. Reads no
|
|
6
|
-
* environment: the caller passes `url` and decides `pglite` (a loopback server outside production).
|
|
4
|
+
* The one place a connection pool is created. Pass Neon's **pooled** URL (`-pooler`, PgBouncer in transaction mode); migrations use the direct endpoint and do not come through here. Reads no environment: the caller passes `url` and decides `pglite` (a loopback server outside production).
|
|
7
5
|
*/
|
|
8
6
|
export declare function createDb<S extends Record<string, unknown>>(opts: {
|
|
9
7
|
url: string;
|
package/dist/db/index.js
CHANGED
|
@@ -4,30 +4,22 @@ import { Pool } from "pg";
|
|
|
4
4
|
import { ExplainingPool } from "./explaining-pool.js";
|
|
5
5
|
import { explicitSslMode } from "./ssl.js";
|
|
6
6
|
/**
|
|
7
|
-
* The one place a connection pool is created. Pass Neon's **pooled** URL (`-pooler`, PgBouncer in
|
|
8
|
-
* transaction mode); migrations use the direct endpoint and do not come through here. Reads no
|
|
9
|
-
* environment: the caller passes `url` and decides `pglite` (a loopback server outside production).
|
|
7
|
+
* The one place a connection pool is created. Pass Neon's **pooled** URL (`-pooler`, PgBouncer in transaction mode); migrations use the direct endpoint and do not come through here. Reads no environment: the caller passes `url` and decides `pglite` (a loopback server outside production).
|
|
10
8
|
*/
|
|
11
9
|
export function createDb(opts) {
|
|
12
10
|
const pglite = opts.pglite === true;
|
|
13
11
|
const pool = new (pglite ? ExplainingPool : Pool)({
|
|
14
12
|
// Neon: `sslmode=require` spelled out as the `verify-full` pg already applies (`ssl.ts`).
|
|
15
13
|
connectionString: pglite ? opts.url : explicitSslMode(opts.url),
|
|
16
|
-
// Neon: a Vercel instance serves a handful of concurrent requests, and many instances share
|
|
17
|
-
// Neon's pooler, so each holds few connections. PGlite allows exactly one.
|
|
14
|
+
// Neon: a Vercel instance serves a handful of concurrent requests, and many instances share Neon's pooler, so each holds few connections. PGlite allows exactly one.
|
|
18
15
|
max: pglite ? 1 : (opts.max ?? 5),
|
|
19
16
|
connectionTimeoutMillis: 5_000,
|
|
20
|
-
// (PGlite: 100 ms, so its one connection is free for the next process almost at once.)
|
|
21
|
-
// Serverless instances are suspended between requests; a socket idle across a suspension comes
|
|
22
|
-
// back dead, so idle sockets are retired quickly rather than handed to the next query.
|
|
17
|
+
// (PGlite: 100 ms, so its one connection is free for the next process almost at once.) Serverless instances are suspended between requests; a socket idle across a suspension comes back dead, so idle sockets are retired quickly rather than handed to the next query.
|
|
23
18
|
idleTimeoutMillis: pglite ? 100 : 10_000,
|
|
24
19
|
});
|
|
25
|
-
// Vercel Fluid compute: lets the platform close idle clients before it suspends the instance, so
|
|
26
|
-
// connections do not leak against Neon's pooler. A no-op outside Vercel (dev, tests, scripts).
|
|
20
|
+
// Vercel Fluid compute: lets the platform close idle clients before it suspends the instance, so connections do not leak against Neon's pooler. A no-op outside Vercel (dev, tests, scripts).
|
|
27
21
|
attachDatabasePool(pool);
|
|
28
|
-
// An unhandled `error` event on a pg Pool takes the whole process down — the web server and,
|
|
29
|
-
// worse, a cron route, which has to survive a database blip. Log it and let the pool retire the
|
|
30
|
-
// socket: the next checkout opens a fresh connection.
|
|
22
|
+
// An unhandled `error` event on a pg Pool takes the whole process down — the web server and, worse, a cron route, which has to survive a database blip. Log it and let the pool retire the socket: the next checkout opens a fresh connection.
|
|
31
23
|
pool.on("error", (error) => {
|
|
32
24
|
console.error("[db] idle client error — the pool will retire this connection", error);
|
|
33
25
|
});
|
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
|
|
2
2
|
/**
|
|
3
|
-
* Whether the database has every migration in `migrationsFolder` (drizzle-kit's output). For a
|
|
4
|
-
* liveness probe: "unknown" is the honest answer whenever it cannot tell — the journal is
|
|
5
|
-
* unreadable (a deploy that did not bundle `drizzle/`), the journal is empty, or the query fails for
|
|
6
|
-
* a reason other than a missing journal table (never migrated: "pending") — rather than a confident "current".
|
|
3
|
+
* Whether the database has every migration in `migrationsFolder` (drizzle-kit's output). For a liveness probe: "unknown" is the honest answer whenever it cannot tell — the journal is unreadable (a deploy that did not bundle `drizzle/`), the journal is empty, or the query fails for a reason other than a missing journal table (never migrated: "pending") — rather than a confident "current".
|
|
7
4
|
*/
|
|
8
5
|
export declare function migrationStatus(db: NodePgDatabase<any>, migrationsFolder: string): Promise<"current" | "pending" | "unknown">;
|
|
@@ -2,10 +2,7 @@ import { readFile } from "node:fs/promises";
|
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { sql } from "drizzle-orm";
|
|
4
4
|
/**
|
|
5
|
-
* Whether the database has every migration in `migrationsFolder` (drizzle-kit's output). For a
|
|
6
|
-
* liveness probe: "unknown" is the honest answer whenever it cannot tell — the journal is
|
|
7
|
-
* unreadable (a deploy that did not bundle `drizzle/`), the journal is empty, or the query fails for
|
|
8
|
-
* a reason other than a missing journal table (never migrated: "pending") — rather than a confident "current".
|
|
5
|
+
* Whether the database has every migration in `migrationsFolder` (drizzle-kit's output). For a liveness probe: "unknown" is the honest answer whenever it cannot tell — the journal is unreadable (a deploy that did not bundle `drizzle/`), the journal is empty, or the query fails for a reason other than a missing journal table (never migrated: "pending") — rather than a confident "current".
|
|
9
6
|
*/
|
|
10
7
|
export async function migrationStatus(
|
|
11
8
|
// biome-ignore lint/suspicious/noExplicitAny: any schema
|
package/dist/db/ssl.d.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Neon's connection strings say `sslmode=require`. pg 8 already treats `prefer`, `require` and
|
|
3
|
-
* `verify-ca` as `verify-full`, and says so on every new connection with a "SECURITY WARNING" that
|
|
4
|
-
* v9 will weaken them; on Vercel that lands in the runtime logs at error level on every cold start.
|
|
5
|
-
* Spelling out `verify-full` keeps exactly today's checking and silences the warning. Any other
|
|
6
|
-
* mode, or no mode, is left as it is; so is a string that is not a URL.
|
|
2
|
+
* Neon's connection strings say `sslmode=require`. pg 8 already treats `prefer`, `require` and `verify-ca` as `verify-full`, and says so on every new connection with a "SECURITY WARNING" that v9 will weaken them; on Vercel that lands in the runtime logs at error level on every cold start. Spelling out `verify-full` keeps exactly today's checking and silences the warning. Any other mode, or no mode, is left as it is; so is a string that is not a URL.
|
|
7
3
|
*/
|
|
8
4
|
export declare function explicitSslMode(connectionString: string): string;
|
package/dist/db/ssl.js
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Neon's connection strings say `sslmode=require`. pg 8 already treats `prefer`, `require` and
|
|
3
|
-
* `verify-ca` as `verify-full`, and says so on every new connection with a "SECURITY WARNING" that
|
|
4
|
-
* v9 will weaken them; on Vercel that lands in the runtime logs at error level on every cold start.
|
|
5
|
-
* Spelling out `verify-full` keeps exactly today's checking and silences the warning. Any other
|
|
6
|
-
* mode, or no mode, is left as it is; so is a string that is not a URL.
|
|
2
|
+
* Neon's connection strings say `sslmode=require`. pg 8 already treats `prefer`, `require` and `verify-ca` as `verify-full`, and says so on every new connection with a "SECURITY WARNING" that v9 will weaken them; on Vercel that lands in the runtime logs at error level on every cold start. Spelling out `verify-full` keeps exactly today's checking and silences the warning. Any other mode, or no mode, is left as it is; so is a string that is not a URL.
|
|
7
3
|
*/
|
|
8
4
|
export function explicitSslMode(connectionString) {
|
|
9
5
|
let url;
|
package/dist/db/target.d.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
/** True only when `connectionString` parses and its host is this machine. Unparseable is not local. */
|
|
2
2
|
export declare function isLoopbackUrl(connectionString: string | undefined): boolean;
|
|
3
3
|
/**
|
|
4
|
-
* A Neon endpoint's identity, the same for its pooled and direct hosts:
|
|
5
|
-
* `ep-x-123-pooler.us-east-2.aws.neon.tech` and `ep-x-123.us-east-2.aws.neon.tech` are one database.
|
|
6
|
-
* Accepts a bare host or a whole connection string.
|
|
4
|
+
* A Neon endpoint's identity, the same for its pooled and direct hosts: `ep-x-123-pooler.us-east-2.aws.neon.tech` and `ep-x-123.us-east-2.aws.neon.tech` are one database. Accepts a bare host or a whole connection string.
|
|
7
5
|
*/
|
|
8
6
|
export declare function endpointOf(hostOrUrl: string): string;
|
|
9
7
|
export type DatabaseTargetOptions = {
|
|
@@ -16,11 +14,6 @@ export type DatabaseTargetOptions = {
|
|
|
16
14
|
productionHost: string | undefined;
|
|
17
15
|
};
|
|
18
16
|
/**
|
|
19
|
-
* Local tooling (dev server, scripts, drizzle-kit, psql) works against a Neon dev branch, and must
|
|
20
|
-
* not reach production by accident — a dev branch's URL and production's differ only in the
|
|
21
|
-
* endpoint. So a remote database is allowed only when `PRODUCTION_DATABASE_HOST` names production
|
|
22
|
-
* and this URL is a different endpoint. Production itself is exempt, a loopback database (the tests'
|
|
23
|
-
* PGlite) always passes, and a command that opted in with `ENV_FILE=…` or `ALLOW_REMOTE_DATABASE=1`
|
|
24
|
-
* is deliberate. Throws; the message names the host only, never the credentials.
|
|
17
|
+
* Local tooling (dev server, scripts, drizzle-kit, psql) works against a Neon dev branch, and must not reach production by accident — a dev branch's URL and production's differ only in the endpoint. So a remote database is allowed only when `PRODUCTION_DATABASE_HOST` names production and this URL is a different endpoint. Production itself is exempt, a loopback database (the tests' PGlite) always passes, and a command that opted in with `ENV_FILE=…` or `ALLOW_REMOTE_DATABASE=1` is deliberate. Throws; the message names the host only, never the credentials.
|
|
25
18
|
*/
|
|
26
19
|
export declare function assertDevDatabase(connectionString: string | undefined, { nodeEnv, allowRemote, envFile, productionHost }: DatabaseTargetOptions): void;
|
package/dist/db/target.js
CHANGED
|
@@ -21,9 +21,7 @@ function hostOf(connectionString) {
|
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
/**
|
|
24
|
-
* A Neon endpoint's identity, the same for its pooled and direct hosts:
|
|
25
|
-
* `ep-x-123-pooler.us-east-2.aws.neon.tech` and `ep-x-123.us-east-2.aws.neon.tech` are one database.
|
|
26
|
-
* Accepts a bare host or a whole connection string.
|
|
24
|
+
* A Neon endpoint's identity, the same for its pooled and direct hosts: `ep-x-123-pooler.us-east-2.aws.neon.tech` and `ep-x-123.us-east-2.aws.neon.tech` are one database. Accepts a bare host or a whole connection string.
|
|
27
25
|
*/
|
|
28
26
|
export function endpointOf(hostOrUrl) {
|
|
29
27
|
let host = hostOrUrl.trim().toLowerCase();
|
|
@@ -39,12 +37,7 @@ export function endpointOf(hostOrUrl) {
|
|
|
39
37
|
return [first.replace(/-pooler$/, ""), ...rest].join(".");
|
|
40
38
|
}
|
|
41
39
|
/**
|
|
42
|
-
* Local tooling (dev server, scripts, drizzle-kit, psql) works against a Neon dev branch, and must
|
|
43
|
-
* not reach production by accident — a dev branch's URL and production's differ only in the
|
|
44
|
-
* endpoint. So a remote database is allowed only when `PRODUCTION_DATABASE_HOST` names production
|
|
45
|
-
* and this URL is a different endpoint. Production itself is exempt, a loopback database (the tests'
|
|
46
|
-
* PGlite) always passes, and a command that opted in with `ENV_FILE=…` or `ALLOW_REMOTE_DATABASE=1`
|
|
47
|
-
* is deliberate. Throws; the message names the host only, never the credentials.
|
|
40
|
+
* Local tooling (dev server, scripts, drizzle-kit, psql) works against a Neon dev branch, and must not reach production by accident — a dev branch's URL and production's differ only in the endpoint. So a remote database is allowed only when `PRODUCTION_DATABASE_HOST` names production and this URL is a different endpoint. Production itself is exempt, a loopback database (the tests' PGlite) always passes, and a command that opted in with `ENV_FILE=…` or `ALLOW_REMOTE_DATABASE=1` is deliberate. Throws; the message names the host only, never the credentials.
|
|
48
41
|
*/
|
|
49
42
|
export function assertDevDatabase(connectionString, { nodeEnv, allowRemote, envFile, productionHost }) {
|
|
50
43
|
if (nodeEnv === "production")
|
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
|
|
2
2
|
/**
|
|
3
|
-
* A throwaway Postgres per test file, migrated from the app's committed migrations with the
|
|
4
|
-
* programmatic migrator.
|
|
3
|
+
* A throwaway Postgres per test file, migrated from the app's committed migrations with the programmatic migrator.
|
|
5
4
|
*
|
|
6
5
|
* Two engines, one helper:
|
|
7
6
|
* - default: an in-memory PGlite served over the wire protocol (no Docker, no image pull);
|
|
8
|
-
* - `TEST_DATABASE_URL` set (a CI job with real Postgres): a fresh `t_<hex>` database on that
|
|
9
|
-
* server, dropped on `stop()`.
|
|
7
|
+
* - `TEST_DATABASE_URL` set (a CI job with real Postgres): a fresh `t_<hex>` database on that server, dropped on `stop()`.
|
|
10
8
|
*
|
|
11
|
-
* **Never the development database.** Either way the test gets its own database, so a run cannot
|
|
12
|
-
* see, change or truncate what `pnpm dev` is pointed at — and `url` is exposed so a test can
|
|
13
|
-
* assert exactly that. PGlite allows one connection: a test must not open a second pool on `url`.
|
|
9
|
+
* **Never the development database.** Either way the test gets its own database, so a run cannot see, change or truncate what `pnpm dev` is pointed at — and `url` is exposed so a test can assert exactly that. PGlite allows one connection: a test must not open a second pool on `url`.
|
|
14
10
|
*/
|
|
15
11
|
export type TestDatabase<S extends Record<string, unknown>> = {
|
|
16
12
|
db: NodePgDatabase<S>;
|
package/dist/db/testing/index.js
CHANGED
|
@@ -37,16 +37,12 @@ async function startPglite() {
|
|
|
37
37
|
export async function startTestDatabase(opts) {
|
|
38
38
|
const adminUrl = opts.adminUrl ?? testDatabaseUrl();
|
|
39
39
|
const backend = adminUrl ? await startRealPostgres(adminUrl) : await startPglite();
|
|
40
|
-
// The same client path apps use. On real Postgres the pool is Neon-sized (`max` 5); PGlite gets
|
|
41
|
-
// the retrying single-connection pool.
|
|
40
|
+
// The same client path apps use. On real Postgres the pool is Neon-sized (`max` 5); PGlite gets the retrying single-connection pool.
|
|
42
41
|
const { db, pool } = createDb({ url: backend.url, schema: opts.schema, pglite: backend.pglite });
|
|
43
|
-
// An idle client's socket can die between tests — PGlite drops it after a SQL error, and on real
|
|
44
|
-
// Postgres `drop database … with (force)` terminates it (57P01) while `stop()` is ending the pool.
|
|
45
|
-
// The pool retires the client either way; the next query reports anything real.
|
|
42
|
+
// An idle client's socket can die between tests — PGlite drops it after a SQL error, and on real Postgres `drop database … with (force)` terminates it (57P01) while `stop()` is ending the pool. The pool retires the client either way; the next query reports anything real.
|
|
46
43
|
pool.on("error", () => { });
|
|
47
44
|
try {
|
|
48
|
-
// The committed SQL migrations are the schema's history; the harness applies the same ones
|
|
49
|
-
// production does rather than a parallel definition that could drift.
|
|
45
|
+
// The committed SQL migrations are the schema's history; the harness applies the same ones production does rather than a parallel definition that could drift.
|
|
50
46
|
await migrate(db, { migrationsFolder: opts.migrationsFolder });
|
|
51
47
|
}
|
|
52
48
|
catch (error) {
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// A side-effect module for a Vitest `setupFiles` entry: it must run before anything that loads
|
|
2
|
-
// `.env`, so it imports only the one function it runs. See `isolateTestEnv`.
|
|
1
|
+
// A side-effect module for a Vitest `setupFiles` entry: it must run before anything that loads `.env`, so it imports only the one function it runs. See `isolateTestEnv`.
|
|
3
2
|
import { isolateTestEnv } from "../../env/testing.js";
|
|
4
3
|
isolateTestEnv();
|
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* PGlite served over the Postgres wire protocol, so the app's `pg` pool talks to it like any other
|
|
3
|
-
* Postgres. Used by the test helper and a template's dev server.
|
|
2
|
+
* PGlite served over the Postgres wire protocol, so the app's `pg` pool talks to it like any other Postgres. Used by the test helper and a template's dev server.
|
|
4
3
|
*
|
|
5
|
-
* `1` because pglite-socket serialises every connection onto PGlite's single session: with more,
|
|
6
|
-
* one connection's open transaction blocks the others and there is no per-connection isolation.
|
|
7
|
-
* At the limit a second connection is refused outright, so a pool pointed at this server must use
|
|
8
|
-
* `max: PGLITE_MAX_CONNECTIONS` and nothing may open another pool against it.
|
|
4
|
+
* `1` because pglite-socket serialises every connection onto PGlite's single session: with more, one connection's open transaction blocks the others and there is no per-connection isolation. At the limit a second connection is refused outright, so a pool pointed at this server must use `max: PGLITE_MAX_CONNECTIONS` and nothing may open another pool against it.
|
|
9
5
|
*/
|
|
10
6
|
export declare const PGLITE_MAX_CONNECTIONS = 1;
|
|
11
7
|
export type PgliteServer = {
|