Skip to content

feat: PostgreSQL with Drizzle (--db drizzle) - #5

Open
SibilSoren wants to merge 6 commits into
mainfrom
feat/drizzle
Open

SibilSoren wants to merge 6 commits into
mainfrom
feat/drizzle

Conversation

@SibilSoren

@SibilSoren SibilSoren commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

Description

The first database option: PostgreSQL with Drizzle ORM, picked with --db drizzle or in the setup questions. It's the first option built on the slots from #4. Projects without a database stay as they were, apart from two small changes listed below.

Rebased onto 1.1.2 (#6), which took this PR's healthcheck and CI flake fixes.

Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • 📝 Documentation update
  • 🔧 Refactor / chore
  • 💥 Breaking change

Changes Made

  • What a project gets. A users module (list, get, create) that keeps all its queries in users.repository.ts, the users table and its first migration, and db:generate, db:migrate and db:seed scripts. Postgres is in docker-compose, and migrations run before the API starts there. DATABASE_URL is checked at startup, and the Dockerfile copies the migrations into the image.
  • Tests without a database. The users tests run on an in-memory Postgres (PGlite) with the same migrations, seeded before each test.
  • Shutdown. On SIGTERM the server closes its database connections after the last request.
  • Postgres port. docker-compose publishes Postgres on 127.0.0.1 only, because its password is a local one.
  • CLI. --db none|drizzle and a setup question. Next steps show how to start Postgres, migrate and seed, also after a failed install.
  • Template slots for routes in app.ts, imports and shutdown in server.ts, env settings, docker-compose, the Dockerfile and the README.
  • Projects without a database change in two places. The shutdown callback in server.ts is async, and the env tests check each variable with its own schema, because they'd break once an option adds a required variable.
  • CI. Two generated Drizzle projects (npm with Gatekeeper, pnpm without) migrate, seed, boot and call the users endpoints against a Postgres container. A docker compose job runs Postgres and the API together. Every generated project now has to shut down cleanly on SIGTERM.
  • Drizzle is on its stable line (drizzle-orm 0.45, drizzle-kit 0.31). Drizzle 1.0 is still a release candidate.

Testing

  • I've tested the CLI tool locally (npx tsx src/index.ts test-project)
  • The generated project boots successfully (npm run dev)
  • The generated project's tests pass (npm test)
  • I've run npm run lint with no errors

Every commit passes the CLI checks. At every commit, generated projects with and without Gatekeeper, and with and without Drizzle, pass typecheck, build, tests, lint, the format check and the boot checks from ci.yml.

There's no Docker on my machine, so locally the Drizzle boot checks ran against PGlite's socket server. It speaks the Postgres protocol, so the app goes through the real pg driver. Migrations, seeding twice, every endpoint, 409 on a duplicate email in any case, and a clean shutdown all worked. A pnpm project installs with no extra build-script approvals. The Postgres container and the docker compose job ran for the first time in this PR's CI, and that first run found the Docker healthcheck bug, which shipped in 1.1.2.

Checklist

  • My code follows the project's code style
  • I've updated documentation if needed
  • I've added an entry to CHANGELOG.md (if user-facing change)

@gitguardian

gitguardian Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

⚠️ GitGuardian has uncovered 2 secrets following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

🔎 Detected hardcoded secrets in your pull request
GitGuardian id GitGuardian status Secret Commit Filename
25119854 Triggered Generic Password eef7c98 .github/workflows/ci.yml View secret
25119854 Triggered Generic Password 450040e src/features/drizzle.ts View secret
🛠 Guidelines to remediate hardcoded secrets
  1. Understand the implications of revoking this secret by investigating where it is used in your code.
  2. Replace and store your secrets safely. Learn here the best practices.
  3. Revoke and rotate these secrets.
  4. If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

The env tests parsed objects with a single variable, so they would fail as
soon as an option adds a required one, like DATABASE_URL. They now check
each variable with its own schema.
Marker lines where a database option adds its code: routes in app.ts,
imports and shutdown in server.ts, env.ts, .env.example, docker-compose,
the Dockerfile and the README. Projects without a database don't change,
except that the shutdown callback is async, so an option can wait for its
connections to close before the process exits.
A new setup question and --db flag. The project gets:

- a users module that keeps all its queries in users.repository.ts
- the users table, its first migration, and a migrate script that also
  runs from the build (node dist/db/migrate.js)
- db:seed with three sample users, safe to run twice
- Postgres in docker-compose, with migrations applied before the API starts
- tests on an in-memory Postgres (PGlite) that use the same migrations

On SIGTERM the server closes its database connections after the last
request. Next steps after scaffolding show how to start Postgres, migrate
and seed.
Two more generated projects (npm with Gatekeeper, pnpm without) run their
migrations and seed against a Postgres container, then boot and check the
users endpoints. A docker compose job starts Postgres and the API
together, and checks that migrations ran and seeding works in the image.
Every generated project now also has to shut down cleanly on SIGTERM.
Port 5432 was published on every interface, so anyone on the same network
could reach a database whose password is "postgres". It's now bound to
127.0.0.1, which is all local development and the app's DATABASE_URL need.

This branch has not been deployed

No deployments
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.

1 participant