Multi-Tenant (Drizzle)
Database-per-tenant multi-tenancy with Drizzle ORM and type-safe TenantConnectionManager<TDb>.
Features
TenantAdapterwith subdomain resolution (*.app.example.com)TenantConnectionManager<TDb>— generic, typed connection cacheTenantDbService— injectable wrapper usinggetCurrentTenant()AuthAdapterwith JWT authentication- Provider/tenant pattern with fallback to default database
- Each
getDb()call returns a fully typed Drizzle instance
Key Files
| File | Purpose |
|---|---|
src/db/schema.ts | Shared schema, provider tenant registry |
src/db/tenant-manager.ts | TenantConnectionManager<TDb> + connection factory |
src/db/tenant-db.service.ts | @Service() wrapping tenant DB resolution |
src/index.ts | Bootstrap with TenantAdapter + AuthAdapter |
src/modules/projects/ | Controller demonstrating tenant-scoped queries |
Running
bash
cd examples/multi-tenant-drizzle-api
pnpm install
kick devbash
# Provider (default tenant)
curl http://localhost:3000/api/v1/projects
# Specific tenant (via header since subdomain needs DNS)
curl -H "x-tenant-id: acme" http://localhost:3000/api/v1/projectsSource
See Also
- Multi-Tenancy Guide — concepts, patterns, all ORMs