Database
Database
NextDevTpl uses PostgreSQL with Drizzle ORM. The schema is the source of truth for auth, credits, payments, support tickets, and user data.
Main files
| Path | Purpose |
|---|---|
src/db/schema.ts | Table definitions |
src/db/index.ts | Database client |
drizzle.config.ts | Drizzle config |
drizzle/ | Generated migration files and snapshots |
src/test/utils/db.ts | Test database helpers |
Development workflow
For a new local database:
pnpm db:push
For a change that should be reviewed and deployed:
pnpm db:generate
pnpm db:migrate
Use Drizzle Studio to inspect local data:
pnpm db:studio
When to use push vs migrations
| Command | Use it when |
|---|---|
pnpm db:push | You are bootstrapping or iterating on a local database |
pnpm db:generate | You changed src/db/schema.ts and need a migration file |
pnpm db:migrate | You want to apply committed migrations |
Schema changes checklist
- Update
src/db/schema.ts. - Update any feature service/action that reads or writes the changed table.
- Generate or push the schema depending on environment.
- Add or update tests under
src/test. - Run
pnpm typecheckand targeted tests.
Common table areas
| Area | Used by |
|---|---|
| Auth users and sessions | Better Auth |
| Credits and transactions | Credits ledger and purchases |
| Subscriptions | Payment and plan lookup |
| Support tickets | User support and admin ticket queue |
| Storage records | Upload and file security tests |