ambiten / core / MultiTenantManager
Class: MultiTenantManager
Defined in: packages/core/src/tanancy/MultiTenantManager.ts:23
MultiTenantManager is responsible for managing tenant configurations and MongoDB client connections in a multi-tenant application. It supports both lazy and immediate tenant registration, allowing for flexible connection management based on application needs.
Constructors
Constructor
new MultiTenantManager():
MultiTenantManager
Defined in: packages/core/src/tanancy/MultiTenantManager.ts:26
Returns
MultiTenantManager
Methods
clearTenants()
staticclearTenants():void
Defined in: packages/core/src/tanancy/MultiTenantManager.ts:209
Clears all tenants from the registry.
Returns
void
getAllConnectedTenants()
staticgetAllConnectedTenants():string[]
Defined in: packages/core/src/tanancy/MultiTenantManager.ts:178
Returns all currently connected tenant IDs.
Returns
string[]
getAllTenants()
staticgetAllTenants():TenantConfig[]
Defined in: packages/core/src/tanancy/MultiTenantManager.ts:187
Returns all registered tenant configs.
Returns
getClient()
staticgetClient(tenantId):Promise<MongoClient|null>
Defined in: packages/core/src/tanancy/MultiTenantManager.ts:117
Retrieves the MongoClient instance for a specific tenant. If the tenant was registered lazily, connection is established on first access.
Parameters
tenantId
string
The tenant ID.
Returns
Promise<MongoClient | null>
The MongoClient or null if not registered.
getConnectedTenant()
staticgetConnectedTenant():string
Defined in: packages/core/src/tanancy/MultiTenantManager.ts:170
Returns the first connected tenant ID. Useful for backward compatibility, though not ideal in multi-tenant flows.
Returns
string
getTenant()
staticgetTenant(tenantId):TenantConfig|undefined
Defined in: packages/core/src/tanancy/MultiTenantManager.ts:151
Retrieves the full tenant configuration.
Parameters
tenantId
string
The tenant ID.
Returns
TenantConfig | undefined
The tenant configuration if found.
getTenantDbName()
staticgetTenantDbName(tenantId):string|undefined
Defined in: packages/core/src/tanancy/MultiTenantManager.ts:162
Retrieves the configured database name for a tenant.
Parameters
tenantId
string
The tenant ID.
Returns
string | undefined
The database name if found.
hasTenant()
statichasTenant(tenantId):boolean
Defined in: packages/core/src/tanancy/MultiTenantManager.ts:33
Checks if a tenant is already registered.
Parameters
tenantId
string
The ID of the tenant to check.
Returns
boolean
true if the tenant is registered, false otherwise.
isEnabled()
staticisEnabled():boolean
Defined in: packages/core/src/tanancy/MultiTenantManager.ts:194
Returns true if at least one tenant is registered.
Returns
boolean
registerLazyTenant()
staticregisterLazyTenant(tenantId,uri,options?):TenantConfig
Defined in: packages/core/src/tanancy/MultiTenantManager.ts:45
Registers a tenant for lazy connection. Connection is established only when the tenant is accessed for the first time.
Parameters
tenantId
string
The tenant ID.
uri
string
The MongoDB URI.
options?
Omit<RegisterTenantOptions, "lazy" | "client"> = {}
Optional tenant registration settings.
Returns
registerTenant()
staticregisterTenant(tenantId,uri,options?):Promise<MongoClient>
Defined in: packages/core/src/tanancy/MultiTenantManager.ts:77
Registers a tenant and establishes a connection immediately.
Parameters
tenantId
string
The tenant ID.
uri
string
The MongoDB URI.
options?
Omit<RegisterTenantOptions, "lazy"> = {}
Optional tenant registration settings.
Returns
Promise<MongoClient>
The connected MongoClient instance.
removeTenant()
staticremoveTenant(tenantId):boolean
Defined in: packages/core/src/tanancy/MultiTenantManager.ts:201
Removes a tenant from the registry.
Parameters
tenantId
string
Returns
boolean
