Skip to content

Use PGlite by default for scaffolded db:generate - #83

Open
jacobsenscott235-web wants to merge 2 commits into
seveibar:mainfrom
jacobsenscott235-web:pglite-default-generate
Open

jacobsenscott235-web wants to merge 2 commits into
seveibar:mainfrom
jacobsenscott235-web:pglite-default-generate

Conversation

@jacobsenscott235-web

@jacobsenscott235-web jacobsenscott235-web commented Oct 2, 2026 •

Copy link
Copy Markdown

Closes #2.

New projects scaffolded with pgstrap init now get "db:generate": "pgstrap generate --pglite", so bun run db:generate generates types without a Postgres server running. pgstrap generate without the flag still generates from a live database.

While testing on Windows I hit three problems on the same path, fixed here as well:

  • Config loading crashed under Node on Windows. getProjectContext did import(path.join(cwd, "pgstrap.config.js")). Node's ESM loader rejects absolute Windows paths (ERR_UNSUPPORTED_ESM_URL_SCHEME), so every CLI command failed there. It now imports through pathToFileURL(...), which resolves to the same file on Linux and macOS.
  • The temporary PGlite gateway listened on every interface, with the fixed password postgres, and triggered a Windows Firewall prompt. It now binds to 127.0.0.1, which the connection string already used.
  • Cleanup on failure. In a finally block, the gateway is closed (and the close is awaited) and DATABASE_URL is restored. The CLI also awaits generate, so errors go through yargs.

The two PGlite tests also get a 30 s timeout. On this machine they take 6–7 s and failed bun's 5 s default even on unmodified main.

Verification (Windows 11, Node 24.13.1, bun 1.4.2)

  • bun test: 4 pass, 0 fail
  • bun run build: CJS and DTS builds succeed
  • biome format: changed files are clean with LF line endings
  • tsc --noEmit: 0 errors in src/; the 3 existing bun:test type errors in tests/ are unchanged
  • End to end, with nothing listening on port 5432 and DATABASE_URL unset: in a fresh pgstrap init project with this build linked in, bun run db:generate applies a migration and writes src/db/zapatos/schema.d.ts and src/db/structure/public/tables/<table>/table.sql. With a migration containing invalid SQL it exits 1 and prints the SQL error. Before the config fix, running the CLI under Node exited with ERR_UNSUPPORTED_ESM_URL_SCHEME.
  • Failure cleanup: with dbDir pointing at a file, generation fails after the gateway starts. generate() now waits for the gateway to close and restores DATABASE_URL; on main the gateway stays listening and DATABASE_URL still points at it. The new test generate with pglite restores DATABASE_URL on failure covers this, and it fails against main's generate.ts.

@algora-pbc /claim #2

🤖 Generated with Claude Code

New projects get "pgstrap generate --pglite", so generating types no
longer needs a running Postgres server (seveibar#2).

- init: scaffold db:generate with --pglite
- generate: bind the temporary PGlite gateway to 127.0.0.1, and close it
  and restore DATABASE_URL even when generation fails
- cli: await generate so failures surface through yargs
- config: import pgstrap.config.js through a file:// URL; Node's ESM
  loader rejects absolute Windows paths, so every command failed there
- tests: give the two PGlite tests a 30 s timeout; they take 6-7 s on
  Windows and failed the 5 s default
- README: document the default

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 2, 2026 16:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Gateway shutdown is not awaited, and the new failure-cleanup behavior lacks regression coverage.

Review effort: Balanced
Findings: 1 Medium severity · 1 Low severity

Open (2)
What changed in this PR

Updates scaffolded projects to generate database types through PGlite by default, while improving Windows compatibility and failure cleanup.

Changes:

  • Makes db:generate use --pglite by default.
  • Fixes Windows config imports and binds the gateway to localhost.
  • Adds error propagation, cleanup, documentation, and longer test timeouts.
File Description
README.md Documents PGlite generation behavior.
src/​cli.ts Awaits type generation.
src/​generate.ts Restricts gateway binding and adds cleanup.
src/​get-project-context.ts Imports config through a file URL.
src/​init.ts Scaffolds the PGlite generation command.
tests/​generate.pglite.test.ts Extends the generation test timeout.
tests/​init.test.ts Updates the expected scaffolded command.
tests/​pglite.test.ts Extends the migration test timeout.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/generate.ts Outdated
Comment thread src/generate.ts
- generate: wait for the temporary gateway to finish closing before
  returning
- tests: check that DATABASE_URL is restored when generation fails
  after the gateway has started

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The implementation matches the stated behavior and includes focused regression coverage for the cleanup path.

Review effort: Balanced
Findings: None

Resolved since last review (2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use pglite so that postgres isn't required when generating types

2 participants