You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/fundamentals/dynamic-modules.md
-96Lines changed: 0 additions & 96 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -278,99 +278,3 @@ When creating a module with:
278
278
-`forFeature`, you are expecting to use the configuration of a dynamic module's `forRoot` but need to modify some configuration specific to the calling module's needs (i.e. which repository this module should have access to, or the context that a logger should use.)
279
279
280
280
All of these, usually, have their `async` counterparts as well, `registerAsync`, `forRootAsync`, and `forFeatureAsync`, that mean the same thing, but use Nest's Dependency Injection for the configuration as well.
281
-
282
-
#### Configurable module builder
283
-
284
-
As manually creating highly configurable, dynamic modules that expose `async` methods (`registerAsync`, `forRootAsync`, etc.) is quite complicated, especially for newcomers, Nest exposes the `ConfigurableModuleBuilder` class that simplifies this process and lets you construct a module "blueprint" in just a few lines of code.
285
-
286
-
For example, let's take the example we used above (`ConfigModule`) and convert it to use the `ConfigurableModuleBuilder`. Before we start, let's make sure we create a dedicated interface that represents what options our `ConfigModule` takes in.
287
-
288
-
```typescript
289
-
exportinterfaceConfigModuleOptions {
290
-
folder:string;
291
-
}
292
-
```
293
-
294
-
With this in place, create a new dedicated file (alongside the existing `config.module.ts` file) and name it `config.module-definition.ts`. In this file, let's utilize the `ConfigurableModuleBuilder` to construct `ConfigModule` definition.
Extending the `ConfigurableModuleClass` means that `ConfigModule` provides now not only the `register` method (as previously with the custom implementation), but also the `registerAsync` method that lets consumers asynchronously configure that module, for example, by supplying async factories:
320
-
321
-
```typescript
322
-
@Module({
323
-
imports: [
324
-
ConfigModule.register({ folder: './config' }),
325
-
// or alternatively:
326
-
// ConfigModule.registerAsync({
327
-
// useFactory: () => {
328
-
// return {
329
-
// folder: './config',
330
-
// }
331
-
// },
332
-
// inject: [...any extra dependencies...]
333
-
// }),
334
-
],
335
-
})
336
-
exportclassAppModule {}
337
-
```
338
-
339
-
Lastly, let's update the `ConfigService` class to inject the generated module options' provider instead of the `'CONFIG_OPTIONS'` that we used so far.
`ConfigurableModuleClass` by default provides the `register` and its counterpart `registerAsync` methods. To use a different method name, use the `ConfigurableModuleBuilder#setClassMethodName` method, as follows:
This construction will instruct `ConfigurableModuleBuilder` to generate a class that exposes `forRoot` and `forRootAsync` instead.
362
-
363
-
#### Custom options factory class
364
-
365
-
Since the `registerAsync` method (or `forRootAsync` or any other name, depending on the configuration) lets consumer pass a provider definition that resolves to the module configuration, a library consumer could potentially supply a class to be used to construct the configuration object.
0 commit comments