Skip to content

Commit bd586d3

Browse files
Merge pull request #2578 from micalevisk/patch-7
docs(fundamentals): minor adjustments to clarify the behavior of durable providers
2 parents 82eecd9 + a217239 commit bd586d3

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

content/fundamentals/provider-scopes.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,21 +197,21 @@ export class AggregateByTenantContextIdStrategy implements ContextIdStrategy {
197197
198198
> warning **Warning** Note this strategy is not ideal for applications operating with a large number of tenants.
199199
200-
The value returned from the `attach` method instructs Nest what context identifier should be used for a given host. In this case, we specified that the `tenantSubTreeId` should be used instead of the original, auto-generated `contextId` object, when the host component (e.g., request-scoped controller) is flagged as durable (you can learn how to mark providers as durable below). Also, in this example, no payload would be registered (where payload = `REQUEST`/`CONTEXT` provider that represents the "root" - parent of the sub-tree).
200+
The value returned from the `attach` method instructs Nest what context identifier should be used for a given host. In this case, we specified that the `tenantSubTreeId` should be used instead of the original, auto-generated `contextId` object, when the host component (e.g., request-scoped controller) is flagged as durable (you can learn how to mark providers as durable below). Also, in the above example, **no payload** would be registered (where payload = `REQUEST`/`CONTEXT` provider that represents the "root" - parent of the sub-tree).
201201

202202
If you want to register the payload for a durable tree, use the following construction instead:
203203

204204
```typescript
205+
// The return of `AggregateByTenantContextIdStrategy#attach` method:
205206
return {
206-
resolve: (info: HostComponentInfo) => {
207-
const context = info.isTreeDurable ? tenantSubTreeId : contextId;
208-
return context;
207+
resolve: (info: HostComponentInfo) =>
208+
info.isTreeDurable ? tenantSubTreeId : contextId;
209209
},
210210
payload: { tenantId },
211211
}
212212
```
213213

214-
Now whenever you inject the `REQUEST` provider (or `CONTEXT` for GraphQL applications) using the `@Inject(REQUEST)`, the `payload` object would be injected (consisting of a single property - `tenantId` in this case).
214+
Now whenever you inject the `REQUEST` provider (or `CONTEXT` for GraphQL applications) using the `@Inject(REQUEST)`/`@Inject(CONTEXT)`, the `payload` object would be injected (consisting of a single property - `tenantId` in this case).
215215

216216
Alright so with this strategy in place, you can register it somewhere in your code (as it applies globally anyway), so for example, you could place it in the `main.ts` file:
217217

@@ -223,7 +223,7 @@ ContextIdFactory.apply(new AggregateByTenantContextIdStrategy());
223223
224224
As long as the registration occurs before any request hits your application, everything will work as intended.
225225

226-
Lastly, to turn a regular provider into a durable provider, simply set the `durable` flag to `true`:
226+
Lastly, to turn a regular provider into a durable provider, simply set the `durable` flag to `true` and change its scope to `Scope.REQUEST` (not needed if the REQUEST scope is in the injection chain already):
227227

228228
```typescript
229229
import { Injectable, Scope } from '@nestjs/common';
@@ -236,7 +236,7 @@ Similarly, for [custom providers](/fundamentals/custom-providers), set the `dura
236236

237237
```typescript
238238
{
239-
provide: 'CONNECTION_POOL',
239+
provide: 'foobar',
240240
useFactory: () => { ... },
241241
scope: Scope.REQUEST,
242242
durable: true,

0 commit comments

Comments
 (0)