Expand description
rok-tenant — Multi-tenancy for the rok ecosystem.
Provides row-level tenant isolation via task-local tenant ID storage and a Tower middleware that resolves the tenant per request.
Two resolution strategies are supported:
- Shared table — A
tenant_idcolumn is filtered automatically via a global scope when#[model(tenant_scoped)]is applied. - Separate schemas — Use
TenantSource::Subdomainto route each request to a dedicated PostgreSQL schema (integration withrok-ormschema switching is handled by the ORM layer).
§Quick start
ⓘ
use rok_tenant::{TenantLayer, TenantSource, current_tenant_id};
// Extract tenant from X-Tenant-ID header
let app = Router::new()
.nest("/", routes())
.layer(TenantLayer::from_header("X-Tenant-ID"));
// Or from the subdomain
let app = Router::new()
.nest("/", routes())
.layer(TenantLayer::from_subdomain());
// Inside a handler or model method:
let tenant = current_tenant_id(); // → Option<i64>Re-exports§
pub use error::TenantError;
Modules§
Structs§
- Tenant
Layer - Tower layer that extracts a tenant ID per-request and scopes it in
task-local storage so that
Model::query()can auto-apply tenant filters.
Enums§
- Tenant
Source - How the
TenantLayerextracts the tenant ID from each request.
Functions§
- current_
tenant_ id - Returns the current request’s tenant ID from the task-local scope, if any.
Always returns
Nonewhen thetenantfeature is disabled.