Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
37
rated 0 times [  38] [ 1]  / answers: 1 / hits: 8803  / 3 Years ago, thu, february 25, 2021, 12:00:00

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.


More From » typescript

 Answers
9

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.


[#1736] Friday, February 19, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
casandra

Total Points: 334
Total Questions: 93
Total Answers: 104

Location: Denmark
Member since Tue, Jul 19, 2022
2 Years ago
casandra questions
Mon, Jul 6, 20, 00:00, 4 Years ago
Thu, May 21, 20, 00:00, 4 Years ago
Tue, Sep 17, 19, 00:00, 5 Years ago
Sat, Jan 19, 19, 00:00, 5 Years ago
;