Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save WooodHead/88e81769396f6bd22b3ec26ac2d68fe5 to your computer and use it in GitHub Desktop.
Save WooodHead/88e81769396f6bd22b3ec26ac2d68fe5 to your computer and use it in GitHub Desktop.
The Explaination of forRoot and ForFeature

ForRoot

This is useful when the registerAs is required across, so it is best to use it in the AppModule

import databaseConfig from './config/database.config';
@Module({
  imports: [
    ConfigModule.forRoot({
      load: [databaseConfig],
    }),
  ],
})
export class AppModule {}

the registerAs has to be loaded into the configureModule.forRoot by adding it in the the load array.

ForFeature

This is valuable to ensure the registerAs is only applicable for the module only. It is better as most of the config is required for the respective modules only.

import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import databaseConfig from './database.config';

@Module({
  imports: [ConfigModule.forFeature(databaseConfig)],
  providers: [],
  exports: []
})
export class CouchdbModule { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment