[x ] Feature request
At this point, I do not have a way to register providers or modules using different lifecycles such as transient or per web request.
Be able to instantiate providers per web request.
By default all providers in Nest are singleton
There are many use cases for this but I am going to explain mine. I am trying to implement a multitenant application using the GraphQL module. In the database side, I am using a database per customer. What I would like to have is the ability to create my database models (mongoose models in this case) specifically for the right database in each request. I already created a middleware that can identify the name of the database I need to work with on that request so I have the information in the request already but because my providers were already instantiated I do not have a way to recreate my models at least not using the Nest container.
Nest version: 5.4.0
For Tooling issues:
- Node version: 11.2.0
- Platform: Mac
What we definitely want to provide is a support for async hooks once they hit a stable release. With async hooks, we could create a kind of 'request bag' that could be easily accessed by each provider/controller/anything, in order to set/get any value that concerns only processed request. That will enable us to provide a way to handle with, for example, transactions-per-request feature. Nevertheless, the async-hooks feature is still in the experimental phase.
See this old discussion for possible workarounds for now:
https://github.com/nestjs/nest/issues/173
Hello @kamilmysliwiec thank you for the details. Using a mix of GraphQL context and a middleware I do have access to the request information anywhere I need it in my resolvers but again because my providers are singleton I do not have a way to take advantage of NestJS dependency injector to instantiate let say a UserService per request. Something like this:
Request 1 -> Tenant A -> UserModel (for tenant A)
Request 2 -> Tenant B -> UserModel (for tenant B)
These user models would be pointing to different databases based on the tenant.
Wow you just closed this without any more comments.. nothing?
This discussion (+similar ones) took place many times already:
https://github.com/nestjs/nest/issues/173
https://github.com/nestjs/nest/issues/953
https://github.com/nestjs/nest/issues/890
https://github.com/nestjs/nest/issues/560#issuecomment-383283784
^ some examples
I fully understand your requirement and we'll definitely provide some solution at some point 馃檪 We're blocked now though
Thank you @kamilmysliwiec that is a way better explanation I really appreciate it. If possible I would like to know how can help with this. I already downloaded the source and was looking around the code to get familiar with it.
@orlaqp here is even more detailed answer https://github.com/nestjs/nest/issues/1376
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
This discussion (+similar ones) took place many times already:
https://github.com/nestjs/nest/issues/173
https://github.com/nestjs/nest/issues/953
https://github.com/nestjs/nest/issues/890
https://github.com/nestjs/nest/issues/560#issuecomment-383283784
^ some examples
I fully understand your requirement and we'll definitely provide some solution at some point 馃檪 We're blocked now though