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,
}),
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.