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

两盒软妹~` 提交于 2020-01-21 18:39:24

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/54157097/while-creating-a-dynamic-module-in-nest-js-should-i-use-registerasync-or-forroot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!