'Create a request scope without a request

I have an multitenant application that has a lot of request scoped services, where I look at the origin to see which tenant we're talking about, and which database filters to use, etc. This works fine as long as there is a request, but now I want to run a script for each of the tenants.

What I would like to do is loop through all the tenants, initialize a request scope for each tenant and run whatever needs to be run. How do I do this?

I am in the Nest context (a module's onApplicationBootstrap, to be specific), so I do have access to DI, just not the dependencies that depend on the request scope.

I found this post: Nestjs call request scoped service without request

But it leaves me with some questions:

  • how do I get a hold of NestApplication
  • it looks like the request is globally registered to the scope, doesn't that mean that after the loop is finished, the application still thinks it's in that request scope?


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source