This is a demo project to show how to implement a schema based multi tenancy in Adonis 6.
- Tenant isolation: Each tenant has its own database schema.
- Tenant commands: Migrate, seed, rollback, and run commands for a specific tenant.
- Tenant middleware: Automatically switch the tenant based on the request header.
- Tenant models: Lucid models are automatically scoped to the current tenant.
- Backoffice isolation: Backoffice models, requests and controllers are isolated from tenants.
- Public schema: Shared tables are stored in the public schema.
- Install dependencies:
npm install
- Create a
.env
file:
cp .env.example .env
- Mount storage services
docker-compose up -d
- Setup public and backoffice schemas
node ace migration:run -c=public
node ace db:seed -c=public
node ace backoffice:setup
node ace migration:run -c=backoffice
node ace db:seed -c=backoffice
- Start the server and bull queue
npm run dev
node ace queue:listen
GET /countries
: List all countries.
POST /backoffice/login
: Login to the backoffice. [email: john.doe@example.com, password: 123456789]DELETE /backoffice/logout
: Logout from the backoffice.GET /backoffice/account
: Return the current user account.GET /backoffice/tenants
: List all tenants.POST /backoffice/tenants
: Create a new tenant.
All routes below require the X-Tenant-Id
header to be set with the tenant id.
POST /tenant/login
: User tenant login.DELETE /tenant/logout
: User tenant logout.POST /tenant/signup
: User tenant signup.GET /tenant/account
: Return the current user account.GET /tenant/users
: List all users.
- Get the current tenant:
request.tenant()
- Start a database transaction in the tenant scope:
tenant.getConnection().transaction(async (trx) => { ... })
- Migrate a single tenant:
node ace migration:tenant:run -t=tenantId
- Migrate all tenants:
node ace migration:tenant:run
- Seed a single tenant:
node ace tenant:seed -t=tenantId
- Seed all tenants:
node ace tenant:seed