Multi-Tenancy
How a Nebutra-Sailor project isolates tenants — data, auth, and context across the stack.
What is a tenant?
A tenant is an organization — a company, team, or workspace using your product. Each tenant has its own members (with roles), data (isolated at the database level via RLS), and billing plan. A single user can belong to multiple organizations.
The tenant context
@nebutra/tenant propagates tenant context through the call stack via Node.js
AsyncLocalStorage, so any code running in a request's context can read the current tenant
without it being passed as a parameter.
import { getCurrentTenant } from "@nebutra/tenant";
const tenant = getCurrentTenant();
// → { id: "org_...", slug?: "acme-corp", plan?: "pro", features?: [...], limits?: {...}, metadata?: {...} }Note the field is id, not tenantId — getCurrentTenantId() is a convenience that just returns
getCurrentTenant().id.
plan is "free" | "pro" | "enterprise" (PlanTier), matching the tiers in
Billing Model.
Database isolation
The type system in @nebutra/tenant names three strategies (IsolationStrategy:
shared_schema | schema_per_tenant | database_per_tenant), but shared schema with
row-level security is what the template actually wires up — every table's access rule lives as
an /// @rls comment in packages/platform/db/prisma/schema.prisma, generated into real Postgres
policies. See Row-Level Security for how that's built and
Database Overview for the deploy flow.
import { getTenantDb } from "@nebutra/db";
import { getCurrentTenant } from "@nebutra/tenant";
const tenant = getCurrentTenant();
const db = getTenantDb(tenant.id); // RLS-scoped — only this tenant's rows
const projects = await db.project.findMany();You cannot accidentally query another tenant's data through this client — RLS enforces the boundary at the database engine, not in application code.
Tenant resolution in the gateway
The gateway's tenantContextMiddleware (backends/gateway/src/middlewares/tenantContext.ts)
resolves tenant context in this order:
- Service-to-service call:
x-service-token(a signed HS256 JWT — see Authentication), together withx-organization-id,x-user-id,x-role,x-planheaders. These headers are only trusted when a valid service token accompanies them — there's no way to spoof tenant context by sending headers alone. - Bearer / session token: the auth provider (Better Auth) verifies and decodes it.
- Neither present: the request is unauthenticated; the tenant has no
userId.
There is no subdomain-based or API-key-prefix tenant resolution — the legacy x-tenant-id header
alias was removed (2026-04) specifically because it could be spoofed without JWT verification; any
client still sending it without x-service-token gets a warning logged and the header ignored.
Tenant middleware in your own app
import { tenantMiddleware } from "@nebutra/tenant/middleware";
app.use("*", tenantMiddleware({ headerName: "x-tenant-id" })); // your own resolver configfromHeader, and other resolvers in @nebutra/tenant, are building blocks for your app's own
routes — the gateway's own tenantContextMiddleware (above) is a separate, stricter
implementation already wired in for you.
Member roles
Each tenant member has a role from @nebutra/permissions's default set — see
Permissions for the full model (6 roles, CASL action/resource rules, not
a 4-tier scope hierarchy).
Related
How is this guide?
Last updated on