Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

236
Views
How to read config value using ConfigModule inside of a provider?

I would like to add a config module to my nestjs application and use some config in my database module.

For example I would like to get the host value of my configuration: In a class I can do const dbHost = this.configService.get<string>('database.host');, but in my code I need the value inside of a provider. How do I get config values inside of a provider?

app.module.ts

@Module({
  imports: [
    DatabaseModule,
    ConfigModule.forRoot({
      load: [configuration],
    }),
  ],
})
export class AppModule {}

database.module.ts

import { Module, Inject } from '@nestjs/common'
import { MongoClient, MongoClientOptions, Db, Logger } from 'mongodb'

@Module({
  providers: [
    {
      provide: 'DATABASE_CLIENT',
      useFactory: () => ({ client: null })
    },
    {
      provide: 'DATABASE_CONNECTION',
      inject: ['DATABASE_CLIENT'],
      useFactory: async (dbClient): Promise<Db> => {
        Logger.setLevel('debug')

        // How do I get the host value of my config?

        const mongo: string = 'mongodb://127.0.0.1:27017'
        const database: string = 'data'
        const options: MongoClientOptions = {}
        const client = new MongoClient(mongo, options)
        await client.connect()
        const db = client.db(database)
        return db
      }
    }
  ],
  exports: ['DATABASE_CONNECTION', 'DATABASE_CLIENT'],
  import: [ConfigModule]
})
export class DatabaseModule {
  constructor(@Inject('DATABASE_CLIENT') private dbClient) {}

  onApplicationShutdown(signal: string) {
    if (signal) console.log(signal + ' signal recieved')
  }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In your inject add ConfigService, then it'll be the second value of useFactory's parameters.

@Module({
  providers: [
    {
      provide: 'DATABASE_CLIENT',
      useFactory: () => ({ client: null })
    },
    {
      provide: 'DATABASE_CONNECTION',
      inject: ['DATABASE_CLIENT', ConfigService],
      useFactory: async (dbClient, config: ConfigService): Promise<Db> => {
        Logger.setLevel('debug')

       const dbHost = config.get('database.host')

        const mongo: string = 'mongodb://127.0.0.1:27017'
        const database: string = 'data'
        const options: MongoClientOptions = {}
        const client = new MongoClient(mongo, options)
        await client.connect()
        const db = client.db(database)
        return db
      }
    }
  ],
  exports: ['DATABASE_CONNECTION', 'DATABASE_CLIENT'],
  import: [ConfigModule]
})
export class DatabaseModule {
  constructor(@Inject('DATABASE_CLIENT') private dbClient) {}

  onApplicationShutdown(signal: string) {
    if (signal) console.log(signal + ' signal recieved')
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!