Nest: What's the right way to holding configuration for multi-env?

Created on 7 Mar 2018  路  9Comments  路  Source: nestjs/nest

hold env config like: ip, port, db, ....

config/
dev.ts
prod.ts
test.ts

I have multi modules in project, should I create a shared module to hold env config?

question 馃檶

Most helpful comment

Are there plans to build @kamilmysliwiec recommendation into the framework?

All 9 comments

If I understand correctly, maybe this can help https://www.npmjs.com/package/config

@janckerchen Im handling this by this example: https://github.com/cojack/ts-config

Additionally, you can do it in more nest-way, using custom providers. Example:

provide: ConfigService,
useClass: process.env.NODE_ENV === 'development' 
     ? DevelopmentConfigService 
     : ProductionConfigService

where ConfigService is an abstract class and both DevelopmentConfigService and ProductionConfigService extends ConfigService.

thx @kamilmysliwiec . I found there are three ways to spread config to multi modules.

  1. pass config to anymodule.forRoot(config). it's a static way, config is passed before module creation and some provider factories will consume the config data.
  2. build ConfigService like @kamilmysliwiec mentioned above. config is injected to component constructor, that's mean config is passed before component creation.
  3. pass config to AnyService.Init(config) in module's constructor, because any component belongs to the module can be inject to module's constructor. At this time, component has been created.

I switched from rails, I expect proper nest-way to manage config.

Are there plans to build @kamilmysliwiec recommendation into the framework?

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yanshuf0 picture yanshuf0  路  3Comments

tronginc picture tronginc  路  3Comments

cojack picture cojack  路  3Comments

menme95 picture menme95  路  3Comments

janckerchen picture janckerchen  路  3Comments