Database ORMs
Which ORM lets you write SQL-close queries, handle schema migrations, and deploy to the edge?
Selection Criteria
The ORM sits in the data-repositories layer of hexagonal architecture — it implements the ports that domain logic depends on. Wrong choice here ripples through every query, every migration, every deploy.
| Criteria | Why It Matters | Weight |
|---|---|---|
| Type safety | Schema changes caught at compile time, not production | Must have |
| SQL proximity | Readable queries, no magic. Debug what you wrote | Must have |
| Edge execution | Vercel Edge, Cloudflare Workers — no heavy runtime | Must have |
| Migration tooling | Declarative schema changes, rollback support | Must have |
| Multi-tenant scoping | organisationId filtering composable into every query | Must have |
| Monorepo support | Works cleanly in Nx workspace with shared schema | Should have |
| Ecosystem | Community health, plugin ecosystem, active maintenance | Should have |
Candidates
| Drizzle | Prisma | Kysely | SQLd | |
|---|---|---|---|---|
| Type safety | Full — schema-as-code in TS | Full — generated client from schema file | Full — query builder with inference | Partial — libSQL typed |
| SQL proximity | High — reads like SQL | Low — custom query API | High — query builder | High — raw SQL |
| Edge runtime | Yes — lightweight | No — binary engine required | Yes — lightweight | Yes — SQLite-based |
| Migrations | drizzle-kit — declarative | prisma migrate — mature | Manual or third-party | Manual |
| Multi-tenant | Composable .where() chains | Middleware or $extends | Composable .where() chains | Per-DB isolation |
| Monorepo | Clean — just TS files | Painful — nx workspace issues | Clean — just TS files | N/A |
| Ecosystem | Growing fast, Drizzle Studio | Largest, Prisma Studio | Smaller, focused | Turso-specific |
Our Choice
Drizzle — SQL-close, edge-native, schema-as-TypeScript-code. No binary engine, no code generation step, composes naturally with organisationId scoping in the ApplicationContext pattern.
Prisma was the previous choice. The migration cost is real but the edge constraint and monorepo friction made it unavoidable.
Context
- Multi-Tenant SaaS — data isolation protocol that the ORM must enforce
- Hexagonal Architecture — the layer pattern that defines where the ORM sits
- Next.js DTOs — how ORM output maps to service layer
- Identity and Security — auth decisions upstream of data access
- Tech Decisions — the general process for evaluating stack choices