it0/packages/shared/database/src/tenant-context.middleware.ts

23 lines
572 B
TypeScript

import { Injectable, NestMiddleware } from '@nestjs/common';
import { TenantContextService, TenantInfo } from '@it0/common';
@Injectable()
export class TenantContextMiddleware implements NestMiddleware {
use(req: any, res: any, next: () => void) {
const tenantId = req.headers?.['x-tenant-id'] as string;
if (!tenantId) {
return next();
}
const tenantInfo: TenantInfo = {
tenantId,
tenantName: tenantId,
plan: 'free',
schemaName: `it0_t_${tenantId}`,
};
TenantContextService.run(tenantInfo, () => next());
}
}