Skip to content

Commit aa56fc2

Browse files
committed
feat: mention the different registration methods
I've added in a segment about forRoot, forFeature, and register. I've seen this question pop up several times on Discord, in GitHub Issues, and in StackOverflow. I figured it was time for us to try and codify a reason for the naming. Of course, there's no hard rule here, but the guideline is suggested and it could become community standard which would be a big win for the mental load of creating and using new modules.
1 parent 3a9315d commit aa56fc2

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

content/fundamentals/dynamic-modules.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,18 @@ One final note: for simplicity we used a string-based injection token (`'CONFIG_
261261
export const CONFIG_OPTIONS = 'CONFIG_OPTIONS';
262262
```
263263

264+
### Community Guidelines
265+
266+
You may have seen the use for methods like `forRoot`, `register`, and `forFeature` around some of the `@nestjs/` packages and may be wondering what the difference for all of these methods are. There is no hard rule about this, but the `@nestjs/` packages try to follow these guidelines:
267+
268+
When creating a module with `register` you are expecting to configuring a dynamic module for this module only with a specific configuration. For example, with Nest's `@nestjs/axios`: `HttpModule.register({{ '{' }} baseUrl: 'someUrl' {{ '}' }})`. If, in another module you use `HttpModule.register({{ '{' }} baseUrl: 'somewhere else' {{ '}' }})`, it will have the different configuration. You can do this for as many modules as you want.
269+
270+
When creating a module with `forRoot`, you are expecting to configure a dynamic module once and re-using that configuration in multiple places (though possibly unknowingly as it's abstracted away). This is why you have one `GraphQLModule.forRoot()`, one `TypeOrmModule.forRoot()`, etc.
271+
272+
When creating a module with `forFeature` you are expecting to use the configuration of a dynamic module's `forRoot` but need module specific configuration like which repository this module should have access to, or the context that a logger should use.
273+
274+
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.
275+
264276
### Example
265277

266278
A full example of the code in this chapter can be found [here](https://github.com/nestjs/nest/tree/master/sample/25-dynamic-modules).

0 commit comments

Comments
 (0)