Multi-Tenancy Architecture

ContractSpec is designed for B2B SaaS applications where data isolation is critical. We support multiple isolation strategies ranging from logical separation (RLS) to physical separation (database-per-tenant).

Row-Level Security (RLS)

By default, ContractSpec uses logical isolation via Row-Level Security. Every database query is intercepted by middleware that injects the current tenantId into the WHERE clause.

// User code
await db.users.findMany({});

// Actual query executed
SELECT * FROM "User" WHERE "tenantId" = 'current-tenant-id';

Tenant Context

The tenant context is resolved at the API layer (e.g., from a subdomain or header) and propagated through the WorkflowRunner andOperationExecutor.