# Multi-Schema Tenancy {#multi-schema-tenancy}


Database-level tenant isolation strategies.


## Tenant Isolation Models {#tenant-isolation-models}


| Model                 | Description                | Isolation | Complexity |
| --------------------- | -------------------------- | --------- | ---------- |
| **Shared Schema**     | All tenants in one DB      | Low       | Low        |
| **Schema per Tenant** | Separate schema per tenant | Medium    | Medium     |
| **DB per Tenant**     | Separate database          | High      | High       |


## Gauzy's Approach: Shared Schema with Row-Level Security {#gauzys-approach-shared-schema-with-row-level-security}


Gauzy uses a shared schema model with `tenantId` on every entity:

```typescript
export abstract class TenantBaseEntity extends BaseEntity {
  @MultiORMColumn()
  tenantId: string;

  @MultiORMManyToOne(() => Tenant)
  tenant: Tenant;
}
```


### Automatic Tenant Filtering {#automatic-tenant-filtering}


Every query is automatically scoped to the current tenant via:

1. **TenantPermissionGuard** — extracts `tenantId` from JWT
2. **RequestContext** — stores tenant info for the request lifecycle
3. **Service base class** — applies tenant filter to all queries

```typescript
// In TenantAwareCrudService
async findAll(filter: any): Promise<IPagination<T>> {
  const tenantId = RequestContext.currentTenantId();
  return super.findAll({
    ...filter,
    where: { ...filter.where, tenantId },
  });
}
```


## PostgreSQL Row-Level Security (Advanced) {#postgresql-row-level-security-advanced}


For additional database-level enforcement:

```sql
ALTER TABLE employee ENABLE ROW LEVEL SECURITY;

CREATE POLICY tenant_isolation ON employee
  USING (tenant_id = current_setting('app.tenant_id')::uuid);
```


## Cross-Tenant Queries (Admin) {#cross-tenant-queries-admin}


Super admin can query across tenants:

```typescript
if (RequestContext.hasRole(RolesEnum.SUPER_ADMIN)) {
  // Skip tenant filter
  return super.findAll(filter);
}
```


## Related Pages {#related-pages}


- [Multi-Tenant Data Flow](https://sourcey-gauzy.pages.dev/architecture/multi-tenant-data-flow/index.md) — tenant flow
- [Entity Inheritance](https://sourcey-gauzy.pages.dev/architecture/entity-inheritance/index.md) — base entities
- [Guard System](https://sourcey-gauzy.pages.dev/architecture/guard-system/index.md) — tenant guards
