cqrs icon indicating copy to clipboard operation
cqrs copied to clipboard

Request scope or an alternative for multitenancy, using CQRS module

Open j3bb9z opened this issue 1 year ago • 2 comments

Is there an existing issue that is already proposing this?

  • [X] I have searched the existing issues

Is your feature request related to a problem? Please describe it

Normally we can do multitenancy using durable providers: https://docs.nestjs.com/fundamentals/injection-scopes#durable-providers

However this doesn't work when using CQRS module.

There is an issue with 80 comments on this, but it's closed for discussion, with no further explanation: https://github.com/nestjs/cqrs/issues/60

There is a MR, but again - without any answer: https://github.com/nestjs/cqrs/pull/549

We need either request scope to work in CQRS module or any viable alternative.

Describe the solution you'd like

Either make request scoped providers work with CQRS module or providing an alternative, how to do multi-tenant applications. Creating separate query bus for every request seems too much of an overhead, but I think it's acceptable, when combined with durable providers and limited number of tenants.

Teachability, documentation, adoption, migration strategy

No response

What is the motivation / use case for changing the behavior?

We are implementing multitenancy in quite big application, with many modules that need to be configured differently, for different tenants. Unfortunately, async_hooks will not be an easy solution, since many modules are configured once, upon their creation. Request scope + durable providers seems to be simplest solution, but it doesn't work with CQRS module.

j3bb9z avatar Mar 14 '24 12:03 j3bb9z

If you need to use request scope with CQRS, you'd either need to play with ContextIdFactory or use an alternative like AsyncLocalStorage to propagate request context.

Papooch avatar Jul 11 '24 08:07 Papooch

Hi, @Papooch. I'm in the process of implementing multi-tenancy using your nestjs-cls library.

Thanks a lot! 🙏

j3bb9z avatar Jul 18 '24 23:07 j3bb9z

https://github.com/nestjs/cqrs/pull/1861

kamilmysliwiec avatar Dec 03 '24 09:12 kamilmysliwiec