Skip to content

@venturekit-pro/tenancy

Terminal window
npm install @venturekit-pro/tenancy@dev
import { TenantContext, createTenantContext, getCurrentTenant, resolveTenant } from '@venturekit-pro/tenancy';
const tenant = getCurrentTenant(ctx);
// { id: 'acme', slug: 'acme', metadata: { ... } }
import { createTenantMiddleware, createQuotaMiddleware } from '@venturekit-pro/tenancy';
import type { IsolationConfig } from '@venturekit-pro/tenancy';
// `strategy` selects the **data** isolation model (shared tables vs schema vs
// database). `resolution` selects how the tenant is identified per request.
const isolation: IsolationConfig = { strategy: 'shared', resolution: 'subdomain' };
// `createTenantMiddleware()` requires `config` (resolution + isolation rules)
// and `lookupTenant` (your tenant store). `createQuotaMiddleware()` requires
// `quotaKey` and `checkUsage` (returns current usage for the quota).
middleware: [
createTenantMiddleware({
config: isolation,
lookupTenant: async (id) => loadTenant(id),
}),
createQuotaMiddleware({
quotaKey: 'apiRequests',
checkUsage: async (tenantId) => getMonthlyUsage(tenantId),
}),
]
import { TenantNotFoundError, TenantSuspendedError, TenantInactiveError, QuotaExceededError } from '@venturekit-pro/tenancy';
import { checkQuotas } from '@venturekit-pro/tenancy';
// checkQuotas(tenantId, quotas, checkUsage)
// quotas: TenantQuotas — a map of quotaKey → numeric limit
// checkUsage: (tenantId, quotaKey) => Promise<number> — current usage
await checkQuotas(
tenantId,
{ apiRequests: 10_000, storage: 5_000_000_000 },
async (id, quotaKey) => loadCurrentUsage(id, quotaKey),
);
StrategySource
subdomainacme.app.example.com
custom_domainapp.acme.com (domain lookup via domainLookup)
path/t/acme/api/tasks
headerX-Tenant-ID: acme (configurable via headerName)
query?tenant=acme (configurable via queryParam)
jwttenant_id claim (configurable via jwtClaim)
custompluggable async resolver via customResolver
  • @venturekit/core — required