Skip to content

Commit 383fe36

Browse files
committed
init: default db:generate to --pglite
Fixes #2 Scaffolded db:generate previously ran 'pgstrap generate', which requires a running Postgres instance. Running 'bun run db:generate' on a fresh project would fail with a connection error. Default to '--pglite' so type generation works out of the box against an in-memory PGlite instance. Users who need to generate against a real Postgres (e.g. via DATABASE_URL) can drop the flag from the scaffolded script. - src/init.ts: scaffold db:generate with --pglite - tests/init.test.ts: assert the new default and add a regression test - README.md: document the new default /claim #2
1 parent 3039eee commit 383fe36

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ npm install pgstrap --save-dev
5555

5656
- `npm run db:migrate` - Run pending migrations
5757
- `npm run db:reset` - Drop and recreate the database, then run all migrations
58-
- `npm run db:generate` - Generate types and structure dumps. Use `pgstrap generate --pglite` to run migrations against an in-memory PGlite instance.
58+
- `npm run db:generate` - Generate types and structure dumps. Defaults to an in-memory PGlite instance, so no Postgres needs to be running. To generate against a real Postgres (e.g. via `DATABASE_URL`), edit the script and drop `--pglite`.
5959
- `npm run db:create-migration` - Create a new migration file
6060

6161
### Configuration

src/init.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ export const initPgstrap = async (ctx: Pick<Context, "cwd">) => {
1616

1717
pkg.scripts["db:migrate"] = "pgstrap migrate"
1818
pkg.scripts["db:reset"] = "pgstrap reset"
19-
pkg.scripts["db:generate"] = "pgstrap generate"
19+
// Default `db:generate` to PGlite so `bun run db:generate` doesn't require
20+
// a Postgres instance running in the background. To generate against a real
21+
// Postgres connection (e.g. DATABASE_URL), drop `--pglite` from this script.
22+
pkg.scripts["db:generate"] = "pgstrap generate --pglite"
2023
pkg.scripts["db:create-migration"] = "pgstrap create-migration"
2124

2225
if (!pkg.devDependencies) pkg.devDependencies = {}

tests/init.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ test("initPgstrap writes scripts to package.json", async () => {
2525
)
2626
expect(pkg.scripts["db:migrate"]).toBe("pgstrap migrate")
2727
expect(pkg.scripts["db:reset"]).toBe("pgstrap reset")
28-
expect(pkg.scripts["db:generate"]).toBe("pgstrap generate")
28+
// db:generate defaults to --pglite so users don't need Postgres running
29+
// in the background to regenerate types/structure. See issue #2.
30+
expect(pkg.scripts["db:generate"]).toBe("pgstrap generate --pglite")
2931
expect(pkg.scripts["db:create-migration"]).toBe("pgstrap create-migration")
3032
})
33+
34+
test("initPgstrap defaults db:generate to PGlite (no Postgres needed)", async () => {
35+
await initPgstrap({ cwd: testDir })
36+
const pkg = JSON.parse(
37+
fs.readFileSync(path.join(testDir, "package.json"), "utf8"),
38+
)
39+
expect(pkg.scripts["db:generate"]).toContain("--pglite")
40+
})

0 commit comments

Comments
 (0)