Local Database with PGLite
To simplify local development and avoid the need for running a full PostgreSQL server (via Docker, Postgres.app, or Homebrew), this fork utilizes PGlite (@electric-sql/pglite) for local database storage.
How it Works
PGlite is an embedded PostgreSQL engine that runs directly in Node.js/Bun. It writes database files to the local file system.
In this project, the local database is stored in the .tmp/pglite directory at the root of the workspace.
Configuration
Environment Variables: The
.envfile configures theDATABASE_URLto point to the local.tmp/pglitedirectory instead of a standardpostgres://connection string.envDATABASE_URL=.tmp/pgliteDrizzle Config:
db/drizzle.config.tsdetects if theDATABASE_URLis a PGLite path (not starting withpostgres://) and automatically switches the Drizzle dialect driver topglite. It also resolves the.tmp/pglitepath relative to the project root.API Development Server:
apps/api/dev.tshas been updated so that when running the local dev server (bun dev), it connects to the PGLite database using the@electric-sql/pgliteclient and passes that instance to the tRPC context. This bypasses the default behavior which attempts to connect to Cloudflare Hyperdrive.Seeding:
db/scripts/seed.tsautomatically parses the non-postgres connection string and uses the PGLite client to run seed scripts correctly.
Usage
You can interact with the local PGLite database using the standard db package scripts from the root of the project:
- Push schema changes:bash
bun db:push - Seed the database:bash
bun db:seed - Run the local dev server (which will automatically use the PGLite database):bash
bun dev
Troubleshooting
Resetting the Database: If you need to completely wipe your local database, you can simply delete the
.tmp/pglitedirectory and re-run the push and seed commands.bashrm -rf .tmp/pglite bun db:push bun db:seedCloudflare Deployments: PGLite is only used for local development. When deployed, the workers will still use the
DATABASE_URLto connect to Neon PostgreSQL via Cloudflare Hyperdrive, as the deployed environments use the values injected in.dev.varsor the Wrangler bindings.