nestjs / cqrs

A lightweight CQRS module for Nest framework (node.js) :balloon:
https://nestjs.com
MIT License
821 stars 151 forks source link

Request scope or an alternative for multitenancy, using CQRS module #1656

Open j3bb9z opened 4 months ago

j3bb9z commented 4 months ago

Is there an existing issue that is already proposing this?

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.

Papooch commented 3 weeks ago

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.

j3bb9z commented 2 weeks ago

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

Thanks a lot! 🙏