A proof-of-concept multi-tenant SaaS application using Hono.js, AsyncLocalStorage, and Drizzle ORM with PostgreSQL.
This project implements a database-per-tenant architecture where:
- Each tenant has their own isolated database
- A central "landlord" database stores tenant registry information
- AsyncLocalStorage maintains tenant context throughout request lifecycle
- Tenant resolution happens via subdomain
docker-compose up -dcp .env.example .env# Generate landlord migrations
pnpm run db:generate:landlord
# Migrate landlord database
pnpm run db:migrate:landlord
# Seed landlord database (creates tenant registry)
pnpm run db:seed:landlord
# Generate tenant migrations - only for generating the migrations
pnpm run db:generate:tenant
# Migrate all tenant databases (reads from landlord registry)
pnpm run db:migrate:tenants
# Seed tenant databases
pnpm run db:seed:tenantspnpm dev- landlord-db (port 5432): Central tenant registry
- tenant1-db (port 5433): Acme Corp tenant database
- tenant2-db (port 5434): Beta Industries tenant database
Test with different subdomains:
# Acme Corp tenant
curl -H "Host: acme.localhost:3000" http://localhost:3000/
# Beta Industries tenant
curl -H "Host: beta.localhost:3000" http://localhost:3000/pnpm run db:generate:landlord- Generate landlord migrationspnpm run db:migrate:landlord- Run landlord migrationspnpm run db:generate:tenant- Generate tenant migrationspnpm run db:migrate:tenants- Migrate all tenant databases from registry
pnpm run db:seed:landlord- Seed landlord database onlypnpm run db:seed:tenants- Seed tenant databases only
pnpm dev- Start development server
The db:migrate:tenants command automatically migrates all tenant databases:
# Migrate all tenants from landlord registry
pnpm run db:migrate:tenantsThis command reads the tenant registry from the landlord database and applies migrations to each tenant's database automatically.