Skip to content

Commit b5b6721

Browse files
chore(): revert configurable module builders section
1 parent 572b3ad commit b5b6721

File tree

1 file changed

+0
-96
lines changed

1 file changed

+0
-96
lines changed

content/fundamentals/dynamic-modules.md

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -278,99 +278,3 @@ When creating a module with:
278278
- `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.)
279279

280280
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-
export interface ConfigModuleOptions {
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.
295-
296-
```typescript
297-
@@filename(config.module-definition)
298-
export const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } =
299-
new ConfigurableModuleBuilder<ConfigModuleOptions>().build();
300-
@@switch
301-
export const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } =
302-
new ConfigurableModuleBuilder().build();
303-
```
304-
305-
Now let's open up the `config.module.ts` file and modify its implementation to leverage the auto-generated `ConfigurableModuleClass`:
306-
307-
```typescript
308-
import { Module } from '@nestjs/common';
309-
import { ConfigService } from './config.service';
310-
import { ConfigurableModuleClass } from './config.module-definition';
311-
312-
@Module({
313-
providers: [ConfigService],
314-
exports: [ConfigService],
315-
})
316-
export class ConfigModule extends ConfigurableModuleClass {}
317-
```
318-
319-
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-
export class AppModule {}
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.
340-
341-
```typescript
342-
@Injectable()
343-
export class ConfigService {
344-
constructor(@Inject(MODULE_OPTIONS_TOKEN) private options: ConfigModuleOptions) { ... }
345-
}
346-
```
347-
348-
#### Custom method key
349-
350-
`ConfigurableModuleClass` by default provides the `register` and its counterpart `registerAsync` methods. To use a different method name, use the `ConfigurableModuleBuilder#setClassMethodName` method, as follows:
351-
352-
```typescript
353-
@@filename(config.module-definition)
354-
export const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } =
355-
new ConfigurableModuleBuilder<ConfigModuleOptions>().setClassMethodName('forRoot').build();
356-
@@switch
357-
export const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } =
358-
new ConfigurableModuleBuilder().setClassMethodName('forRoot').build();
359-
```
360-
361-
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.
366-
367-
```typescript
368-
@Module({
369-
imports: [
370-
ConfigModule.registerAsync({
371-
useClass: ConfigModuleOptionsFactory,
372-
}),
373-
],
374-
})
375-
export class AppModule {}
376-
```

0 commit comments

Comments
 (0)