TenancyConfig¶
TenancyConfig ¶
Bases: BaseSettings
Central configuration for the fastapi-tenancy library.
Instances are validated eagerly: invalid field values and inconsistent field
combinations both raise ValidationError at construction time.
Example — programmatic::
config = TenancyConfig(
database_url="postgresql+asyncpg://user:pass@localhost/myapp",
resolution_strategy="header",
isolation_strategy="schema",
)
Example — environment variables::
# .env
TENANCY_DATABASE_URL=postgresql+asyncpg://user:pass@localhost/myapp
TENANCY_RESOLUTION_STRATEGY=subdomain
TENANCY_DOMAIN_SUFFIX=.example.com
TENANCY_ISOLATION_STRATEGY=rls
config = TenancyConfig() # reads from environment / .env
get_schema_name ¶
Compute the PostgreSQL schema name for tenant_identifier.
The identifier is validated against tenant slug rules before use to prevent SQL injection via schema names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tenant_identifier
|
str
|
The tenant's human-readable slug. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Schema name string (e.g. |
Raises:
| Type | Description |
|---|---|
ValueError
|
When tenant_identifier fails validation. |
Example::
config.get_schema_name("acme-corp") # → "tenant_acme_corp"
Source code in src/fastapi_tenancy/core/config.py
get_database_url_for_tenant ¶
Build the database URL for tenant_id in DATABASE isolation mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tenant_id
|
str
|
The tenant's opaque unique ID. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A fully-qualified async database URL string. |
Source code in src/fastapi_tenancy/core/config.py
is_premium_tenant ¶
Return True if tenant_id is in the premium-tenants list.
Uses an internal frozenset built at construction time for O(1)
lookup — the premium_tenants list may contain thousands of IDs and
this method is called on every request in HYBRID mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tenant_id
|
str
|
Tenant ID to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/fastapi_tenancy/core/config.py
get_isolation_strategy_for_tenant ¶
get_isolation_strategy_for_tenant(
tenant_id: str,
) -> IsolationStrategy
Return the effective isolation strategy for tenant_id.
In HYBRID mode this dispatches to premium_isolation_strategy
or standard_isolation_strategy based on the tenant's tier. In
all other modes it returns isolation_strategy unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tenant_id
|
str
|
Tenant ID to evaluate. |
required |
Returns:
| Type | Description |
|---|---|
IsolationStrategy
|
The |