Skip to content

feat(generate): default to embedded PGlite when no PostgreSQL connection is configured - #63

Open
Furox-Art wants to merge 2 commits into
seveibar:mainfrom
Furox-Art:feat/pglite-default-offline
Open

feat(generate): default to embedded PGlite when no PostgreSQL connection is configured#63
Furox-Art wants to merge 2 commits into
seveibar:mainfrom
Furox-Art:feat/pglite-default-offline

Conversation

@Furox-Art

@Furox-Art Furox-Art commented Sep 9, 2026

Copy link
Copy Markdown

Closes #2
/claim #2

Disclosure: This PR was authored by @Furox-Art (AI-assisted). Happy to adjust anything.

Summary

pgstrap generate no longer requires a running PostgreSQL server by default.
The PGlite mode that already existed behind --pglite becomes the automatic
default whenever no external PostgreSQL connection is configured.

Behavior

situation database used
--pglite passed embedded PGlite (always)
--no-pglite passed configured PostgreSQL (always)
neither flag, connection env configured configured PostgreSQL (backward compatible)
neither flag, no connection env embedded PGlite (new default)

The auto-detection checks every variable that pg-connection-from-env and
pg-schema-dump honor, including POSTGRES_URI which has higher precedence
than DATABASE_URL — so zapatos types and the structure dump always come
from the same database.

Resource handling fixes

  • The temporary pg-gateway server is shut down in an awaited finally block.
  • The PGlite instance is closed in the same finally (also covers migration
    failures — previously an error there leaked the instance).
  • listen() rejects through server.once("error", reject) instead of
    throwing from an event handler.
  • The CLI now awaits generate(...) so errors surface as a clean exit code.

Tests

  • New: generate.auto-pglite.test.ts — with no flag and no connection env,
    generation completes offline and produces both the zapatos types and the
    structure dump.
  • Suite: bun test4 pass, 0 fail. tsc --noEmit clean in src/.
    biome format clean.

…ion is configured

pgstrap already shipped an opt-in `--pglite` mode, but `pgstrap generate`
still required a running PostgreSQL server unless the flag was passed,
which made the zero-config onboarding flow impossible offline.

Now the default is automatic:

- explicit `--pglite` / `--no-pglite` always wins
- otherwise PGlite is used unless a PostgreSQL connection is configured
  through the environment (DATABASE_URL, DATABASE_URI, PG_URI or
  POSTGRES_HOST)

Also fixes resource handling in the PGlite path: the temporary
pg-gateway server is now closed in a `finally` block (awaited) and
server errors no longer crash the process via an unhandled 'error'
event.

Closes seveibar#2

Signed-off-by: Furox-Art <Furox-Art@users.noreply.github.com>
Review follow-ups: POSTGRES_URI has higher precedence than DATABASE_URL
in pg-schema-dump, so it must count as a configured connection and be
overridden in the PGlite path together with PG_URI and DATABASE_URI.
Also closes the PGlite instance in the finally block (covers migration
failures) and awaits the CLI generate call.

Signed-off-by: Furox-Art <Furox-Art@users.noreply.github.com>
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

1 participant