Skip to content

ambiten


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

static clearTenants(): void

Defined in: packages/core/src/tanancy/MultiTenantManager.ts:209

Clears all tenants from the registry.

Returns

void


getAllConnectedTenants()

static getAllConnectedTenants(): string[]

Defined in: packages/core/src/tanancy/MultiTenantManager.ts:178

Returns all currently connected tenant IDs.

Returns

string[]


getAllTenants()

static getAllTenants(): TenantConfig[]

Defined in: packages/core/src/tanancy/MultiTenantManager.ts:187

Returns all registered tenant configs.

Returns

TenantConfig[]


getClient()

static getClient(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()

static getConnectedTenant(): 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()

static getTenant(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()

static getTenantDbName(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()

static hasTenant(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()

static isEnabled(): boolean

Defined in: packages/core/src/tanancy/MultiTenantManager.ts:194

Returns true if at least one tenant is registered.

Returns

boolean


registerLazyTenant()

static registerLazyTenant(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

TenantConfig


registerTenant()

static registerTenant(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()

static removeTenant(tenantId): boolean

Defined in: packages/core/src/tanancy/MultiTenantManager.ts:201

Removes a tenant from the registry.

Parameters

tenantId

string

Returns

boolean

Context-aware execution infrastructure for multi-tenant systems.