Dependencies¶
make_tenant_db_dependency ¶
make_tenant_db_dependency(manager: TenancyManager) -> Any
Create a FastAPI dependency that yields a tenant-scoped AsyncSession.
The returned async generator function captures manager in its closure.
This is the correct pattern — no app.state lookup, no circular
imports, and the dependency works regardless of application startup order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manager
|
TenancyManager
|
The configured :class: |
required |
Returns:
| Type | Description |
|---|---|
Any
|
An async generator function suitable for use as a FastAPI |
Example::
get_tenant_db = make_tenant_db_dependency(manager)
@app.get("/orders")
async def list_orders(
session: Annotated[AsyncSession, Depends(get_tenant_db)],
):
result = await session.execute(select(Order))
Source code in src/fastapi_tenancy/dependencies.py
make_tenant_config_dependency ¶
make_tenant_config_dependency(
manager: TenancyManager,
) -> Any
Create a FastAPI dependency that yields the current tenant's config.
Reads tenant.metadata and constructs a TenantConfig with typed
quota and feature-flag fields. Falls back to defaults when fields are
absent from the metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manager
|
TenancyManager
|
The configured :class: |
required |
Returns:
| Type | Description |
|---|---|
Any
|
An async function returning a |
Example::
get_tenant_config = make_tenant_config_dependency(manager)
@app.get("/status")
async def status(
config: Annotated[TenantConfig, Depends(get_tenant_config)],
):
return {"max_users": config.max_users}
Source code in src/fastapi_tenancy/dependencies.py
make_audit_log_dependency ¶
make_audit_log_dependency(manager: TenancyManager) -> Any
Create a FastAPI dependency that provides an audit-log writer function.
Returns a callable that the route handler uses to record operations::
get_audit_logger = make_audit_log_dependency(manager)
@app.delete("/orders/{order_id}")
async def delete_order(
order_id: str,
audit: Annotated[..., Depends(get_audit_logger)],
tenant: TenantDep,
):
...
await audit(action="delete", resource="order", resource_id=order_id)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manager
|
TenancyManager
|
The configured :class: |
required |
Returns:
| Type | Description |
|---|---|
Any
|
An async function that returns a write-audit-log callable. |
Source code in src/fastapi_tenancy/dependencies.py
| Python | |
|---|---|
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |