TenantStore
Defined in: packages/ddd-mikro-orm/src/tenancy/tenant-store.ts:43
The EntityManager source of a multi-tenant application: em and
transactional() always act on the active tenant’s database or schema, for
AggregateRepository, MikroOrmCache and the application’s own repositories.
em reuses a contextual manager (a MikroORM RequestContext or an active
transaction) only when it belongs to the tenant’s ORM, configuration, driver
and schema and no other tenant claimed it first; otherwise it forks one.
transactional() always runs on a fresh fork. Every manager it hands out is
recorded to its tenant outside the manager (a WeakMap), so a later request
for another tenant cannot reuse it; MikroORM objects are never modified.
Example
Section titled “Example”const store = new TenantStore({ tenant: () => currentTenantId() ?? fail(), orm: (tenant) => orms.get(tenant) ?? fail(), isolation: 'database',});const repository = new UpdateUserRepository(cache, store);Type Parameters
Section titled “Type Parameters”TManager
Section titled “TManager”TManager extends EntityManager = EntityManager
Implements
Section titled “Implements”Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new TenantStore<
TManager>(options):TenantStore<TManager>
Defined in: packages/ddd-mikro-orm/src/tenancy/tenant-store.ts:48
Parameters
Section titled “Parameters”options
Section titled “options”Returns
Section titled “Returns”TenantStore<TManager>
Accessors
Section titled “Accessors”Get Signature
Section titled “Get Signature”get em():
TManager
Defined in: packages/ddd-mikro-orm/src/tenancy/tenant-store.ts:56
The active tenant’s manager: its contextual one when that may be reused, otherwise a new fork. Read it per operation, never keep it.
Throws
Section titled “Throws”What options.tenant or options.orm throws.
Returns
Section titled “Returns”TManager
Implementation of
Section titled “Implementation of”Methods
Section titled “Methods”transactional()
Section titled “transactional()”transactional<
T>(work):Promise<T>
Defined in: packages/ddd-mikro-orm/src/tenancy/tenant-store.ts:77
Runs work in a transaction on a fork dedicated to this call, for the
active tenant; commits when it resolves and rolls back when it rejects.
What options.tenant or options.orm throws becomes its rejection.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”(em) => Promise<T>
Returns
Section titled “Returns”Promise<T>
Example
Section titled “Example”await store.transactional((em) => em.nativeDelete(CacheEntry, { key }));