Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Hono Multi-Tenant Context

A proof-of-concept multi-tenant SaaS application using Hono.js, AsyncLocalStorage, and Drizzle ORM with PostgreSQL.

Architecture

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

Quick Start

1. Start Databases

docker-compose up -d

2. Environment Setup

cp .env.example .env

3. Generate and Run Migrations

# 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:tenants

4. Start Development Server

pnpm dev

Database Setup

Services

  • landlord-db (port 5432): Central tenant registry
  • tenant1-db (port 5433): Acme Corp tenant database
  • tenant2-db (port 5434): Beta Industries tenant database

Testing Tenant Isolation

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/

Available Scripts

Database Operations

  • pnpm run db:generate:landlord - Generate landlord migrations
  • pnpm run db:migrate:landlord - Run landlord migrations
  • pnpm run db:generate:tenant - Generate tenant migrations
  • pnpm run db:migrate:tenants - Migrate all tenant databases from registry

Seeding

  • pnpm run db:seed:landlord - Seed landlord database only
  • pnpm run db:seed:tenants - Seed tenant databases only

Development

  • pnpm dev - Start development server

Tenant Migration

The db:migrate:tenants command automatically migrates all tenant databases:

# Migrate all tenants from landlord registry
pnpm run db:migrate:tenants

This command reads the tenant registry from the landlord database and applies migrations to each tenant's database automatically.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages