@nestarc/tenancy
Classes
BullTenantPropagator
Defined in: src/propagation/bull-tenant-propagator.ts:30
Bull/BullMQ tenant propagator.
Injects the current tenant ID into job data on the producer side, and extracts it on the consumer side. Uses a configurable key (default: __tenantId) to avoid collisions with application data.
No runtime dependency on bullmq — uses plain object types.
Example
const propagator = new BullTenantPropagator(new TenancyContext());
// Producer: inject tenant into job data
await queue.add('process', propagator.inject({ orderId: '123' }));
// Consumer: extract tenant from job data
const tenantId = propagator.extract(job.data);Implements
TenantContextCarrier<Record<string,unknown>>
Constructors
Constructor
new BullTenantPropagator(context, options?): BullTenantPropagator;Defined in: src/propagation/bull-tenant-propagator.ts:35
Parameters
| Parameter | Type |
|---|---|
context | TenancyContext |
options? | BullPropagationOptions |
Returns
Methods
extract()
extract(jobData): string | null;Defined in: src/propagation/bull-tenant-propagator.ts:53
Extracts the tenant ID from an incoming carrier. Returns the tenant ID string, or null if not present.
Parameters
| Parameter | Type |
|---|---|
jobData | Record<string, unknown> |
Returns
string | null
Implementation of
inject()
inject(jobData): Record<string, unknown>;Defined in: src/propagation/bull-tenant-propagator.ts:42
Attaches the current tenant ID to the carrier for outbound propagation. Returns the carrier with tenant context included. If no tenant context is available, returns the carrier unchanged.
Parameters
| Parameter | Type |
|---|---|
jobData | Record<string, unknown> |
Returns
Record<string, unknown>
Implementation of
CompositeTenantExtractor
Defined in: src/extractors/composite.extractor.ts:4
Contract for extracting a tenant ID from an inbound HTTP request.
Return the tenant ID string when present, or null when the request does not carry tenant information. A missing tenant is not an error condition; TenantMiddleware will call onTenantNotFound and let the application decide whether to continue, respond, or throw.
Implementations may return synchronously or return a Promise for async lookups. Throw only for malformed input or policy failures that should reject the request immediately.
Implements
Constructors
Constructor
new CompositeTenantExtractor(extractors): CompositeTenantExtractor;Defined in: src/extractors/composite.extractor.ts:7
Parameters
| Parameter | Type |
|---|---|
extractors | TenantExtractor[] |
Returns
Methods
extract()
extract(request): string | Promise<string | null> | null;Defined in: src/extractors/composite.extractor.ts:11
Parameters
| Parameter | Type |
|---|---|
request | TenancyRequest |
Returns
string | Promise<string | null> | null
Implementation of
GrpcTenantPropagator
Defined in: src/propagation/grpc-tenant-propagator.ts:43
gRPC tenant propagator.
Injects tenant ID into gRPC call metadata on the client side, and extracts it on the server side.
Uses lowercase metadata keys per gRPC convention (keys are case-insensitive but lowercase is standard).
No runtime dependency on @grpc/grpc-js — uses structural types.
Example
const propagator = new GrpcTenantPropagator(new TenancyContext());
// Client: inject tenant into outgoing metadata
const metadata = new Metadata();
propagator.inject(metadata);
// Server: extract tenant from incoming metadata
const tenantId = propagator.extract(call.metadata);Implements
Constructors
Constructor
new GrpcTenantPropagator(context, options?): GrpcTenantPropagator;Defined in: src/propagation/grpc-tenant-propagator.ts:48
Parameters
| Parameter | Type |
|---|---|
context | TenancyContext |
options? | GrpcPropagationOptions |
Returns
Methods
extract()
extract(metadata): string | null;Defined in: src/propagation/grpc-tenant-propagator.ts:62
Extracts the tenant ID from an incoming carrier. Returns the tenant ID string, or null if not present.
Parameters
| Parameter | Type |
|---|---|
metadata | GrpcMetadataLike |
Returns
string | null
Implementation of
inject()
inject(metadata): GrpcMetadataLike;Defined in: src/propagation/grpc-tenant-propagator.ts:55
Attaches the current tenant ID to the carrier for outbound propagation. Returns the carrier with tenant context included. If no tenant context is available, returns the carrier unchanged.
Parameters
| Parameter | Type |
|---|---|
metadata | GrpcMetadataLike |
Returns
Implementation of
HeaderTenantExtractor
Defined in: src/extractors/header.extractor.ts:4
Contract for extracting a tenant ID from an inbound HTTP request.
Return the tenant ID string when present, or null when the request does not carry tenant information. A missing tenant is not an error condition; TenantMiddleware will call onTenantNotFound and let the application decide whether to continue, respond, or throw.
Implementations may return synchronously or return a Promise for async lookups. Throw only for malformed input or policy failures that should reject the request immediately.
Implements
Constructors
Constructor
new HeaderTenantExtractor(headerName): HeaderTenantExtractor;Defined in: src/extractors/header.extractor.ts:7
Parameters
| Parameter | Type |
|---|---|
headerName | string |
Returns
Methods
extract()
extract(request): string | null;Defined in: src/extractors/header.extractor.ts:11
Parameters
| Parameter | Type |
|---|---|
request | TenancyRequest |
Returns
string | null
Implementation of
HttpTenantPropagator
Defined in: src/propagation/http-tenant-propagator.ts:23
HTTP-specific tenant propagator.
Reads the current tenant from TenancyContext and returns it as an HTTP header. Returns an empty object when no tenant context is available.
Example
const propagator = new HttpTenantPropagator(tenancyContext);
const headers = propagator.getHeaders();
// { 'X-Tenant-Id': 'tenant-abc' }Implements
Constructors
Constructor
new HttpTenantPropagator(context, options?): HttpTenantPropagator;Defined in: src/propagation/http-tenant-propagator.ts:26
Parameters
| Parameter | Type |
|---|---|
context | TenancyContext |
options? | HttpPropagationOptions |
Returns
Methods
getHeaders()
getHeaders(): Record<string, string>;Defined in: src/propagation/http-tenant-propagator.ts:33
Returns headers to propagate tenant context. Returns an empty object if no tenant context is available.
Returns
Record<string, string>
Implementation of
JwtClaimTenantExtractor
Defined in: src/extractors/jwt-claim.extractor.ts:37
Extracts the tenant ID from a JWT claim in the Authorization header.
IMPORTANT: This extractor does NOT verify the JWT signature. It decodes the payload (Base64URL) without cryptographic validation. You MUST ensure that JWT authentication (e.g., @nestjs/passport AuthGuard, or an upstream auth middleware) has already validated the token before this extractor runs. Using this extractor without prior JWT verification allows attackers to forge tenant IDs via crafted tokens.
Implements
Constructors
Constructor
new JwtClaimTenantExtractor(options): JwtClaimTenantExtractor;Defined in: src/extractors/jwt-claim.extractor.ts:41
Parameters
| Parameter | Type |
|---|---|
options | JwtClaimExtractorOptions |
Returns
Methods
extract()
extract(request): string | null;Defined in: src/extractors/jwt-claim.extractor.ts:46
Parameters
| Parameter | Type |
|---|---|
request | TenancyRequest |
Returns
string | null
Implementation of
KafkaTenantPropagator
Defined in: src/propagation/kafka-tenant-propagator.ts:40
Kafka tenant propagator.
Implements both TenantContextCarrier<KafkaMessageLike> (for inject/extract) and TenantPropagator (for getHeaders compatibility).
Handles Kafka headers that may be string or Buffer on extraction. No runtime dependency on kafkajs — uses structural types.
Example
const propagator = new KafkaTenantPropagator(new TenancyContext());
// Producer: inject tenant into message
await producer.send({
topic: 'orders',
messages: [propagator.inject({ value: JSON.stringify(payload) })],
});
// Consumer: extract tenant from message
const tenantId = propagator.extract(message);Implements
Constructors
Constructor
new KafkaTenantPropagator(context, options?): KafkaTenantPropagator;Defined in: src/propagation/kafka-tenant-propagator.ts:45
Parameters
| Parameter | Type |
|---|---|
context | TenancyContext |
options? | KafkaPropagationOptions |
Returns
Methods
extract()
extract(message): string | null;Defined in: src/propagation/kafka-tenant-propagator.ts:61
Extracts the tenant ID from an incoming carrier. Returns the tenant ID string, or null if not present.
Parameters
| Parameter | Type |
|---|---|
message | KafkaMessageLike |
Returns
string | null
Implementation of
getHeaders()
getHeaders(): Record<string, string>;Defined in: src/propagation/kafka-tenant-propagator.ts:71
Returns headers to propagate tenant context. Returns an empty object if no tenant context is available.
Returns
Record<string, string>
Implementation of
inject()
inject(message): KafkaMessageLike;Defined in: src/propagation/kafka-tenant-propagator.ts:52
Attaches the current tenant ID to the carrier for outbound propagation. Returns the carrier with tenant context included. If no tenant context is available, returns the carrier unchanged.
Parameters
| Parameter | Type |
|---|---|
message | KafkaMessageLike |
Returns
Implementation of
PathTenantExtractor
Defined in: src/extractors/path.extractor.ts:13
Contract for extracting a tenant ID from an inbound HTTP request.
Return the tenant ID string when present, or null when the request does not carry tenant information. A missing tenant is not an error condition; TenantMiddleware will call onTenantNotFound and let the application decide whether to continue, respond, or throw.
Implementations may return synchronously or return a Promise for async lookups. Throw only for malformed input or policy failures that should reject the request immediately.
Implements
Constructors
Constructor
new PathTenantExtractor(options): PathTenantExtractor;Defined in: src/extractors/path.extractor.ts:17
Parameters
| Parameter | Type |
|---|---|
options | PathExtractorOptions |
Returns
Methods
extract()
extract(request): string | null;Defined in: src/extractors/path.extractor.ts:29
Parameters
| Parameter | Type |
|---|---|
request | TenancyRequest |
Returns
string | null
Implementation of
SubdomainTenantExtractor
Defined in: src/extractors/subdomain.extractor.ts:30
Contract for extracting a tenant ID from an inbound HTTP request.
Return the tenant ID string when present, or null when the request does not carry tenant information. A missing tenant is not an error condition; TenantMiddleware will call onTenantNotFound and let the application decide whether to continue, respond, or throw.
Implementations may return synchronously or return a Promise for async lookups. Throw only for malformed input or policy failures that should reject the request immediately.
Implements
Constructors
Constructor
new SubdomainTenantExtractor(options?): SubdomainTenantExtractor;Defined in: src/extractors/subdomain.extractor.ts:34
Parameters
| Parameter | Type |
|---|---|
options? | SubdomainExtractorOptions |
Returns
Methods
extract()
extract(request): string | null;Defined in: src/extractors/subdomain.extractor.ts:41
Parameters
| Parameter | Type |
|---|---|
request | TenancyRequest |
Returns
string | null
Implementation of
TenancyContext
Defined in: src/services/tenancy-context.ts:9
Constructors
Constructor
new TenancyContext(): TenancyContext;Returns
Methods
getCurrentTenantId()
static getCurrentTenantId(): string | null;Defined in: src/services/tenancy-context.ts:12
Returns
string | null
getTenantId()
getTenantId(): string | null;Defined in: src/services/tenancy-context.ts:22
Returns
string | null
isBypassed()
isBypassed(): boolean;Defined in: src/services/tenancy-context.ts:26
Returns
boolean
run()
Call Signature
run<T>(tenantId, callback): Promise<T>;Defined in: src/services/tenancy-context.ts:16
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type |
|---|---|
tenantId | string |
callback | () => Promise<T> |
Returns
Promise<T>
Call Signature
run<T>(tenantId, callback): T;Defined in: src/services/tenancy-context.ts:17
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type |
|---|---|
tenantId | string |
callback | () => T |
Returns
T
runWithoutTenant()
Call Signature
runWithoutTenant<T>(callback): Promise<T>;Defined in: src/services/tenancy-context.ts:30
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type |
|---|---|
callback | () => Promise<T> |
Returns
Promise<T>
Call Signature
runWithoutTenant<T>(callback): T;Defined in: src/services/tenancy-context.ts:31
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type |
|---|---|
callback | () => T |
Returns
T
TenancyContextRequiredError
Defined in: src/errors/tenancy-context-required.error.ts:3
Extends
Constructors
Constructor
new TenancyContextRequiredError(model, operation): TenancyContextRequiredError;Defined in: src/errors/tenancy-context-required.error.ts:6
Parameters
| Parameter | Type |
|---|---|
model | string |
operation | string |
Returns
Overrides
TenantContextMissingError.constructor
Properties
message
message: string;Defined in: node_modules/typescript/lib/lib.es5.d.ts:1077
Inherited from
TenantContextMissingError.message
model
readonly model: string;Defined in: src/errors/tenancy-context-required.error.ts:7
name
name: string = 'TenancyContextRequiredError';Defined in: src/errors/tenancy-context-required.error.ts:4
Overrides
TenantContextMissingError.name
operation
readonly operation: string;Defined in: src/errors/tenancy-context-required.error.ts:8
stack?
optional stack?: string;Defined in: node_modules/typescript/lib/lib.es5.d.ts:1078
Inherited from
TenantContextMissingError.stack
stackTraceLimit
static stackTraceLimit: number;Defined in: node_modules/@types/node/globals.d.ts:67
The Error.stackTraceLimit property specifies the number of stack frames collected by a stack trace (whether generated by new Error().stack or Error.captureStackTrace(obj)).
The default value is 10 but may be set to any valid JavaScript number. Changes will affect any stack trace captured after the value has been changed.
If set to a non-number value, or set to a negative number, stack traces will not capture any frames.
Inherited from
TenantContextMissingError.stackTraceLimit
Methods
captureStackTrace()
static captureStackTrace(targetObject, constructorOpt?): void;Defined in: node_modules/@types/node/globals.d.ts:51
Creates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called.
const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack; // Similar to `new Error().stack`The first line of the trace will be prefixed with ${myObject.name}: ${myObject.message}.
The optional constructorOpt argument accepts a function. If given, all frames above constructorOpt, including constructorOpt, will be omitted from the generated stack trace.
The constructorOpt argument is useful for hiding implementation details of error generation from the user. For instance:
function a() {
b();
}
function b() {
c();
}
function c() {
// Create an error without stack trace to avoid calculating the stack trace twice.
const { stackTraceLimit } = Error;
Error.stackTraceLimit = 0;
const error = new Error();
Error.stackTraceLimit = stackTraceLimit;
// Capture the stack trace above function b
Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
throw error;
}
a();Parameters
| Parameter | Type |
|---|---|
targetObject | object |
constructorOpt? | Function |
Returns
void
Inherited from
TenantContextMissingError.captureStackTrace
prepareStackTrace()
static prepareStackTrace(err, stackTraces): any;Defined in: node_modules/@types/node/globals.d.ts:55
Parameters
| Parameter | Type |
|---|---|
err | Error |
stackTraces | CallSite[] |
Returns
any
See
https://v8.dev/docs/stack-trace-api#customizing-stack-traces
Inherited from
TenantContextMissingError.prepareStackTrace
toJSON()
toJSON(): {
message: string;
model: string;
name: string;
operation: string;
};Defined in: src/errors/tenancy-context-required.error.ts:17
Returns
{
message: string;
model: string;
name: string;
operation: string;
}| Name | Type | Defined in |
|---|---|---|
message | string | src/errors/tenancy-context-required.error.ts:20 |
model | string | src/errors/tenancy-context-required.error.ts:21 |
name | string | src/errors/tenancy-context-required.error.ts:19 |
operation | string | src/errors/tenancy-context-required.error.ts:22 |
TenancyEventService
Defined in: src/events/tenancy-event.service.ts:14
Optional event emission service that integrates with @nestjs/event-emitter.
If @nestjs/event-emitter is installed and EventEmitterModule.forRoot() is imported, events are emitted via EventEmitter2. If not installed, all emit() calls are silently ignored.
Implements
OnModuleInit
Constructors
Constructor
new TenancyEventService(moduleRef): TenancyEventService;Defined in: src/events/tenancy-event.service.ts:18
Parameters
| Parameter | Type |
|---|---|
moduleRef | ModuleRef |
Returns
Methods
emit()
emit<K>(event, payload): void;Defined in: src/events/tenancy-event.service.ts:31
Type Parameters
| Type Parameter |
|---|
K extends keyof TenancyEventMap |
Parameters
| Parameter | Type |
|---|---|
event | K |
payload | TenancyEventMap[K] |
Returns
void
onModuleInit()
onModuleInit(): Promise<void>;Defined in: src/events/tenancy-event.service.ts:20
Returns
Promise<void>
Implementation of
OnModuleInit.onModuleInitTenancyModule
Defined in: src/tenancy.module.ts:49
Implements
NestModule
Constructors
Constructor
new TenancyModule(): TenancyModule;Returns
Methods
configure()
configure(consumer): void;Defined in: src/tenancy.module.ts:50
Parameters
| Parameter | Type |
|---|---|
consumer | MiddlewareConsumer |
Returns
void
Implementation of
NestModule.configureforRoot()
static forRoot(options): DynamicModule;Defined in: src/tenancy.module.ts:59
Parameters
| Parameter | Type |
|---|---|
options | TenancyModuleOptions |
Returns
DynamicModule
forRootAsync()
static forRootAsync(options): DynamicModule;Defined in: src/tenancy.module.ts:65
Parameters
| Parameter | Type |
|---|---|
options | TenancyModuleAsyncOptions |
Returns
DynamicModule
TenancyService
Defined in: src/services/tenancy.service.ts:8
Constructors
Constructor
new TenancyService(context, eventService?): TenancyService;Defined in: src/services/tenancy.service.ts:9
Parameters
| Parameter | Type |
|---|---|
context | TenancyContext |
eventService? | TenancyEventService |
Returns
Methods
getCurrentTenant()
getCurrentTenant(): string | null;Defined in: src/services/tenancy.service.ts:14
Returns
string | null
getCurrentTenantOrThrow()
getCurrentTenantOrThrow(): string;Defined in: src/services/tenancy.service.ts:18
Returns
string
isTenantBypassed()
isTenantBypassed(): boolean;Defined in: src/services/tenancy.service.ts:26
Returns
boolean
withoutTenant()
withoutTenant<T>(callback): Promise<T>;Defined in: src/services/tenancy.service.ts:30
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type |
|---|---|
callback | () => T | Promise<T> |
Returns
Promise<T>
TenancyTelemetryService
Defined in: src/telemetry/tenancy-telemetry.service.ts:17
Optional OpenTelemetry integration service.
If @opentelemetry/api is installed, automatically adds the tenant ID as a span attribute to the current active span. Optionally creates custom spans for tenant lifecycle events.
If @opentelemetry/api is not installed, all methods are silently no-ops. Follows the same graceful degradation pattern as TenancyEventService.
Implements
OnModuleInit
Constructors
Constructor
new TenancyTelemetryService(options): TenancyTelemetryService;Defined in: src/telemetry/tenancy-telemetry.service.ts:24
Parameters
| Parameter | Type |
|---|---|
options | TenancyModuleOptions |
Returns
Methods
endSpan()
endSpan(span): void;Defined in: src/telemetry/tenancy-telemetry.service.ts:95
Safely end a span (null-safe).
Parameters
| Parameter | Type |
|---|---|
span | Pick<Span, "end"> | null |
Returns
void
onModuleInit()
onModuleInit(): Promise<void>;Defined in: src/telemetry/tenancy-telemetry.service.ts:32
Returns
Promise<void>
Implementation of
OnModuleInit.onModuleInitsetTenantAttribute()
setTenantAttribute(tenantId): void;Defined in: src/telemetry/tenancy-telemetry.service.ts:44
Add tenant.id attribute to the current active span.
Parameters
| Parameter | Type |
|---|---|
tenantId | string |
Returns
void
startSpan()
startSpan(name, attributes?): Span | null;Defined in: src/telemetry/tenancy-telemetry.service.ts:51
Start a custom span (only when createSpans is true). Returns null if disabled or OTel unavailable.
Parameters
| Parameter | Type |
|---|---|
name | string |
attributes? | Attributes |
Returns
Span | null
startTenantSpan()
startTenantSpan(name, tenantId): Span | null;Defined in: src/telemetry/tenancy-telemetry.service.ts:57
Start a custom span with the configured tenant ID attribute attached.
Parameters
| Parameter | Type |
|---|---|
name | string |
tenantId | string |
Returns
Span | null
withSpan()
withSpan<T>(
name,
attributes,
callback): T;Defined in: src/telemetry/tenancy-telemetry.service.ts:62
Run a callback with a custom span set as the active OpenTelemetry span.
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type |
|---|---|
name | string |
attributes | Attributes | undefined |
callback | (span) => T |
Returns
T
withTenantSpan()
withTenantSpan<T>(
name,
tenantId,
callback): T;Defined in: src/telemetry/tenancy-telemetry.service.ts:86
Run a callback with a tenant lifecycle span set as active.
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type |
|---|---|
name | string |
tenantId | string |
callback | (span) => T |
Returns
T
TenantContextInterceptor
Defined in: src/propagation/tenant-context.interceptor.ts:51
NestJS interceptor that restores tenant context from incoming microservice messages.
Designed for RPC transports only (Kafka, Bull, gRPC). HTTP requests are skipped because TenantMiddleware + TenancyGuard already handle HTTP tenant extraction as part of TenancyModule.
Wraps the handler execution inside TenancyContext.run(), ensuring that all downstream code (services, Prisma extension, etc.) has access to the tenant context through AsyncLocalStorage.
For best results, set the transport option explicitly to avoid duck-typing ambiguity when multiple RPC transports share similar context shapes.
Example
// Global interceptor for Kafka consumers
app.useGlobalInterceptors(
new TenantContextInterceptor(new TenancyContext(), { transport: 'kafka' }),
);
// Bull processor with explicit transport
@UseInterceptors(new TenantContextInterceptor(new TenancyContext(), { transport: 'bull' }))
@Controller()
export class OrderProcessor { ... }Implements
NestInterceptor
Constructors
Constructor
new TenantContextInterceptor(context, options?): TenantContextInterceptor;Defined in: src/propagation/tenant-context.interceptor.ts:57
Parameters
| Parameter | Type |
|---|---|
context | TenancyContext |
options? | TenantContextInterceptorOptions |
Returns
Methods
intercept()
intercept(executionContext, next): Observable<unknown>;Defined in: src/propagation/tenant-context.interceptor.ts:73
Method to implement a custom interceptor.
Parameters
| Parameter | Type | Description |
|---|---|---|
executionContext | ExecutionContext | - |
next | CallHandler | a reference to the CallHandler, which provides access to an Observable representing the response stream from the route handler. |
Returns
Observable<unknown>
Implementation of
NestInterceptor.interceptTenantContextMissingError
Defined in: src/errors/tenant-context-missing.error.ts:22
Extends
Error
Extended by
Constructors
Constructor
new TenantContextMissingError(message?): TenantContextMissingError;Defined in: src/errors/tenant-context-missing.error.ts:25
Parameters
| Parameter | Type |
|---|---|
message? | string |
Returns
Overrides
Error.constructorProperties
message
message: string;Defined in: node_modules/typescript/lib/lib.es5.d.ts:1077
Inherited from
Error.messagename
name: string = 'TenantContextMissingError';Defined in: src/errors/tenant-context-missing.error.ts:23
Overrides
Error.namestack?
optional stack?: string;Defined in: node_modules/typescript/lib/lib.es5.d.ts:1078
Inherited from
Error.stackstackTraceLimit
static stackTraceLimit: number;Defined in: node_modules/@types/node/globals.d.ts:67
The Error.stackTraceLimit property specifies the number of stack frames collected by a stack trace (whether generated by new Error().stack or Error.captureStackTrace(obj)).
The default value is 10 but may be set to any valid JavaScript number. Changes will affect any stack trace captured after the value has been changed.
If set to a non-number value, or set to a negative number, stack traces will not capture any frames.
Inherited from
Error.stackTraceLimitMethods
captureStackTrace()
static captureStackTrace(targetObject, constructorOpt?): void;Defined in: node_modules/@types/node/globals.d.ts:51
Creates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called.
const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack; // Similar to `new Error().stack`The first line of the trace will be prefixed with ${myObject.name}: ${myObject.message}.
The optional constructorOpt argument accepts a function. If given, all frames above constructorOpt, including constructorOpt, will be omitted from the generated stack trace.
The constructorOpt argument is useful for hiding implementation details of error generation from the user. For instance:
function a() {
b();
}
function b() {
c();
}
function c() {
// Create an error without stack trace to avoid calculating the stack trace twice.
const { stackTraceLimit } = Error;
Error.stackTraceLimit = 0;
const error = new Error();
Error.stackTraceLimit = stackTraceLimit;
// Capture the stack trace above function b
Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
throw error;
}
a();Parameters
| Parameter | Type |
|---|---|
targetObject | object |
constructorOpt? | Function |
Returns
void
Inherited from
Error.captureStackTraceprepareStackTrace()
static prepareStackTrace(err, stackTraces): any;Defined in: node_modules/@types/node/globals.d.ts:55
Parameters
| Parameter | Type |
|---|---|
err | Error |
stackTraces | CallSite[] |
Returns
any
See
https://v8.dev/docs/stack-trace-api#customizing-stack-traces
Inherited from
Error.prepareStackTraceInterfaces
BullPropagationOptions
Defined in: src/propagation/bull-tenant-propagator.ts:5
Properties
dataKey?
optional dataKey?: string;Defined in: src/propagation/bull-tenant-propagator.ts:7
Key name used to store tenant ID in job data. Defaults to '__tenantId'.
GrpcMetadataLike
Defined in: src/propagation/grpc-tenant-propagator.ts:15
Structural type for gRPC Metadata — no dependency on @grpc/grpc-js.
Matches the subset of @grpc/grpc-js Metadata used for tenant propagation.
Methods
get()
get(key): (string | Buffer<ArrayBufferLike>)[];Defined in: src/propagation/grpc-tenant-propagator.ts:17
Parameters
| Parameter | Type |
|---|---|
key | string |
Returns
(string | Buffer<ArrayBufferLike>)[]
set()
set(key, value): void;Defined in: src/propagation/grpc-tenant-propagator.ts:16
Parameters
| Parameter | Type |
|---|---|
key | string |
value | string |
Returns
void
GrpcPropagationOptions
Defined in: src/propagation/grpc-tenant-propagator.ts:5
Properties
metadataKey?
optional metadataKey?: string;Defined in: src/propagation/grpc-tenant-propagator.ts:7
Metadata key for tenant ID. Defaults to 'x-tenant-id' (lowercase per gRPC convention).
HttpPropagationOptions
Defined in: src/propagation/http-tenant-propagator.ts:5
Properties
headerName?
optional headerName?: string;Defined in: src/propagation/http-tenant-propagator.ts:7
Header name for tenant ID propagation. Defaults to 'X-Tenant-Id'.
JwtClaimExtractorOptions
Defined in: src/extractors/jwt-claim.extractor.ts:4
Properties
claimKey
claimKey: string;Defined in: src/extractors/jwt-claim.extractor.ts:5
headerName?
optional headerName?: string;Defined in: src/extractors/jwt-claim.extractor.ts:6
KafkaMessageLike
Defined in: src/propagation/kafka-tenant-propagator.ts:12
Structural type for Kafka message — no dependency on kafkajs.
Indexable
[key: string]: unknownProperties
headers?
optional headers?: Record<string, string | Buffer<ArrayBufferLike> | undefined>;Defined in: src/propagation/kafka-tenant-propagator.ts:13
KafkaPropagationOptions
Defined in: src/propagation/kafka-tenant-propagator.ts:6
Properties
headerName?
optional headerName?: string;Defined in: src/propagation/kafka-tenant-propagator.ts:8
Header name for tenant ID in Kafka message headers. Defaults to 'X-Tenant-Id'.
PathExtractorOptions
Defined in: src/extractors/path.extractor.ts:4
Properties
paramName
paramName: string;Defined in: src/extractors/path.extractor.ts:6
pattern
pattern: string;Defined in: src/extractors/path.extractor.ts:5
PrismaTenancyExtensionOptions
Defined in: src/prisma/prisma-tenancy.extension.ts:31
Properties
autoInjectTenantId?
optional autoInjectTenantId?: boolean;Defined in: src/prisma/prisma-tenancy.extension.ts:33
dbSettingKey?
optional dbSettingKey?: string;Defined in: src/prisma/prisma-tenancy.extension.ts:32
failClosed?
optional failClosed?: boolean;Defined in: src/prisma/prisma-tenancy.extension.ts:44
When true, throws TenancyContextRequiredError if a query is executed without a tenant context (unless the model is in sharedModels or withoutTenant() was used to explicitly bypass).
Prevents accidental data exposure when RLS policies are misconfigured.
Default
trueinteractiveTransactionSupport?
optional interactiveTransactionSupport?: boolean;Defined in: src/prisma/prisma-tenancy.extension.ts:60
Enable transparent interactive transaction support.
When enabled, the extension detects interactive transactions ($transaction(async (tx) => ...)) and sets the RLS context on the transaction's connection directly.
Relies on Prisma internal APIs (__internalParams, _createItxClient). Compatibility is validated at extension creation time — an error is thrown immediately if the current Prisma version does not support this feature.
For an alternative that uses only public Prisma APIs, see tenancyTransaction().
Default
falsesharedModels?
optional sharedModels?: string[];Defined in: src/prisma/prisma-tenancy.extension.ts:35
tenantIdField?
optional tenantIdField?: string;Defined in: src/prisma/prisma-tenancy.extension.ts:34
PrismaTransactionClient
Defined in: src/prisma/tenancy-transaction.ts:18
Structural type representing a Prisma-like client that supports interactive transactions. PrismaClient satisfies this automatically.
Type Parameters
| Type Parameter | Default type |
|---|---|
TTx extends PrismaTransactionContext | any |
Methods
$transaction()
$transaction<T>(fn, options?): Promise<T>;Defined in: src/prisma/tenancy-transaction.ts:19
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type |
|---|---|
fn | (tx) => Promise<T> |
options? | Record<string, unknown> |
Returns
Promise<T>
PrismaTransactionContext
Defined in: src/prisma/tenancy-transaction.ts:7
Minimal transaction client shape required by tenancyTransaction.
Methods
$executeRaw()
$executeRaw(strings, ...values): Promise<unknown>;Defined in: src/prisma/tenancy-transaction.ts:8
Parameters
| Parameter | Type |
|---|---|
strings | TemplateStringsArray |
...values | unknown[] |
Returns
Promise<unknown>
SubdomainExtractorOptions
Defined in: src/extractors/subdomain.extractor.ts:4
Properties
excludeSubdomains?
optional excludeSubdomains?: string[];Defined in: src/extractors/subdomain.extractor.ts:5
TelemetryOptions
Defined in: src/interfaces/tenancy-module-options.interface.ts:6
Properties
createSpans?
optional createSpans?: boolean;Defined in: src/interfaces/tenancy-module-options.interface.ts:10
Create custom spans for tenant lifecycle events (resolved, not_found, etc.).
Default
falsespanAttributeKey?
optional spanAttributeKey?: string;Defined in: src/interfaces/tenancy-module-options.interface.ts:8
Span attribute key for tenant ID.
Default
'tenant.id'TenancyEventMap
Defined in: src/events/tenancy-events.ts:89
Type-safe mapping from event name to payload type. Used by TenancyEventService.emit() to enforce correct payloads at compile time.
Properties
tenant.context_bypassed
tenant.context_bypassed: TenantContextBypassedEvent;Defined in: src/events/tenancy-events.ts:94
tenant.cross_check_failed
tenant.cross_check_failed: TenantCrossCheckFailedEvent;Defined in: src/events/tenancy-events.ts:95
tenant.extraction_failed
tenant.extraction_failed: TenantExtractionFailedEvent;Defined in: src/events/tenancy-events.ts:92
tenant.not_found
tenant.not_found: TenancyEventRequestPayload;Defined in: src/events/tenancy-events.ts:91
tenant.resolved
tenant.resolved: TenantResolvedEvent;Defined in: src/events/tenancy-events.ts:90
tenant.validation_failed
tenant.validation_failed: TenantValidationFailedEvent;Defined in: src/events/tenancy-events.ts:93
TenancyEventRequestSummary
Defined in: src/events/tenancy-events.ts:3
Properties
host?
optional host?: string;Defined in: src/events/tenancy-events.ts:8
ip?
optional ip?: string;Defined in: src/events/tenancy-events.ts:6
method?
optional method?: string;Defined in: src/events/tenancy-events.ts:4
path?
optional path?: string;Defined in: src/events/tenancy-events.ts:5
userAgent?
optional userAgent?: string;Defined in: src/events/tenancy-events.ts:7
TenancyModuleAsyncOptions
Defined in: src/interfaces/tenancy-module-options.interface.ts:97
Extends
Pick<ModuleMetadata,"imports">
Properties
imports?
optional imports?: (
| DynamicModule
| Type<any>
| Promise<DynamicModule>
| ForwardReference<any>)[];Defined in: node_modules/@nestjs/common/interfaces/modules/module-metadata.interface.d.ts:18
Optional list of imported modules that export the providers which are required in this module.
Inherited from
Pick.importsinject?
optional inject?: (InjectionToken | OptionalFactoryDependency)[];Defined in: src/interfaces/tenancy-module-options.interface.ts:99
useClass?
optional useClass?: Type<TenancyModuleOptionsFactory>;Defined in: src/interfaces/tenancy-module-options.interface.ts:103
useExisting?
optional useExisting?: Type<TenancyModuleOptionsFactory>;Defined in: src/interfaces/tenancy-module-options.interface.ts:104
useFactory?
optional useFactory?: (...args) =>
| TenancyModuleOptions
| Promise<TenancyModuleOptions>;Defined in: src/interfaces/tenancy-module-options.interface.ts:100
Parameters
| Parameter | Type |
|---|---|
...args | any[] |
Returns
| TenancyModuleOptions | Promise<TenancyModuleOptions>
TenancyModuleOptions
Defined in: src/interfaces/tenancy-module-options.interface.ts:13
Properties
crossCheck?
optional crossCheck?: {
extractor: TenantExtractor;
onFailed?: "reject" | "log";
required?: boolean;
};Defined in: src/interfaces/tenancy-module-options.interface.ts:66
Cross-check configuration for tenant ID forgery prevention.
Compares the primary extractor result with a secondary source. Common pattern: primary = header, cross-check = JWT claim.
If the cross-check extractor returns null (e.g., no JWT present), validation is skipped — allowing unauthenticated endpoints to work normally. Set required: true to reject requests when the cross-check extractor returns null, enforcing that every request must have a verifiable secondary source.
extractor
extractor: TenantExtractor;Secondary extractor to validate the tenant ID against.
onFailed?
optional onFailed?: "reject" | "log";Behavior on mismatch.
'reject'(default): throws ForbiddenException'log': logs a warning and continues with the primary extractor's value
required?
optional required?: boolean;When true, the cross-check extractor must return a non-null value. Throws ForbiddenException if the extractor returns null. Use this for endpoints that require authenticated cross-validation.
Default
falsedbSettingKey?
optional dbSettingKey?: string;Defined in: src/interfaces/tenancy-module-options.interface.ts:28
onTenantNotFound?
optional onTenantNotFound?: (request, response) => void | "skip" | Promise<void | "skip">;Defined in: src/interfaces/tenancy-module-options.interface.ts:53
Called when no tenant ID could be extracted from the request.
Behavior based on return value:
void/undefined: request continues to the next middleware (observation-only hook)'skip': request continues butnext()is NOT called. Warning: You must send a response (e.g.,response.status(403).end()) or throw an exception before returning'skip'. Otherwise the HTTP request will hang indefinitely with no response sent to the client.
Throwing an exception (e.g., throw new ForbiddenException()) always aborts the request regardless of return value.
Parameters
| Parameter | Type |
|---|---|
request | TenancyRequest |
response | TenancyResponse |
Returns
void | "skip" | Promise<void | "skip">
onTenantResolved?
optional onTenantResolved?: (tenantId, request) => void | Promise<void>;Defined in: src/interfaces/tenancy-module-options.interface.ts:38
Called after a tenant ID is successfully extracted and validated. Runs inside TenancyContext.run(), so getCurrentTenant() is available.
Throwing an exception aborts the request — NestJS handles it as a 500 (or whatever your exception filter maps it to). The telemetry span is always closed via finally, so throwing is safe for audit/authorization checks.
Parameters
| Parameter | Type |
|---|---|
tenantId | string |
request | TenancyRequest |
Returns
void | Promise<void>
telemetry?
optional telemetry?: TelemetryOptions;Defined in: src/interfaces/tenancy-module-options.interface.ts:88
OpenTelemetry integration. Automatically adds tenant.id to active spans. Silently ignored if @opentelemetry/api is not installed.
tenantExtractor
tenantExtractor: string | TenantExtractor;Defined in: src/interfaces/tenancy-module-options.interface.ts:27
Tenant extraction strategy.
A string is a shortcut for HeaderTenantExtractor and is interpreted as the HTTP header name. Use a TenantExtractor instance for non-header strategies such as subdomain, path, JWT claim, or composite extraction.
Example
tenantExtractor: 'X-Tenant-Id'
tenantExtractor: new SubdomainTenantExtractor()validateTenantId?
optional validateTenantId?: (tenantId) => boolean | Promise<boolean>;Defined in: src/interfaces/tenancy-module-options.interface.ts:29
Parameters
| Parameter | Type |
|---|---|
tenantId | string |
Returns
boolean | Promise<boolean>
TenancyModuleOptionsFactory
Defined in: src/interfaces/tenancy-module-options.interface.ts:91
Methods
createTenancyOptions()
createTenancyOptions():
| TenancyModuleOptions
| Promise<TenancyModuleOptions>;Defined in: src/interfaces/tenancy-module-options.interface.ts:92
Returns
| TenancyModuleOptions | Promise<TenancyModuleOptions>
TenancyRequest
Defined in: src/interfaces/tenancy-request.interface.ts:9
Minimal HTTP request interface for @nestarc/tenancy public API.
This is intentionally framework-agnostic. Express Request, Fastify FastifyRequest, and Node.js http.IncomingMessage all satisfy this interface. Use type assertion if you need platform-specific properties (e.g., request as import('express').Request).
Indexable
[key: string]: unknownIndex signature for platform-specific properties. Use type assertion to access.
Properties
headers
headers: Record<string, string | string[] | undefined>;Defined in: src/interfaces/tenancy-request.interface.ts:11
HTTP request headers. Keys are lowercase in Node.js.
hostname?
optional hostname?: string;Defined in: src/interfaces/tenancy-request.interface.ts:13
Hostname derived from the Host header.
path?
optional path?: string;Defined in: src/interfaces/tenancy-request.interface.ts:15
Request path without query string.
url?
optional url?: string;Defined in: src/interfaces/tenancy-request.interface.ts:17
Full request URL.
TenancyResponse
Defined in: src/interfaces/tenancy-request.interface.ts:32
Minimal HTTP response interface for @nestarc/tenancy public API.
Used only in onTenantNotFound callback. Framework-agnostic — both Express Response and Fastify FastifyReply satisfy this interface.
The named methods are optional to maintain compatibility with any response-like object. If you need the full response API, use type assertion: (response as import('express').Response).
Indexable
[key: string]: unknownIndex signature for platform-specific properties. Use type assertion to access.
Methods
end()?
optional end(): void;Defined in: src/interfaces/tenancy-request.interface.ts:38
End the response without a body.
Returns
void
json()?
optional json(body): void;Defined in: src/interfaces/tenancy-request.interface.ts:36
Send JSON response body.
Parameters
| Parameter | Type |
|---|---|
body | unknown |
Returns
void
status()?
optional status(code): this;Defined in: src/interfaces/tenancy-request.interface.ts:34
Set HTTP status code. Returns this for chaining (Express/Fastify convention).
Parameters
| Parameter | Type |
|---|---|
code | number |
Returns
this
TenancyTransactionOptions
Defined in: src/prisma/tenancy-transaction.ts:25
Properties
dbSettingKey?
optional dbSettingKey?: string;Defined in: src/prisma/tenancy-transaction.ts:29
isolationLevel?
optional isolationLevel?: "ReadUncommitted" | "ReadCommitted" | "RepeatableRead" | "Serializable";Defined in: src/prisma/tenancy-transaction.ts:28
PostgreSQL transaction isolation level.
timeout?
optional timeout?: number;Defined in: src/prisma/tenancy-transaction.ts:26
TenantContextBypassedEvent
Defined in: src/events/tenancy-events.ts:44
Properties
previousTenantId?
optional previousTenantId?: string | null;Defined in: src/events/tenancy-events.ts:46
reason
reason: "decorator" | "withoutTenant";Defined in: src/events/tenancy-events.ts:45
requestSummary?
optional requestSummary?: TenancyEventRequestSummary;Defined in: src/events/tenancy-events.ts:47
TenantContextCarrier
Defined in: src/interfaces/tenant-context-carrier.interface.ts:14
Transport-agnostic contract for propagating tenant context across service boundaries.
Unlike TenantPropagator (HTTP-specific, returns Record<string, string>), this interface supports any carrier type: Bull job data, Kafka messages, gRPC metadata, or custom transports.
Follows the OpenTelemetry inject/extract pattern:
inject: attaches the current tenant ID to an outgoing carrierextract: reads a tenant ID from an incoming carrier
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
TCarrier | unknown | The transport-specific data structure (e.g., job data object, Kafka message, gRPC Metadata) |
Methods
extract()
extract(carrier): string | null;Defined in: src/interfaces/tenant-context-carrier.interface.ts:26
Extracts the tenant ID from an incoming carrier. Returns the tenant ID string, or null if not present.
Parameters
| Parameter | Type |
|---|---|
carrier | TCarrier |
Returns
string | null
inject()
inject(carrier): TCarrier;Defined in: src/interfaces/tenant-context-carrier.interface.ts:20
Attaches the current tenant ID to the carrier for outbound propagation. Returns the carrier with tenant context included. If no tenant context is available, returns the carrier unchanged.
Parameters
| Parameter | Type |
|---|---|
carrier | TCarrier |
Returns
TCarrier
TenantCrossCheckFailedEvent
Defined in: src/events/tenancy-events.ts:50
Extends
TenancyEventRequestPayload
Properties
crossCheckTenantId
crossCheckTenantId: string;Defined in: src/events/tenancy-events.ts:52
extractedTenantId
extractedTenantId: string;Defined in: src/events/tenancy-events.ts:51
request?
optional request?: TenancyRequest;Defined in: src/events/tenancy-events.ts:26
Deprecated
Use requestSummary instead. Raw request objects may contain credentials, cookies, body data, and framework-specific references.
Inherited from
TenancyEventRequestPayload.requestrequestSummary?
optional requestSummary?: TenancyEventRequestSummary;Defined in: src/events/tenancy-events.ts:21
Inherited from
TenancyEventRequestPayload.requestSummaryTenantExtractionFailedEvent
Defined in: src/events/tenancy-events.ts:35
Extends
TenancyEventRequestPayload
Properties
errorMessage
errorMessage: string;Defined in: src/events/tenancy-events.ts:37
errorName
errorName: string;Defined in: src/events/tenancy-events.ts:36
request?
optional request?: TenancyRequest;Defined in: src/events/tenancy-events.ts:26
Deprecated
Use requestSummary instead. Raw request objects may contain credentials, cookies, body data, and framework-specific references.
Inherited from
TenancyEventRequestPayload.requestrequestSummary?
optional requestSummary?: TenancyEventRequestSummary;Defined in: src/events/tenancy-events.ts:21
Inherited from
TenancyEventRequestPayload.requestSummaryTenantExtractor
Defined in: src/interfaces/tenant-extractor.interface.ts:15
Contract for extracting a tenant ID from an inbound HTTP request.
Return the tenant ID string when present, or null when the request does not carry tenant information. A missing tenant is not an error condition; TenantMiddleware will call onTenantNotFound and let the application decide whether to continue, respond, or throw.
Implementations may return synchronously or return a Promise for async lookups. Throw only for malformed input or policy failures that should reject the request immediately.
Methods
extract()
extract(request): string | Promise<string | null> | null;Defined in: src/interfaces/tenant-extractor.interface.ts:16
Parameters
| Parameter | Type |
|---|---|
request | TenancyRequest |
Returns
string | Promise<string | null> | null
TenantPropagator
Defined in: src/interfaces/tenant-propagator.interface.ts:8
Contract for propagating tenant context to outgoing requests.
Implementations transform the current tenant ID into transport-specific headers or metadata. Used by HttpTenantPropagator for HTTP and KafkaTenantPropagator for Kafka. For Bull and gRPC, see TenantContextCarrier.
Methods
getHeaders()
getHeaders(): Record<string, string>;Defined in: src/interfaces/tenant-propagator.interface.ts:13
Returns headers to propagate tenant context. Returns an empty object if no tenant context is available.
Returns
Record<string, string>
TenantResolvedEvent
Defined in: src/events/tenancy-events.ts:29
Extends
TenancyEventRequestPayload
Properties
request?
optional request?: TenancyRequest;Defined in: src/events/tenancy-events.ts:26
Deprecated
Use requestSummary instead. Raw request objects may contain credentials, cookies, body data, and framework-specific references.
Inherited from
TenancyEventRequestPayload.requestrequestSummary?
optional requestSummary?: TenancyEventRequestSummary;Defined in: src/events/tenancy-events.ts:21
Inherited from
TenancyEventRequestPayload.requestSummarytenantId
tenantId: string;Defined in: src/events/tenancy-events.ts:30
TenantValidationFailedEvent
Defined in: src/events/tenancy-events.ts:40
Extends
TenancyEventRequestPayload
Properties
request?
optional request?: TenancyRequest;Defined in: src/events/tenancy-events.ts:26
Deprecated
Use requestSummary instead. Raw request objects may contain credentials, cookies, body data, and framework-specific references.
Inherited from
TenancyEventRequestPayload.requestrequestSummary?
optional requestSummary?: TenancyEventRequestSummary;Defined in: src/events/tenancy-events.ts:21
Inherited from
TenancyEventRequestPayload.requestSummarytenantId
tenantId: string;Defined in: src/events/tenancy-events.ts:41
Type Aliases
TenantContextInterceptorOptions
type TenantContextInterceptorOptions =
| {
kafkaHeaderName?: string;
transport: "kafka";
}
| {
bullDataKey?: string;
transport: "bull";
}
| {
grpcMetadataKey?: string;
transport: "grpc";
}
| {
bullDataKey?: string;
grpcMetadataKey?: string;
kafkaHeaderName?: string;
transport?: undefined;
};Defined in: src/propagation/tenant-context.interceptor.ts:17
Options for TenantContextInterceptor.
When transport is specified, only the matching transport key is accepted. When transport is omitted, all keys are available for duck-typing fallback.
TenantNotFoundEvent
type TenantNotFoundEvent = TenancyEventRequestPayload;Defined in: src/events/tenancy-events.ts:33
Variables
CurrentTenant
const CurrentTenant: (...dataOrPipes) => ParameterDecorator;Defined in: src/decorators/current-tenant.decorator.ts:4
Parameters
| Parameter | Type |
|---|---|
...dataOrPipes | unknown[] |
Returns
ParameterDecorator
TENANCY_MODULE_OPTIONS
const TENANCY_MODULE_OPTIONS: typeof TENANCY_MODULE_OPTIONS;Defined in: src/tenancy.constants.ts:2
TenancyEvents
const TenancyEvents: {
CONTEXT_BYPASSED: "tenant.context_bypassed";
CROSS_CHECK_FAILED: "tenant.cross_check_failed";
EXTRACTION_FAILED: "tenant.extraction_failed";
NOT_FOUND: "tenant.not_found";
RESOLVED: "tenant.resolved";
VALIDATION_FAILED: "tenant.validation_failed";
};Defined in: src/events/tenancy-events.ts:11
Type Declaration
| Name | Type | Default value | Defined in |
|---|---|---|---|
CONTEXT_BYPASSED | "tenant.context_bypassed" | 'tenant.context_bypassed' | src/events/tenancy-events.ts:16 |
CROSS_CHECK_FAILED | "tenant.cross_check_failed" | 'tenant.cross_check_failed' | src/events/tenancy-events.ts:17 |
EXTRACTION_FAILED | "tenant.extraction_failed" | 'tenant.extraction_failed' | src/events/tenancy-events.ts:14 |
NOT_FOUND | "tenant.not_found" | 'tenant.not_found' | src/events/tenancy-events.ts:13 |
RESOLVED | "tenant.resolved" | 'tenant.resolved' | src/events/tenancy-events.ts:12 |
VALIDATION_FAILED | "tenant.validation_failed" | 'tenant.validation_failed' | src/events/tenancy-events.ts:15 |
Functions
BypassTenancy()
function BypassTenancy(): CustomDecorator<typeof BYPASS_TENANCY_KEY>;Defined in: src/decorators/bypass-tenancy.decorator.ts:14
Marks a route or controller to skip TenancyGuard's tenant-required check.
Important: This only bypasses the guard — it does NOT clear the tenant context. If the request contains a tenant header, TenantMiddleware still sets the context, so getCurrentTenant() may return a value and Prisma queries will still be RLS-filtered.
Use this for endpoints that should work with or without a tenant (e.g., health checks, public APIs). If you need to explicitly run without tenant context, use withoutTenant().
Returns
CustomDecorator<typeof BYPASS_TENANCY_KEY>
createPrismaTenancyExtension()
function createPrismaTenancyExtension(tenancyService, options?): (client) => PrismaClientExtends<InternalArgs<{
}, {
}, {
}, {
}>>;Defined in: src/prisma/prisma-tenancy.extension.ts:96
Creates a Prisma Client Extension that sets the PostgreSQL RLS context before every model query when a tenant context exists.
Uses Prisma.defineExtension to access the base client via closure, then wraps each query in a batch transaction:
SELECT set_config(key, tenantId, TRUE)— sets the RLS variable (transaction-local)query(args)— the original query, now filtered by RLS
SECURITY: Uses $executeRaw tagged template with bind parameters. set_config() accepts parameterized values, unlike SET LOCAL which requires string interpolation. This eliminates SQL injection risk entirely.
Options:
dbSettingKey: PostgreSQL session variable name (default: app.current_tenant)autoInjectTenantId: Automatically inject tenant ID into write operationstenantIdField: Field name to inject tenant ID into (default: tenant_id)sharedModels: Models that are shared across tenants (skips RLS and injection)failClosed: Throw when model queries run without tenant context (default: true)
Interactive transactions: By default, the batch $transaction([set_config, query]) does not propagate into interactive transactions ($transaction(async (tx) => ...)). Two solutions:
- Enable
interactiveTransactionSupport: truefor transparent handling (uses Prisma internals). - Use the standalone
tenancyTransaction()helper (public APIs only).
Usage:
const prisma = basePrisma.$extends(
createPrismaTenancyExtension(tenancyService)
);Parameters
| Parameter | Type |
|---|---|
tenancyService | TenancyService |
options? | PrismaTenancyExtensionOptions |
Returns
(client) => PrismaClientExtends<InternalArgs<{ }, { }, { }, { }>>
propagateTenantHeaders()
function propagateTenantHeaders(headerName?): Record<string, string>;Defined in: src/propagation/propagate-tenant-headers.ts:34
Returns HTTP headers containing the current tenant ID for service-to-service propagation.
Works with any HTTP client (fetch, axios, got, undici, node:http) — no dependencies required. Returns an empty object when no tenant context is available.
Uses the static AsyncLocalStorage from TenancyContext, so it works anywhere in the call stack without dependency injection.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
headerName | string | DEFAULT_PROPAGATION_HEADER | Header name for tenant ID (default: 'X-Tenant-Id') |
Returns
Record<string, string>
Object with tenant header, or empty object if no tenant context
Example
// With fetch
const res = await fetch('/api/orders', {
headers: { ...propagateTenantHeaders() },
});
// With axios
const res = await axios.get('/api/orders', {
headers: propagateTenantHeaders(),
});
// With @nestjs/axios HttpService
this.httpService.get('/api/orders', {
headers: propagateTenantHeaders(),
});tenancyTransaction()
function tenancyTransaction<T, TTx>(
prisma,
tenancyService,
callback,
options?): Promise<T>;Defined in: src/prisma/tenancy-transaction.ts:44
Executes a Prisma interactive transaction with RLS tenant context.
Runs set_config() as the first statement inside the interactive transaction, ensuring the PostgreSQL session variable is set on the same connection that executes the callback queries.
Type Parameters
| Type Parameter | Default type |
|---|---|
T | - |
TTx extends PrismaTransactionContext | any |
Parameters
| Parameter | Type | Description |
|---|---|---|
prisma | PrismaTransactionClient<TTx> | PrismaClient instance (not extended — raw client) |
tenancyService | TenancyService | TenancyService to read current tenant |
callback | (tx) => Promise<T> | Function receiving the transaction client |
options? | TenancyTransactionOptions | Transaction timeout, isolation level, and DB setting key |
Returns
Promise<T>