interface IMultiTenancyService
Since 0.1.0

Multi-tenancy service — exposes tenant context, repository creation, and cache-key helpers.

Methods

Return the tenant resolved for this request context, or undefined.

getRepository<Entity, Id = string>(
entity: string
): ITenantRepository<Entity, Id>

Create a tenant-scoped repository for the given entity type. Throws TenantNotResolvedError if no tenant is resolved.

Since 0.4.0
getRepositoryFor<Entity, Id = string>(
tenantId: string,
entity: string
): ITenantRepository<Entity, Id>

Create a tenant-scoped repository for the given entity type, scoped to the tenant id GIVEN — no IRequestContext required. This is the entry point for non-HTTP work (an ingress behaviour, a queue processor, a scheduled job), where no request exists to resolve a tenant from; the caller reads the tenant id from the work item's own payload. Modelled on prefixCacheKey — this interface's other ctx-free, id-taking member.

The id is TRUSTED INPUT: nothing resolves it, so a caller passing a user-controlled value bypasses the resolved tenant. On the HTTP path use getRepository, which reads the middleware-resolved ctx.request.tenant.

prefixCacheKey(
tenantId: string,
key: string
): string

Build a cache key that includes the tenant id, joined by the separator the plugin was configured with (cache.separator, default ':'). The separator is deliberately NOT a per-call argument: this method is the single home for separator resolution, so the middleware's ctx.state prefix and a caller's key can never disagree.

Usage

import { type IMultiTenancyService } from "common/src/index.ts";