question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proper way to initialize config

See original GitHub issue

I’m submitting a…

[ ] Regression [ ] Bug report [ ] Feature request [x] Documentation issue or request [ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

There is nothing but a short snippet about a “configServiceProvider” in the documentation:

const configServiceProvider = {
  provide: ConfigService,
  useClass: DevelopmentConfigService,
};

@Module({
  components: [configServiceProvider],
})

Expected behavior

One article describing how to properly create a ConfigService provider which loads variables from the .env file, including validation (for example: is port actually a number between 1 and 65535?) and usage in other components.

It could easily be added to one of the examples showing how to pass database credentials for example.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:19
  • Comments:43 (11 by maintainers)

github_iconTop GitHub Comments

82reactions
kamilmysliwieccommented, Aug 24, 2018

Creating new abstractions like modules dependent on providers directly might create too much mess. However, I fully understand your concerns, the possibility to pass options asynchronously is a very essential thing. Thus, I just published new versions of both @nestjs/typeorm and @nestjs/mongoose modules. They expose forRootAsync() method now, which give various ways to initialize the configuration object.

1. Use factory

MongooseModule.forRootAsync({
  useFactory: () => ({
    uri: 'mongodb://localhost:27017/test',
  }),
}),

Obviously, our factory behaves like every other one (might be async and is able to inject dependencies inject).

MongooseModule.forRootAsync({
  imports: [ConfigModule],
  useFactory: async (configService: ConfigService) => ({
    uri: configService.getString('MONGO_URL'),
  }),
  inject: [ConfigService],
}),

2. Use class

MongooseModule.forRootAsync({
  useClass: MongooseConfigService,
})

Above construction will instantiate MongooseConfigService inside MongooseModule and will leverage it to create options object.

class MongooseConfigService implements MongooseOptionsFactory {
  createMongooseOptions(): MongooseModuleOptions {
    return {
      uri: 'mongodb://localhost:27017/test',
    };
  }
}

3. Use existing

MongooseModule.forRootAsync({
  imports: [ConfigModule],
  useExisting: ConfigService,
}),

It works the same as useClass with one critical difference - MongooseModule will lookup imported modules to reuse already created ConfigService, instead of instantiating it on its own.

The internal implementation is quite simple. Everyone who is interested in that should check the repositories to see how it is achieved underneath.

34reactions
sebastianvidalescommented, May 27, 2018

@cdiaz thanks. And now how do I pass a config variable to dynamic module in the forRoot() method?

import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';

@Module({
  imports: [MongooseModule.forRoot(this.config.mongoUri)],
})
export class ApplicationModule {}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Initialization Configuration — Python 3.11.1 ...
Python can be initialized with Py_InitializeFromConfig() and the PyConfig structure. It can be preinitialized with Py_PreInitialize() and the PyPreConfig ...
Read more >
Correct way to initialize settings in .net core 2.2?
Now to initialize GlobalSettings I used configure method in startup class like this. using (var scope = app.ApplicationServices.CreateScope()) { ...
Read more >
Chapter 7. Application Configuration and Initialization
The Struts initialization process reads, initially, the struts-config.xml file (also in the config directory) in order to determine how URL's are mapped to ......
Read more >
Initializing the Configuration - Revenera Product Documentation
The createConfig() method must be called in order to initialize the configuration. The createConfig() method creates a configuration for the SDK instance.
Read more >
How to Fix "Configuration System Failed to Initialize" Error in ...
How to Fix " Configuration System Failed to Initialize " Error in Windows Computer.Some users reported that Configuration system failed to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found