mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
24 lines
742 B
TypeScript
24 lines
742 B
TypeScript
import { ContextId, ContextIdStrategy, HostComponentInfo } from '@nestjs/core';
|
|
import { Request } from 'express';
|
|
|
|
const tenants = new Map<string, ContextId>();
|
|
|
|
export class DurableContextIdStrategy implements ContextIdStrategy {
|
|
attach(contextId: ContextId, request: Request) {
|
|
const tenantId = request.headers['x-tenant-id'] as string;
|
|
let tenantSubTreeId: ContextId;
|
|
|
|
if (tenants.has(tenantId)) {
|
|
tenantSubTreeId = tenants.get(tenantId)!;
|
|
} else {
|
|
tenantSubTreeId = { id: +tenantId } as ContextId;
|
|
tenants.set(tenantId, tenantSubTreeId);
|
|
}
|
|
return {
|
|
resolve: (info: HostComponentInfo) =>
|
|
info.isTreeDurable ? tenantSubTreeId : contextId,
|
|
payload: { tenantId },
|
|
};
|
|
}
|
|
}
|