-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proxy provider issues with tenant connections #208
Comments
Resolved by manually hard setting the CLS_REQ value in the storage (saveReq option was not working as intended): // CLS Module for async storage in context, helps with request id generation & storage
// See: https://papooch.github.io/nestjs-cls/introduction/how-it-works
ClsModule.forRootAsync({
global: true,
useFactory: () => ({
middleware: {
mount: true,
generateId: true,
// Generates the requestId as a UUID string type
idGenerator: () => uuidv4(),
setup: (cls, req) => {
cls.set('req', req);
},
},
}),
}),
ClsModule.forFeatureAsync({
global: true,
provide: TENANT_CONNECTION,
imports: [TenantModule],
inject: [ClsService, MultiTenantService],
useFactory: async (
cls: ClsService,
service: MultiTenantService,
): Promise<DataSource | undefined> => {
const request = cls.get<Request>('req');
const user = request.user;
if (user) {
const connection = await service.createConnection(user.tenantId);
return connection;
}
return undefined;
},
}), |
Thank you for the bug report ad the code snippets. I'll investigate this. |
Keep in mind, I'm trying to generate RequestIds and create a proxy provider bother at root... I see in the |
Hi, @knovu, I tried to test the issue locally with the setup you provided, but I couldn't reproduce the behavior that you describe. The Proxy Provider This is the test code that I tried (based on the original post): @Controller()
class TestController {
constructor(
@Inject(PROXY_RESPONSE)
private readonly proxyResponse: any,
) {}
@Post('/hello')
async getHello(): Promise<void> {
return this.proxyResponse;
}
}
@Module({
imports: [
ClsModule.forRootAsync({
global: true,
useFactory: () => ({
middleware: {
mount: true,
generateId: true,
idGenerator: () => Math.random().toString(),
// saveReq: true,
},
}),
}),
ClsModule.forFeatureAsync({
provide: PROXY_RESPONSE,
inject: [CLS_REQ],
useFactory: async (req: Request) => {
return {
response: req.body.request,
};
},
// make the TENANT_CONNECTION available for injection globally
global: true,
}),
],
controllers: [TestController],
})
class TestModule {}
describe('Using CLS_REQ Proxy provider', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [TestModule],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
return app;
});
afterAll(async () => {
await app.close();
});
it('should work', async () => {
await request(app.getHttpServer())
.post('/hello')
.send({ request: 'testing value' })
.expect(201)
.expect({ response: 'testing value' });
});
}); It works as intended on both version That said, the issues you describe - and the reason why it works if you manually save the request using a
The It's the same root cause as this dependency error in the NestJS FAQ. |
Problem statement:
I want to allow the ability to dynamically connect to databases with my Nest.js API based on the user's assigned organization id (tenant id). We currently use this package globally in root to generate request id's.
The request object from CLS_REQ is returning undefined
The text was updated successfully, but these errors were encountered: