Skip to main content

Crate rok_tenant

Crate rok_tenant 

Source
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_id column is filtered automatically via a global scope when #[model(tenant_scoped)] is applied.
  • Separate schemas — Use TenantSource::Subdomain to route each request to a dedicated PostgreSQL schema (integration with rok-orm schema 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§

error

Structs§

TenantLayer
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§

TenantSource
How the TenantLayer extracts 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 None when the tenant feature is disabled.