Skip to content

Examples

Complete examples demonstrating drizzle-multitenant with different frameworks.

Prerequisites

Start PostgreSQL with Docker:

yaml
# docker-compose.yml
services:
  postgres:
    image: postgres:16-alpine
    container_name: drizzle-multitenant-examples
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: multitenant
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 5

volumes:
  postgres_data:
bash
docker-compose up -d

Available Examples

ExampleFrameworkDescription
ExpressExpress.jsREST API with CRUD and retry
FastifyFastifyProject management API
NestJSNestJSE-commerce with modules

Testing Tenant Isolation

Each tenant has its own schema. Data created for one tenant is invisible to others:

bash
# Create user in tenant 'acme'
curl -X POST http://localhost:3000/users \
  -H "Content-Type: application/json" \
  -H "X-Tenant-ID: acme" \
  -d '{"email": "john@acme.com", "name": "John"}'

# List users in 'acme' - returns John
curl http://localhost:3000/users -H "X-Tenant-ID: acme"

# List users in 'globex' - returns empty (different schema)
curl http://localhost:3000/users -H "X-Tenant-ID: globex"

Environment Variables

VariableDefaultDescription
DATABASE_URLpostgresql://postgres:postgres@localhost:5432/multitenantPostgreSQL connection
PORT3000/3001/3002Server port
NODE_ENV-Set to development for debug logs

Released under the MIT License.