@wwchen/stack 0.6.3 → 0.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 +9 -8
- package/blocks/env.example +1 -2
- package/dist/auth/better-auth/index.d.ts +2 -316
- package/dist/auth/better-auth/index.js +2 -237
- package/dist/auth/index.d.ts +2 -58
- package/dist/auth/index.js +2 -120
- package/dist/auth/proxy.d.ts +2 -14
- package/dist/auth/proxy.js +2 -50
- package/dist/cli/context.d.ts +6 -3
- package/dist/cli/context.js +3 -0
- package/dist/cli/db.d.ts +3 -0
- package/dist/cli/db.js +26 -0
- package/dist/cli/index.js +14 -3
- package/dist/cli/migrate-suite.d.ts +2 -0
- package/dist/cli/migrate-suite.js +787 -0
- 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 +3 -9
- package/dist/connections/providers.d.ts +1 -3
- package/dist/connections/table.d.ts +2 -3
- 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/schema-changes.d.ts +13 -0
- package/dist/db/schema-changes.js +99 -0
- 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/guards.d.ts +3 -10
- package/dist/http/guards.js +4 -12
- package/dist/jobs/index.d.ts +2 -9
- package/dist/jobs/index.js +2 -9
- package/dist/mail/index.d.ts +4 -45
- package/dist/mail/index.js +4 -97
- package/dist/next/api.d.ts +7 -19
- 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 +15 -9
- package/dist/auth/better-auth/tables.d.ts +0 -628
- package/dist/auth/better-auth/tables.js +0 -76
- package/dist/auth/better-auth/users.d.ts +0 -27
- package/dist/auth/better-auth/users.js +0 -128
- package/dist/auth/safe-next.d.ts +0 -17
- package/dist/auth/safe-next.js +0 -36
- package/dist/auth/types.d.ts +0 -47
- package/dist/auth/types.js +0 -5
- package/dist/http/client-ip.d.ts +0 -12
- package/dist/http/client-ip.js +0 -28
- package/dist/http/rate-limit.d.ts +0 -34
- package/dist/http/rate-limit.js +0 -79
package/dist/cli/neon.d.ts
CHANGED
|
@@ -11,10 +11,7 @@ export type Branch = {
|
|
|
11
11
|
export declare const SEED_NEXT = "seed-next";
|
|
12
12
|
export declare function projectId(env: Env): string;
|
|
13
13
|
/**
|
|
14
|
-
* Refuses a dev branch name that is not a dev branch: production (whatever this project calls its
|
|
15
|
-
* default branch, and the name `production` itself), the shared `seed` and `preview`, and `seed-next`
|
|
16
|
-
* (the seed being built, which may briefly hold production's data). One check
|
|
17
|
-
* for every command that uses your dev branch: with-neon, dev-setup and user:create.
|
|
14
|
+
* 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.
|
|
18
15
|
*/
|
|
19
16
|
export declare function assertDevBranchName(name: string, production: Branch): void;
|
|
20
17
|
/** `dev/<os user>` unless NEON_DEV_BRANCH names another. */
|
|
@@ -23,25 +20,19 @@ export declare function findBranch(cli: Cli, name: string): Promise<Branch | und
|
|
|
23
20
|
/** Production: the project's default branch (Neon's `default` flag), not a branch name. */
|
|
24
21
|
export declare function defaultBranch(branches: Branch[]): Branch;
|
|
25
22
|
/**
|
|
26
|
-
* What `.env` used to carry for local work: `branch`'s URLs, and the host of production (the
|
|
27
|
-
* default branch; `production` when the caller already has it, else listed).
|
|
23
|
+
* 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).
|
|
28
24
|
*/
|
|
29
25
|
export declare function branchEnv(cli: Cli, branch: string, production?: Branch): Promise<Record<string, string>>;
|
|
30
26
|
/** The migrations, run against whatever database `env` names, through the direct endpoint. */
|
|
31
27
|
export declare function migrate(cli: Cli, env: Env): Promise<number>;
|
|
32
28
|
/**
|
|
33
|
-
* `stack with-neon <command> [args]` — runs a local command against your Neon dev branch with no
|
|
34
|
-
* `.env`: the connection strings and production's host come from the Neon CLI on every run.
|
|
29
|
+
* `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.
|
|
35
30
|
*
|
|
36
|
-
* Steps aside, and runs the command untouched, when something else already says where the database
|
|
37
|
-
* is: DATABASE_URL in the environment (Vercel, an e2e run, a local Postgres), ENV_FILE
|
|
38
|
-
* (a deliberate production command). A `.env` never decides: what this sets wins over it, in Next
|
|
39
|
-
* and in `loadEnvFile` alike.
|
|
31
|
+
* 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.
|
|
40
32
|
*/
|
|
41
33
|
export declare function withNeon(cli: Cli, [command, ...args]: string[]): Promise<number>;
|
|
42
34
|
/**
|
|
43
|
-
* Your dev branch's `branchEnv`, as `with-neon` hands it to a command; nothing (`{}`) when
|
|
44
|
-
* DATABASE_URL or ENV_FILE already says where the database is. Refuses the default branch.
|
|
35
|
+
* 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.
|
|
45
36
|
*/
|
|
46
37
|
export declare function devBranchEnv(cli: Cli): Promise<Record<string, string>>;
|
|
47
38
|
/**
|
|
@@ -49,47 +40,23 @@ export declare function devBranchEnv(cli: Cli): Promise<Record<string, string>>;
|
|
|
49
40
|
*/
|
|
50
41
|
export declare function previewBranchEnv(cli: Cli): Promise<Record<string, string>>;
|
|
51
42
|
/**
|
|
52
|
-
* `stack dev-setup` — once per developer: makes your Neon dev branch from `seed` (never from
|
|
53
|
-
* production) if it does not exist yet (`dev/<os user>`, or NEON_DEV_BRANCH) and migrates it. Safe
|
|
54
|
-
* to re-run. Sign-in needs nothing on the branch: Better Auth runs in the app, with a development
|
|
55
|
-
* BETTER_AUTH_SECRET and magic links written to the sink file. There is no `.env` to write:
|
|
56
|
-
* `stack with-neon` reads the branch from the Neon CLI on every command.
|
|
43
|
+
* `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.
|
|
57
44
|
*/
|
|
58
45
|
export declare function devSetup(cli: Cli): Promise<number>;
|
|
59
46
|
/**
|
|
60
|
-
* Schemas Neon's own services install, which hold functions and settings rather than anyone's rows:
|
|
61
|
-
* `auth` is the pg_session_jwt extension the Data API installs (Neon: "do not install this extension
|
|
62
|
-
* manually"), `pgrst` is the Data API's PostgREST configuration. Left in place so the branch keeps
|
|
63
|
-
* working like any other. Neon Auth's `neon_auth` is *not* here: it holds users.
|
|
47
|
+
* 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.
|
|
64
48
|
*/
|
|
65
49
|
export declare const NEON_SCHEMAS: readonly string[];
|
|
66
50
|
/**
|
|
67
|
-
* Empties a database down to an empty `public`: drops every schema but Postgres's own
|
|
68
|
-
* (`pg_catalog`, `pg_toast*`, `information_schema`, any `pg_*`) and Neon's service schemas, with
|
|
69
|
-
* everything in them — tables, rows, drizzle's journal, Neon Auth's users. Then the migrations
|
|
70
|
-
* run from nothing, as on a new database. A schema-only branch needs this as much as a full copy:
|
|
71
|
-
* it keeps drizzle's journal *table* but not its rows, so migrate would re-run 0000 into tables
|
|
72
|
-
* that already exist. Large objects belong to the database rather than to a schema, so no drop
|
|
73
|
-
* removes them: every one is unlinked too.
|
|
51
|
+
* 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.
|
|
74
52
|
*
|
|
75
|
-
* Neon's schemas are kept by name, so first it checks they hold what Neon puts there: functions and
|
|
76
|
-
* settings, no rows. A table, partitioned table, materialized view, sequence or foreign table in
|
|
77
|
-
* one (an app's own `auth.accounts`, say) would carry production's rows into the seed, so it
|
|
78
|
-
* refuses before dropping anything.
|
|
53
|
+
* 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.
|
|
79
54
|
*/
|
|
80
55
|
export declare function emptyDatabase(sql: (statement: string) => Promise<Record<string, unknown>[]>): Promise<string[]>;
|
|
81
56
|
/**
|
|
82
|
-
* `stack neon seed` — (re)creates the `seed` branch: exactly the app's migrations plus its seed
|
|
83
|
-
* rows, and none of production's data. Cuts it from production (the project's default branch) as
|
|
84
|
-
* Neon's `--schema-only` branch (a normal branch where that is unsupported), empties it of every
|
|
85
|
-
* non-system schema either way (`emptyDatabase`), migrates it from nothing and runs the app's
|
|
86
|
-
* `db/seed.ts`: `export default async function seed(db)`.
|
|
57
|
+
* `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)`.
|
|
87
58
|
*
|
|
88
|
-
* The new seed is built as `seed-next` and takes the name `seed` only once it is clean, so a
|
|
89
|
-
* failure part-way never leaves a branch holding production's data under the name every dev and
|
|
90
|
-
* preview branch is cut from; `seed-next` is deleted on any failure. Neon will not delete a branch
|
|
91
|
-
* that has children, so a seed with dev/preview branches on it is refused up front: remove them
|
|
92
|
-
* yourself (this never deletes them).
|
|
59
|
+
* 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).
|
|
93
60
|
*/
|
|
94
61
|
export declare function neonSeed(cli: Cli): Promise<number>;
|
|
95
62
|
/**
|
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=`. */
|
|
@@ -1,19 +1,13 @@
|
|
|
1
1
|
import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
|
2
|
+
import { safeNext } from "@wwchen/stack-auth";
|
|
2
3
|
import { and, asc, eq } from "drizzle-orm";
|
|
3
|
-
import { safeNext } from "../auth/safe-next.js";
|
|
4
4
|
import { decryptToken, encryptToken, parseEncryptionKey } from "./crypto.js";
|
|
5
5
|
/**
|
|
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;
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import type { AuthTables } from "
|
|
1
|
+
import type { AuthTables } from "@wwchen/stack-auth/better-auth";
|
|
2
2
|
/**
|
|
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
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The SQL `drizzle-kit generate` would write for `schema` (the app's `src/db/schema.ts` exports) against the latest migration in `migrationsFolder`: empty when the migrations are current. Only tables in `public` count, as in the template's drizzle config. Assumes that config sets no `casing` (the template's does not): with one, the names here would differ from what `generate` writes.
|
|
3
|
+
*/
|
|
4
|
+
export declare function pendingStatements(schema: Record<string, unknown>, migrationsFolder: string): Promise<string[]>;
|
|
5
|
+
/**
|
|
6
|
+
* The statements in a drizzle-kit migration that an old deployment cannot survive. The old deployment keeps serving while the build migrates, so a migration may only add what the old code ignores. Flagged:
|
|
7
|
+
* - each `DROP` (a table, column, index, constraint, default…), except `DROP NOT NULL`, which only loosens a column;
|
|
8
|
+
* - each `RENAME`;
|
|
9
|
+
* - `ADD COLUMN … NOT NULL` without a default: the old code's inserts leave it empty;
|
|
10
|
+
* - `SET NOT NULL` and `SET DATA TYPE` on an existing column: the old code's writes may no longer fit;
|
|
11
|
+
* - `ADD CONSTRAINT` (any kind) and `CREATE UNIQUE INDEX` on a table the same migration did not create: they can reject the old code's inserts. drizzle-kit writes a new table's foreign keys as separate `ADD CONSTRAINT` statements, and those pass.
|
|
12
|
+
*/
|
|
13
|
+
export declare function contractStatements(sql: string): string[];
|