new TenantContext()
Manages tenant context for multi-tenant applications using DynamoBao. Provides runtime tenant resolution and instance management with concurrency safety.
- Source
Methods
(static) addResolver(resolver)
Adds a resolver function to determine tenant context. Resolvers are tried in the order they were added.
Name | Type | Description |
---|---|---|
resolver | function | Function that returns a tenant ID or null |
- Source
(static) clearResolvers()
Clears all resolver functions.
- Source
(static) clearTenant()
Clears the current tenant context. For test cleanup and simple use cases.
- Source
(static) getCurrentTenant() → {string|null}
Gets the current tenant ID using async context or resolver chain. Concurrency-safe using AsyncLocalStorage.
- Source
The current tenant ID or null if not found
- Type:
- string |
null
(static) getInstance(tenantId) → {ModelManager}
Gets or creates a ModelManager instance for the specified tenant.
Name | Type | Default | Description |
---|---|---|---|
tenantId | string | | null | The tenant identifier or null for default |
- Source
The ModelManager instance
- Type:
- ModelManager
(static) reset()
Resets all tenant context state (for testing).
- Source
(static) runWithTenant(tenantId, callback) → {Promise}
Runs a callback function with a specific tenant context. Ensures concurrency safety by using AsyncLocalStorage.
Name | Type | Description |
---|---|---|
tenantId | string | The tenant identifier |
callback | function | The function to run with tenant context |
- Source
The result of the callback
- Type:
- Promise
(static) setCurrentTenant(tenantId) → {ModelManager}
Sets the current tenant ID and returns the associated ModelManager instance. For backward compatibility and simple use cases (like tests).
Name | Type | Description |
---|---|---|
tenantId | string | The tenant identifier |
- Source
The ModelManager instance for the tenant
- Type:
- ModelManager
(static) validateTenantRequired(config)
Validates that tenant context is available when required by configuration.
Name | Type | Description |
---|---|---|
config | Object | The configuration object |
- Source
If tenancy is enabled but no tenant context is found
- Type
- Error
(static) withTenant(tenantId, operation) → {Promise}
Explicit tenant override for cross-tenant operations. Alias for runWithTenant for better readability in cross-tenant scenarios.
Name | Type | Description |
---|---|---|
tenantId | string | The tenant identifier |
operation | function | The operation to run with tenant context |
- Source
The result of the operation
- Type:
- Promise