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.
- Central schema: Shared tables are stored in the central schema.
- Install dependencies:
npm install- Create a
.envfile:
cp .env.example .env- Mount storage services
docker-compose up -d- Setup central and backoffice schemas (check the ``config/multitenancy.ts` file for the connection names)
node ace migration:run -c=<< central connection name >>
node ace db:seed -c=<< central connection name >>
node ace migration:run -c=<< backoffice connection name >>
node ace db:seed -c=<< backoffice connection name >>- Start the server and bull queue
npm run dev
node ace queue:listenGET /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