Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While creating a dynamic module in Nest.js should i use registerAsync or forRootAsync?

While creating a dynamic module some of the nestjs modules are using registerAsync() some use forRootAsync(). which is the recommended method or is there any difference between these two?

PassportModule.registerAsync({
  imports: [ConfigModule],
  useExisting: PassportConfigService,
}),

TypeOrmModule.forRootAsync({
  imports: [ConfigModule],
  useExisting: TypeormConfigService,
}),
like image 971
Frozenex Avatar asked Jan 12 '19 05:01

Frozenex


1 Answers

The names are just conventions and do not influence your application's behavior. Nevertheless, it is important to choose a name that properly fits your use case. I would consider the following criteria:

If your module has to be imported differently in root/child modules, then stick with forRoot/forChild.

Otherwise, use a name that describes your use case. Why do you need a dynamic import in the first place and what does it do? For instance:
MyDatabaseModule.populate(data) vs. MyDatabaseModule.createConnection(configuration)

Not all dynamic modules are actually asynchronous. So only use the postfix async if your import actually is (or can be) asynchronous. This also gives you the opportunity to offer both a synchronous and an asynchronous variant of the import.

like image 125
Kim Kern Avatar answered Oct 02 '22 21:10

Kim Kern