@schemavaults/dbh 0.11.0 → 0.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@ Ensure that you have both `postgres` and a `postgres-ws-proxy` containers runnin
|
|
|
20
20
|
|
|
21
21
|
You'll likely want to replace the `build:` sections for the services in the e2e test example `.yml` file with `image:`. For example, use `image: postgres:17.7` for the `postgres` service. For the proxy, you can pull the docker image from `ghcr.io/schemavaults/dbh/postgres-ws-proxy`; use the version number equal to your `@schemavaults/dbh` npm package installation:
|
|
22
22
|
```md
|
|
23
|
-
# NPM Package: @schemavaults/dbh@0.11.
|
|
23
|
+
# NPM Package: @schemavaults/dbh@0.11.1 => ghcr.io/schemavaults/dbh/postgres-ws-proxy:0.11.1
|
|
24
24
|
```
|
|
25
25
|
|
|
26
26
|
### In your application server code
|
package/package.json
CHANGED
package/.claude/settings.json
DELETED
|
@@ -1,244 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: database-migrations
|
|
3
|
-
description: Authoring, building, validating, and running PostgreSQL database migrations with the @schemavaults/dbh package. Use when a project depends on @schemavaults/dbh and you are creating or editing Kysely migration files, setting up a migrations/ directory, or when the user mentions migrations, up()/down(), schema changes, or the dbh CLI's migrate / build-db-migrations / validate-migration-directory commands.
|
|
4
|
-
---
|
|
5
|
-
|
|
6
|
-
# Database Migrations with @schemavaults/dbh
|
|
7
|
-
|
|
8
|
-
`@schemavaults/dbh` provides [Kysely](https://kysely.dev/) migrations for
|
|
9
|
-
PostgreSQL, applied through the `dbh` CLI. Migrations are opinionated: every file
|
|
10
|
-
is a numbered module that exports an `up()` and a `down()` function. TypeScript
|
|
11
|
-
source migrations are **built** to JavaScript first, then **applied** with the
|
|
12
|
-
CLI.
|
|
13
|
-
|
|
14
|
-
Invoke the CLI with your package runner. Use **`bunx @schemavaults/dbh`** for
|
|
15
|
-
**validating and building** migrations — `build-db-migrations` uses Bun's
|
|
16
|
-
bundler and requires Bun anyway. Use **`npx @schemavaults/dbh`** for **running /
|
|
17
|
-
applying** migrations (`migrate` and `reverse`): most PostgreSQL drivers are
|
|
18
|
-
built for Node.js rather than Bun, so apply migrations on the Node runtime.
|
|
19
|
-
|
|
20
|
-
## One-time setup (for consumers)
|
|
21
|
-
|
|
22
|
-
Migrations import the `sql` template tag from `@/sql` rather than directly from
|
|
23
|
-
the package. This indirection is required by the build step (see the note under
|
|
24
|
-
"Building migrations"), so configure it once:
|
|
25
|
-
|
|
26
|
-
1. **Create a local `sql` module** somewhere in your source tree, e.g.
|
|
27
|
-
`./src/db/sql.ts`, that re-exports the tag from the package:
|
|
28
|
-
|
|
29
|
-
```ts
|
|
30
|
-
// src/db/sql.ts
|
|
31
|
-
export { sql, sql as default } from "@schemavaults/dbh/sql";
|
|
32
|
-
export type * from "@schemavaults/dbh/sql";
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
2. **Configure the `@/sql` path alias** in your `tsconfig.json` so migration
|
|
36
|
-
sources typecheck and resolve:
|
|
37
|
-
|
|
38
|
-
```jsonc
|
|
39
|
-
{
|
|
40
|
-
"compilerOptions": {
|
|
41
|
-
"baseUrl": ".",
|
|
42
|
-
"paths": {
|
|
43
|
-
"@/sql": ["./src/db/sql.ts"]
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
3. **Create a migrations directory**, e.g. `./src/db/migrations/`, and add your
|
|
50
|
-
numbered migration files there.
|
|
51
|
-
|
|
52
|
-
## Migration file format
|
|
53
|
-
|
|
54
|
-
Each migration is a single file in your migrations directory. The rules are:
|
|
55
|
-
|
|
56
|
-
1. **The directory is non-empty.**
|
|
57
|
-
2. **Each file name is prefixed with a 5-digit migration number**, followed by a
|
|
58
|
-
short kebab-case description, e.g. `00000-template-migration.ts`,
|
|
59
|
-
`00001-create-users-table.ts`. The number defines apply order.
|
|
60
|
-
3. **Each module exports an `up(db)` and a `down(db)` function.** `up()` applies
|
|
61
|
-
the change; `down()` must reverse it exactly so migrations can be rolled back.
|
|
62
|
-
4. **Migration numbers are unique** — never reuse a number. If two branches both
|
|
63
|
-
add `00040-*.ts`, that collision must be resolved by renumbering one of them
|
|
64
|
-
before merge.
|
|
65
|
-
|
|
66
|
-
Both `up` and `down` receive a `Kysely<any>` instance and return a `Promise`.
|
|
67
|
-
Import the `Kysely` type from the package: `import type { Kysely } from "@schemavaults/dbh"`.
|
|
68
|
-
|
|
69
|
-
### Example: using the `Kysely<any>` query builder
|
|
70
|
-
|
|
71
|
-
Prefer the typed query builder for schema operations:
|
|
72
|
-
|
|
73
|
-
```ts
|
|
74
|
-
// 00001-create-users-table.ts
|
|
75
|
-
import type { Kysely } from "@schemavaults/dbh";
|
|
76
|
-
|
|
77
|
-
export async function up(db: Kysely<any>): Promise<void> {
|
|
78
|
-
await db.schema
|
|
79
|
-
.createTable("users")
|
|
80
|
-
.addColumn("user_id", "uuid", (col) => col.primaryKey())
|
|
81
|
-
.addColumn("email", "text", (col) => col.notNull().unique())
|
|
82
|
-
.addColumn("created_at", "bigint", (col) => col.notNull())
|
|
83
|
-
.execute();
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export async function down(db: Kysely<any>): Promise<void> {
|
|
87
|
-
await db.schema.dropTable("users").execute();
|
|
88
|
-
}
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
### Example: using the `sql` template tag
|
|
92
|
-
|
|
93
|
-
For statements the builder can't express (or raw DDL), import `sql` from
|
|
94
|
-
`@/sql` (your local module from setup, which re-exports Kysely's `sql` tag) and
|
|
95
|
-
call `.execute(db)`:
|
|
96
|
-
|
|
97
|
-
```ts
|
|
98
|
-
// 00002-create-squirrels-table.ts
|
|
99
|
-
import type { Kysely } from "@schemavaults/dbh";
|
|
100
|
-
import { sql } from "@/sql";
|
|
101
|
-
|
|
102
|
-
export async function up(db: Kysely<any>): Promise<void> {
|
|
103
|
-
await sql`
|
|
104
|
-
CREATE TABLE IF NOT EXISTS EXAMPLE_SQUIRRELS (
|
|
105
|
-
squirrel_id UUID PRIMARY KEY,
|
|
106
|
-
squirrel_name TEXT NOT NULL,
|
|
107
|
-
created_at BIGINT NOT NULL
|
|
108
|
-
);
|
|
109
|
-
`.execute(db);
|
|
110
|
-
|
|
111
|
-
// Always interpolate values via ${...}; the sql tag parameterizes them.
|
|
112
|
-
await sql`CREATE INDEX squirrels_name_idx ON EXAMPLE_SQUIRRELS (squirrel_name);`.execute(
|
|
113
|
-
db,
|
|
114
|
-
);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
export async function down(db: Kysely<any>): Promise<void> {
|
|
118
|
-
await sql`DROP TABLE IF EXISTS EXAMPLE_SQUIRRELS;`.execute(db);
|
|
119
|
-
}
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
> Important: migration files must **always** import `sql` from `@/sql`, never
|
|
123
|
-
> directly from `@schemavaults/dbh/sql`. The `build-db-migrations` step rewrites
|
|
124
|
-
> the literal `@/sql` import specifier to a relative path pointing at the built,
|
|
125
|
-
> standalone `sql.js`, so the import must be written exactly as `@/sql` for the
|
|
126
|
-
> build to work. (This is why the one-time setup configures the `@/sql` alias.)
|
|
127
|
-
|
|
128
|
-
### Empty template migration
|
|
129
|
-
|
|
130
|
-
A no-op migration is valid (useful as a starting template):
|
|
131
|
-
|
|
132
|
-
```ts
|
|
133
|
-
// 00000-template-migration.ts
|
|
134
|
-
import type { Kysely } from "@schemavaults/dbh";
|
|
135
|
-
|
|
136
|
-
export async function up(
|
|
137
|
-
db: Kysely<any>, // eslint-disable-line @typescript-eslint/no-unused-vars
|
|
138
|
-
): Promise<void> {}
|
|
139
|
-
|
|
140
|
-
export async function down(
|
|
141
|
-
db: Kysely<any>, // eslint-disable-line @typescript-eslint/no-unused-vars
|
|
142
|
-
): Promise<void> {}
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
## Validating migrations
|
|
146
|
-
|
|
147
|
-
Before building or applying, assert your source migrations directory is
|
|
148
|
-
well-formed. The `validate-migration-directory` command checks all four rules
|
|
149
|
-
above and exits `0` when valid, non-zero otherwise (good for CI / pre-commit):
|
|
150
|
-
|
|
151
|
-
```bash
|
|
152
|
-
bunx @schemavaults/dbh validate-migration-directory ./src/db/migrations
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
It reports each problem with an `[ERROR]`/`[WARN]` prefix:
|
|
156
|
-
- empty directory,
|
|
157
|
-
- a file missing the 5-digit prefix,
|
|
158
|
-
- a module missing `up()` or `down()`,
|
|
159
|
-
- duplicate migration numbers (branch collisions).
|
|
160
|
-
|
|
161
|
-
Treat duplicate numbers as warnings (non-fatal) with `--duplicates-as-warnings`.
|
|
162
|
-
|
|
163
|
-
## Building migrations
|
|
164
|
-
|
|
165
|
-
TypeScript migrations must be compiled to JavaScript before they're applied
|
|
166
|
-
(the `migrate` step runs on Node and imports `.js`). The `build-db-migrations`
|
|
167
|
-
command uses Bun's bundler and also builds the standalone `sql` module the
|
|
168
|
-
migrations depend on. Point `--sql-module` at the local `sql.ts` you created
|
|
169
|
-
during setup:
|
|
170
|
-
|
|
171
|
-
```bash
|
|
172
|
-
bunx @schemavaults/dbh build-db-migrations ./src/db/migrations \
|
|
173
|
-
--outdir ./dist/migrations \
|
|
174
|
-
--sql-module ./src/db/sql.ts \
|
|
175
|
-
--sql-outdir ./dist
|
|
176
|
-
```
|
|
177
|
-
|
|
178
|
-
Key options:
|
|
179
|
-
- `<migrations-src>` — directory of `.ts` migration sources (positional).
|
|
180
|
-
- `--outdir <dir>` — where compiled `.js` migrations are written (required).
|
|
181
|
-
- `--sql-module <path>` — path to your local `sql.ts` module to build alongside (required).
|
|
182
|
-
- `--sql-outdir <dir>` — where the built `sql.js` goes (defaults to the parent of `--outdir`).
|
|
183
|
-
- `--external <pkg...>` — packages to keep external (default: `@schemavaults/dbh`, `kysely`).
|
|
184
|
-
|
|
185
|
-
`build-db-migrations` requires `bun` to be installed and on the PATH.
|
|
186
|
-
|
|
187
|
-
## Running migrations
|
|
188
|
-
|
|
189
|
-
Apply built migrations with `migrate`, and roll back with `reverse`. Both take
|
|
190
|
-
the **built** migration folder and require an `--environment`; credentials come
|
|
191
|
-
from `process.env` (or an `--env-file`). Run these with **`npx`** (Node.js):
|
|
192
|
-
most PostgreSQL drivers target Node rather than Bun.
|
|
193
|
-
|
|
194
|
-
```bash
|
|
195
|
-
# Apply all pending migrations (to latest):
|
|
196
|
-
npx @schemavaults/dbh migrate ./dist/migrations --environment production --env-file ./.env.production
|
|
197
|
-
|
|
198
|
-
# Apply up to a specific version (the migration name w/o extension):
|
|
199
|
-
npx @schemavaults/dbh migrate ./dist/migrations 00001-create-users-table --environment staging
|
|
200
|
-
|
|
201
|
-
# Roll back down to a target version:
|
|
202
|
-
npx @schemavaults/dbh reverse ./dist/migrations 00000-template-migration --environment staging
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
Options for `migrate` / `reverse`:
|
|
206
|
-
- `<folder>` — path to the built migration folder (positional).
|
|
207
|
-
- `[version]` / `<version>` — target migration name; `migrate` defaults to latest, `reverse` requires it.
|
|
208
|
-
- `-e, --environment <env>` — `development | test | staging | production` (required).
|
|
209
|
-
- `--ws-proxy-url <url>` — custom Neon-compatible WebSocket proxy URL.
|
|
210
|
-
- `--env-file <path>` — load DB credentials from a `.env` file first.
|
|
211
|
-
|
|
212
|
-
Each result line prints as `[Up|Down] <migrationName>: <Success|Error|NotExecuted>`.
|
|
213
|
-
|
|
214
|
-
### Programmatic API
|
|
215
|
-
|
|
216
|
-
The same operations are available from `@schemavaults/dbh/migrate` for tests or
|
|
217
|
-
custom scripts, using the adapter's Kysely instance:
|
|
218
|
-
|
|
219
|
-
```ts
|
|
220
|
-
import { migrate, reverse } from "@schemavaults/dbh/migrate";
|
|
221
|
-
|
|
222
|
-
await migrate({ db: adapter.db, migrationFolder, version /* optional */ });
|
|
223
|
-
await reverse({ db: adapter.db, migrationFolder, version });
|
|
224
|
-
```
|
|
225
|
-
|
|
226
|
-
## Typical end-to-end flow
|
|
227
|
-
|
|
228
|
-
```bash
|
|
229
|
-
# 1. Validate the source migrations directory.
|
|
230
|
-
bunx @schemavaults/dbh validate-migration-directory ./src/db/migrations
|
|
231
|
-
|
|
232
|
-
# 2. Build .ts migrations (+ sql module) to .js.
|
|
233
|
-
bunx @schemavaults/dbh build-db-migrations ./src/db/migrations \
|
|
234
|
-
--outdir ./dist/migrations --sql-module ./src/db/sql.ts --sql-outdir ./dist
|
|
235
|
-
|
|
236
|
-
# 3. Apply the built migrations (npx / Node.js — pg drivers target Node).
|
|
237
|
-
npx @schemavaults/dbh migrate ./dist/migrations --environment production --env-file ./.env.production
|
|
238
|
-
```
|
|
239
|
-
|
|
240
|
-
## Required environment variables (for migrate/reverse)
|
|
241
|
-
|
|
242
|
-
`POSTGRES_USER`, `POSTGRES_PASSWORD`, `POSTGRES_URL`, `POSTGRES_HOST`,
|
|
243
|
-
`POSTGRES_PORT`, `POSTGRES_DATABASE` (and optional `POSTGRES_URL_NON_POOLING`).
|
|
244
|
-
Set `SCHEMAVAULTS_DBH_DEBUG=true` for verbose debug logging.
|
package/CLAUDE.md
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
# CLAUDE.md
|
|
2
|
-
|
|
3
|
-
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
|
4
|
-
|
|
5
|
-
## What This Is
|
|
6
|
-
|
|
7
|
-
`@schemavaults/dbh` is an npm package that provides a Kysely-based adapter for connecting to Postgres databases through a Neon-compatible WebSocket proxy. It works with both Neon-hosted serverless Postgres and local Postgres instances (via a bundled WS proxy).
|
|
8
|
-
|
|
9
|
-
## Commands
|
|
10
|
-
|
|
11
|
-
Ensure dependencies are installed with `bun install` before attempting to run any other commands.
|
|
12
|
-
|
|
13
|
-
- **Build:** `bun run build` (runs tsc + tsc-alias, then cleans test files from dist/)
|
|
14
|
-
- **Lint:** `bun run lint` (eslint on src/)
|
|
15
|
-
- **Unit tests:** `bun run test` or `bun run test:unit`
|
|
16
|
-
- **Single test:** `bun test --test-name-pattern '<pattern>'`
|
|
17
|
-
- **E2E tests:** `cd tests && /bin/bash ./run_e2e_tests.sh` (requires Docker Compose — spins up postgres, ws-proxy, and test-runner containers)
|
|
18
|
-
- **CLI:** `bun run cli --help` locally or `bunx @schemavaults/dbh --help` remotely.
|
|
19
|
-
|
|
20
|
-
## Architecture
|
|
21
|
-
|
|
22
|
-
The core adapter is `SchemaVaultsPostgresNeonProxyAdapter<T>` (generic over Kysely table types). It wraps Kysely with a `NeonDialect` and handles credential parsing, WS proxy URL resolution, and debug logging. Consumers extend or instantiate it, passing an environment (`development|test|staging|production`), optional credentials (defaults to env vars), and an optional `wsProxyUrl` (string or generator function).
|
|
23
|
-
|
|
24
|
-
Key modules:
|
|
25
|
-
- `src/schemavaults-postgres-neon-proxy-adapter.ts` — the main adapter class
|
|
26
|
-
- `src/migrate.ts` — Kysely migration helpers (`migrate`, `reverse`), exported as a separate entrypoint (`@schemavaults/dbh/migrate`)
|
|
27
|
-
- `src/sql.ts` — re-exports Kysely's `sql` tag
|
|
28
|
-
- `src/utils/parseDatabaseCredentials.ts` — parses/validates DB credentials from an object or `process.env`
|
|
29
|
-
|
|
30
|
-
The package has two export entrypoints: `.` (adapter + sql + types), `./sql` (Kysely template tag re-export), `./migrate` (migration utilities), and `./cli` (@schemavaults/dbh command-line utility).
|
|
31
|
-
|
|
32
|
-
## Local Dev Environment
|
|
33
|
-
|
|
34
|
-
- Runtime/package manager: **Bun** (v1.3.6)
|
|
35
|
-
- TypeScript with path alias `@/*` → `src/*` (resolved by tsc-alias at build time)
|
|
36
|
-
- E2E tests run inside Docker containers (postgres:17.7 + a Go-based WS proxy on port 5433)
|
|
37
|
-
|
|
38
|
-
## Environment Variables
|
|
39
|
-
|
|
40
|
-
The adapter reads these from `process.env` when credentials aren't passed directly:
|
|
41
|
-
`POSTGRES_USER`, `POSTGRES_PASSWORD`, `POSTGRES_URL`, `POSTGRES_URL_NON_POOLING` (optional), `POSTGRES_HOST`, `POSTGRES_PORT`, `POSTGRES_DATABASE`. Debug mode via `SCHEMAVAULTS_DBH_DEBUG=true`.
|
package/eslint.config.cjs
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
// @schemavaults/dbh - eslint.config.cjs
|
|
2
|
-
|
|
3
|
-
const js = require("@eslint/js");
|
|
4
|
-
const tsParser = require("@typescript-eslint/parser");
|
|
5
|
-
const tsPlugin = require("@typescript-eslint/eslint-plugin");
|
|
6
|
-
const globals = require("globals");
|
|
7
|
-
|
|
8
|
-
module.exports = [
|
|
9
|
-
// Base recommended configs
|
|
10
|
-
js.configs.recommended,
|
|
11
|
-
|
|
12
|
-
// Main config
|
|
13
|
-
{
|
|
14
|
-
files: ["src/**/*.{ts,tsx,js,jsx}"],
|
|
15
|
-
|
|
16
|
-
languageOptions: {
|
|
17
|
-
parser: tsParser,
|
|
18
|
-
parserOptions: {
|
|
19
|
-
ecmaVersion: "latest",
|
|
20
|
-
sourceType: "module",
|
|
21
|
-
ecmaFeatures: {
|
|
22
|
-
jsx: false,
|
|
23
|
-
},
|
|
24
|
-
project: "./tsconfig.json",
|
|
25
|
-
},
|
|
26
|
-
globals: {
|
|
27
|
-
...globals.browser,
|
|
28
|
-
...globals.es2021,
|
|
29
|
-
...globals.node,
|
|
30
|
-
},
|
|
31
|
-
},
|
|
32
|
-
|
|
33
|
-
plugins: {
|
|
34
|
-
"@typescript-eslint": tsPlugin,
|
|
35
|
-
},
|
|
36
|
-
|
|
37
|
-
rules: {
|
|
38
|
-
// TypeScript recommended rules
|
|
39
|
-
...tsPlugin.configs.recommended.rules,
|
|
40
|
-
|
|
41
|
-
"@typescript-eslint/no-unused-vars": [
|
|
42
|
-
"warn",
|
|
43
|
-
{ argsIgnorePattern: "^_" },
|
|
44
|
-
],
|
|
45
|
-
"@typescript-eslint/no-explicit-any": "off",
|
|
46
|
-
"@typescript-eslint/no-namespace": "off",
|
|
47
|
-
"@typescript-eslint/no-empty-object-type": "off",
|
|
48
|
-
"no-redeclare": "off",
|
|
49
|
-
},
|
|
50
|
-
},
|
|
51
|
-
|
|
52
|
-
// Ignore patterns
|
|
53
|
-
{
|
|
54
|
-
ignores: ["dist/**", "node_modules/**", "*.config.js", "*.config.cjs"],
|
|
55
|
-
},
|
|
56
|
-
];
|