Skip to content

db:generate works without an external Postgres via auto PGlite (#2)#32

Open
quantamShade0337 wants to merge 2 commits into
seveibar:mainfrom
quantamShade0337:feat/db-generate-without-postgres
Open

db:generate works without an external Postgres via auto PGlite (#2)#32
quantamShade0337 wants to merge 2 commits into
seveibar:mainfrom
quantamShade0337:feat/db-generate-without-postgres

Conversation

@quantamShade0337
Copy link
Copy Markdown

Closes #2bun run db:generate no longer requires a running Postgres.

When no external DB connection is configured (no DATABASE_URL/PGHOST/PGPORT/PGDATABASE/PGUSER/PGPASSWORD), generate now automatically:

  1. starts an in-process PGlite instance,
  2. runs migrations against it,
  3. serves it over a local ephemeral Postgres wire port via pg-gateway,
  4. points Zapatos type generation + pg-schema-dump at it,
  5. tears it down (restores DATABASE_URL, closes the server + PGlite).

The existing external-Postgres path is preserved when DB env vars are present. Also added the missing db:generate script (bun ./src/cli.ts generate) and awaited the async generate/migrate calls in the CLI.

Verified: bun install && bun test3 pass / 0 fail, including the updated tests/generate.pglite.test.ts which clears Postgres env vars and runs generate() end-to-end against in-process PGlite.

/claim #2

…eveibar#2)

When no external DB env is configured, generate now starts an in-process
PGlite, runs migrations, serves it over a local pg-gateway wire port, and
points Zapatos + pg-schema-dump at it (then tears down). External Postgres
path preserved when DB env vars are present. Adds the missing db:generate
script and awaits the async CLI calls.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 4, 2026 03:54
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Updates generate to automatically fall back to an in-memory PGlite database when no Postgres connection environment is configured, and aligns CLI/docs/scripts with this behavior.

Changes:

  • Add detection of external Postgres env config and auto-run PGlite migrations/generation when absent.
  • Improve resource cleanup in PGlite generation and ensure CLI subcommands await async work.
  • Update tests, README, and package scripts to reflect the new default behavior.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
tests/generate.pglite.test.ts Adjusts the generation test to verify implicit PGlite usage when Postgres env vars are absent, with env restore logic.
src/generate.ts Adds env-based fallback to PGlite and refactors PGlite generation into a dedicated function with cleanup.
src/cli.ts Ensures migrate and generate commands await async operations.
package.json Adds a db:generate script for running the CLI generate command.
bun.lock Updates lockfile metadata (configVersion).
README.md Documents the new automatic PGlite fallback behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/generate.ts Outdated
Comment on lines 69 to 71
await new Promise<void>((resolve) => server.listen(0, resolve))
const port = (server.address() as any).port
const connectionString = `postgres://postgres:postgres@127.0.0.1:${port}/postgres`
Comment thread src/generate.ts Outdated
})

const db = new PGlite()
const prevDbUrl = process.env.DATABASE_URL
Comment thread src/generate.ts Outdated
const connectionString = `postgres://postgres:postgres@127.0.0.1:${port}/postgres`

const prevDbUrl = process.env.DATABASE_URL
process.env.DATABASE_URL = connectionString
Comment thread src/generate.ts Outdated
Comment on lines 91 to 92
if (prevDbUrl === undefined) delete process.env.DATABASE_URL
else process.env.DATABASE_URL = prevDbUrl
Comment thread src/generate.ts
Comment on lines +8 to +15
const postgresEnvKeys = [
"DATABASE_URL",
"PGHOST",
"PGPORT",
"PGDATABASE",
"PGUSER",
"PGPASSWORD",
]
Comment thread src/generate.ts
Comment on lines +46 to +54
async onMessage(data: Uint8Array, { isAuthenticated }: any) {
if (!isAuthenticated) return
try {
const { data: responseData } = await (db as any).execProtocol(data)
return responseData
} catch {
return undefined
}
},
Comment thread tests/generate.pglite.test.ts Outdated
Comment on lines 26 to 27
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "pgstrap-generate-"))
const migrationsDir = path.join(tmp, "migrations")
Comment on lines 34 to +40
fs.mkdirSync(migrationsDir, { recursive: true })
fs.writeFileSync(
path.join(migrationsDir, "001_create_table.js"),
migrationFile,
)

await generate({
schemas: ["public"],
defaultDatabase: "postgres",
dbDir: path.join(tmp, "db"),
migrationsDir,
pglite: true,
})

const zapatosFile = path.join(tmp, "db", "zapatos", "schema.d.ts")
const structureDir = path.join(
tmp,
"db",
"structure",
"public",
"tables",
"foo",
)
try {
Comment thread src/generate.ts
Comment on lines +118 to 126
if (pglite || !hasExternalDatabaseConfig()) {
await generateWithPglite({
schemas,
defaultDatabase,
dbDir,
migrationsDir,
})
return
}
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