Skip to main content

Overview

Multi-tenancy means multiple customers (tenants) share the same database but each can only see their own data. IlanaORM’s global scopes make this automatic — once set up, every query on a model is silently filtered to the current tenant.
This tutorial uses a tenant_id column approach (row-level tenancy), which works with any relational database.

1. Add tenant_id to Your Tables

Already know your schema? Run npx ilana make:model Post --migration to scaffold both files at once and skip straight to Step 2.

2. Create a Tenant Context

Store the current tenant in an async context so it’s available anywhere without passing it around:

3. Create a Tenant-Aware Base Model

Extend TenantModel instead of Model for any tenant-scoped model:

4. Wire Up the Middleware

5. Everything Just Works

Now every query is automatically scoped:

6. Bypass the Scope When Needed

For admin routes that need cross-tenant access:

Summary