Hi guys. I'm developing a multi-tenant - microservices system. By using mongoose and check the 'x-tenant-id' in every request headers, I can detech which tenant is using which database and ensure they will not be able to access each other database.
1 issue that I was facing is that, the checking step needs to be in Scope.REQUEST in order to check the header. However, it caused the "too many open connection" issue to MongoDB because each time I request to an API, it will open new connection.
1 solution that I found from Nest.js is Durable Provider https://docs.nestjs.com/fundamentals/injection-scopes#durable-providers. It actually worked because it performed some kind of caching the tenantId into a sub-map, so we don't have to query new connection every request. This is the code that I use for the Strategy. It's exactly the same as in the Nest.js documentation:
However, it only works for HTTP request that exist inside that service. Our microservices are communicating through MessagePattern, so I will have to include the tenantId inside the data context. But it seems to be impossible to retrieve that inside the Strategy code. I've try many ways but I couldn't achieve it. Can someone give me some idea? Thanks
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).