I would like to understand the difference between forRoot and forFeature in nest js dynamic modules.
I also would like to understand this difference in the case of the TypeOrm dynamic module used with nestjs.
I would like to understand the difference between forRoot and forFeature in nest js dynamic modules.
I also would like to understand this difference in the case of the TypeOrm dynamic module used with nestjs.
Generally speaking, as this does not always hold true, forRoot
/register
is a way to provide the configuration the module is going to use whereas forFeature
is away to create a dynamic provider that has it's own injection token.
In the case of the TypeOrmModule
as you mentioned, forRoot()
sets up the connection information that Nest makes use of, and Nest creates the injection token for the connection
that is created. For forFeature
, Nest takes that connection injection token under the hood and creates the injection token and custom provider for the repositories that were passed n. The token usually looks like <EntityName>Repository
, and uses a factory under the hood to inject the connection and get the repository out from the TypeORM system so it can be injected into your regular services.