vendix-multi-tenant-context

Propagate multi-tenant context across NestJS request lifecycles using AsyncLocalStorage.

5|Updated Aug 23, 2025
One-click install
npx skills add https://github.com/Rzyfront/Vendix --skill vendix-multi-tenant-context
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: vendix-multi-tenant-context
Source: https://github.com/Rzyfront/Vendix/tree/main/skills/vendix-multi-tenant-context
Command: npx skills add https://github.com/Rzyfront/Vendix --skill vendix-multi-tenant-context

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Vendix's multi-tenant backend requires a reliable per-request tenant context (store_id and organization_id) to be available across the entire execution flow without passing identifiers through every function.

Core Features & Use Cases

  • Context Bridge pattern attaches tenant information to the Request object early in the pipeline.
  • DomainResolverMiddleware resolves the tenant from hostname or headers to determine store and organization.
  • RequestContextInterceptor merges authentication data with domain context and initializes AsyncLocalStorage for downstream components.
  • Safe context access via RequestContextService prevents cross-tenant data leakage by not providing static fallbacks when context is missing.
  • Use case: a SaaS app serving multiple stores uses per-request tenant data to scope database queries and business rules.

Quick Start

Apply the DomainResolverMiddleware on routes, register the RequestContextInterceptor (globally or per scope), and use RequestContextService to access tenant context in services to scope data access.

Frequently Asked Questions about vendix-multi-tenant-context

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I propagate tenant context across requests in NestJS without passing identifiers to every function?

Multi-tenant context propagation in NestJS uses a DomainResolverMiddleware to identify the tenant from hostname or headers, then a RequestContextInterceptor initializes AsyncLocalStorage to maintain store_id and organization_id throughout the request lifecycle without passing identifiers manually.

What is the best way to prevent cross-tenant data leakage in a multi-tenant SaaS backend?

Preventing cross-tenant data leakage requires a safe RequestContextService that provides no static fallbacks when context is missing. By scoping database queries via AsyncLocalStorage initialized per request, the system securely isolates tenant data across stores and organizations.

How does AsyncLocalStorage work with NestJS interceptors for tenant isolation?

AsyncLocalStorage for tenant isolation works when a RequestContextInterceptor merges authentication data with domain context resolved by middleware. The interceptor initializes the storage, allowing downstream services to safely access store_id and organization_id without explicit parameter passing.

Can I use domain resolution middleware to scope database queries by hostname in a multi-tenant app?

Yes, a DomainResolverMiddleware resolves tenant information from the hostname or headers early in the request pipeline. This Context Bridge pattern attaches store_id and organization_id to the request, enabling downstream services to accurately scope database queries per tenant.

Do I need to register a request context interceptor globally for multi-tenant NestJS applications?

For multi-tenant NestJS applications, you should register the RequestContextInterceptor globally or per scope to ensure AsyncLocalStorage is initialized for all routes. This guarantees the tenant context is consistently available for all downstream operations and database scoping.

Why does my NestJS multi-tenant context return undefined when accessing store_id in downstream services?

Multi-tenant context returns undefined when the RequestContextInterceptor is not registered correctly or AsyncLocalStorage was not initialized for that request lifecycle. Ensure the interceptor runs before downstream services execute and the DomainResolverMiddleware successfully attached the tenant context.