TenantContext

new TenantContext()

Manages tenant context for multi-tenant applications using DynamoBao. Provides runtime tenant resolution and instance management with concurrency safety.

Methods

(static) addResolver(resolver)

Adds a resolver function to determine tenant context. Resolvers are tried in the order they were added.

Parameters:
NameTypeDescription
resolverfunction

Function that returns a tenant ID or null

(static) clearResolvers()

Clears all resolver functions.

(static) clearTenant()

Clears the current tenant context. For test cleanup and simple use cases.

(static) getCurrentTenant() → {string|null}

Gets the current tenant ID using async context or resolver chain. Concurrency-safe using AsyncLocalStorage.

Returns:

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.

Parameters:
NameTypeDefaultDescription
tenantIdstring | nullnull

The tenant identifier or null for default

Returns:

The ModelManager instance

Type: 
ModelManager

(static) reset()

Resets all tenant context state (for testing).

(static) runWithTenant(tenantId, callback) → {Promise}

Runs a callback function with a specific tenant context. Ensures concurrency safety by using AsyncLocalStorage.

Parameters:
NameTypeDescription
tenantIdstring

The tenant identifier

callbackfunction

The function to run with tenant context

Returns:

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).

Parameters:
NameTypeDescription
tenantIdstring

The tenant identifier

Returns:

The ModelManager instance for the tenant

Type: 
ModelManager

(static) validateTenantRequired(config)

Validates that tenant context is available when required by configuration.

Parameters:
NameTypeDescription
configObject

The configuration object

Throws:

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.

Parameters:
NameTypeDescription
tenantIdstring

The tenant identifier

operationfunction

The operation to run with tenant context

Returns:

The result of the operation

Type: 
Promise