new TenantContext()
- Description:
Manages tenant context for multi-tenant applications using DynamoBao. Provides runtime tenant resolution and instance management with concurrency safety.
- Source:
Methods
(static) addResolver(resolver)
- Description:
Adds a resolver function to determine tenant context. Resolvers are tried in the order they were added.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
resolver |
function | Function that returns a tenant ID or null |
(static) clearResolvers()
- Description:
Clears all resolver functions.
- Source:
(static) clearTenant()
- Description:
Clears the current tenant. Inside runWithTenant() or runWithBatchContext() this clears only that context's tenant; outside both it clears the process-wide tenant.
- Source:
(static) getCurrentTenant() → {string|null}
- Description:
Gets the current tenant ID. Checked in order: the runWithTenant() context, a tenant set inside the current batch context, resolvers, and finally the process-wide tenant (only outside runWithTenant()).
- Source:
Returns:
The current tenant ID or null if not found
- Type
- string | null
(static) getInstance(tenantId) → {ModelManager}
- Description:
Gets or creates a ModelManager instance for the specified tenant.
- Source:
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
tenantId |
string | null |
null
|
The tenant identifier or null for default |
Returns:
The ModelManager instance
- Type
- ModelManager
(static) reset()
- Description:
Resets all tenant context state (for testing).
- Source:
(static) runWithTenant(tenantId, callback) → {Promise}
- Description:
Runs a callback function with a specific tenant context. Ensures concurrency safety by using AsyncLocalStorage.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
tenantId |
string | The tenant identifier |
callback |
function | The function to run with tenant context |
Returns:
The result of the callback
- Type
- Promise
(static) setCurrentTenant(tenantId) → {ModelManager}
- Description:
Sets the current tenant ID and returns the associated ModelManager instance.
Inside runWithTenant() or runWithBatchContext() the tenant is set for that context only. Outside both, it sets a process-wide tenant, which is only appropriate for scripts and tests: concurrent requests that have no tenant of their own would see it.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
tenantId |
string | The tenant identifier |
Returns:
The ModelManager instance for the tenant
- Type
- ModelManager
(static) setDefaultTenant(tenantId) → {ModelManager}
- Description:
Sets the process-wide tenant regardless of the current context. Meant for scripts, jobs and tests that handle one tenant at a time; never call it from request handling code.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
tenantId |
string | null | The tenant identifier, or null to clear it |
Returns:
The ModelManager instance for the tenant
- Type
- ModelManager
(static) validateTenantRequired(config)
- Description:
Validates that tenant context is available when required by configuration.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
config |
Object | The configuration object |
Throws:
-
If tenancy is enabled but no tenant context is found
- Type
- Error
(static) withTenant(tenantId, operation) → {Promise}
- Description:
Explicit tenant override for cross-tenant operations. Alias for runWithTenant for better readability in cross-tenant scenarios.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
tenantId |
string | The tenant identifier |
operation |
function | The operation to run with tenant context |
Returns:
The result of the operation
- Type
- Promise