#Required DI subtrees strategy?

9 messages · Page 1 of 1 (latest)

boreal geyser
#

Is it required to define the DI subtrees strategy for a "durable" provider to work correctly?

Or can I skip defining the strategy and have nestjs manages internally

Context:
I want to obtain the request in a certain provider, but I don't want to pass the request from the controller throughout the entire graph of providers

For that reason I have defined the scope of that provider as "REQUEST" but I want to make it durable for performance reasons, I am not really interested in having a multi tentant strategy

Thanks

dry socket
#

What about this would be durable? The point of durable providers is to re-get a request scoped provider that was created by an earlier request without having to recreate the provider itself. A common use case would be something like getting a specific database connection based on a request header.

Your use case sounds like you just want to get the request in a provider correct?

boreal geyser
#

Initially, it is what I want, without having to pass the request from the controller through the entire providers graph

When I saw the tutorial of making the request scope durable to reuse it, I added my doubt if it is necessary to define the strategy or nest could manages that

dry socket
#

What about the request scoped provider is durable though? That's what I'm trying to figure out here. Being durable means that Nest will old the request scoped provider in memory and when a request comes in that matches the strategy, then Nest will re-use that already created provider.

From what I understand, you're just wanting to pass the request to a provider, nothing needs to be held in memory or re-used on later request, correct? Each request would be unique, and thus there's no need for a durable request scope

boreal geyser
#

maybe I am mixing both doubts and that causes confusion

My first context-independent question is: is it necessary to define the subtrees strategy or can I omit it and nest will directly create a durable subtree which it will use for all requests?

I know my question doesn't make sense, because I want to have a kind of singleton but with the addition that I can access the REQUEST in the provider

#

but seeing my question from the implementation sense, is it required to define the strategy?

dry socket
#

I believe that you have to define a strategy. To my knowledge, there isn't a default durable strategy that Nest has in place.

Before we start flagging providers as durable, we must first register a strategy that instructs Nest what are those "common request attributes", provide logic that groups requests - associates them with their corresponding DI sub-trees.

boreal geyser
#

Ok, thanks!

Now my last question and the reason why everything came up, the only 2 ways to receive the REQUEST in a providers are:

  • send from controller through providers graph
  • set REQUEST scope

or is there another way?

dry socket
#

or is there another way?
I mean, other than something like nestjs-cls which uses async hooks and AsyncLocalStorage, no, there's not