Skip to main content

rok_tenant/
lib.rs

1//! rok-tenant — Multi-tenancy for the rok ecosystem.
2//!
3//! Provides row-level tenant isolation via task-local tenant ID storage and a
4//! Tower middleware that resolves the tenant per request.
5//!
6//! Two resolution strategies are supported:
7//!
8//! - **Shared table** — A `tenant_id` column is filtered automatically via a
9//!   global scope when `#[model(tenant_scoped)]` is applied.
10//! - **Separate schemas** — Use [`TenantSource::Subdomain`] to route each
11//!   request to a dedicated PostgreSQL schema (integration with `rok-orm`
12//!   schema switching is handled by the ORM layer).
13//!
14//! # Quick start
15//!
16//! ```rust,ignore
17//! use rok_tenant::{TenantLayer, TenantSource, current_tenant_id};
18//!
19//! // Extract tenant from X-Tenant-ID header
20//! let app = Router::new()
21//!     .nest("/", routes())
22//!     .layer(TenantLayer::from_header("X-Tenant-ID"));
23//!
24//! // Or from the subdomain
25//! let app = Router::new()
26//!     .nest("/", routes())
27//!     .layer(TenantLayer::from_subdomain());
28//!
29//! // Inside a handler or model method:
30//! let tenant = current_tenant_id(); // → Option<i64>
31//! ```
32
33pub use rok_orm_core::tenant::current_tenant_id;
34pub use rok_orm_core::tenant::{TenantLayer, TenantSource};
35
36pub mod error;
37pub use error::TenantError;