Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

146
Visualizações
Do NestJS ConfigService needs DI?

So I've got a NestJS project with a CustomConfigService which extends @nestjs/config and overrides some of it's methods to deal with environment variables and I've got a myApp.config.ts that contains a dozen of exported consts that return config blocks, like this:


export const deviConfig = () => {
  return {
    secret: config.get('SECRET_KEY'),
    auth: config.get('AUTH_OPTION'),
    steps: config.get('STEPS_OPTIONS'),
    active: config.get('ENABLE_DEVI'),
  };
};

That config.get is coming from my custom class with the overwritten .get() from ConfigService.

The main thing here is: If I'm going to use my CustomConfigService on my main, my app.module and maybe on some other providers, should I use dependency injection?

If I was about to use the actual ConfigService methods, I suppose I should, since that's how the NestJS docs tell us to do, but since it's a custom class that overrides the actual ConfigService, should I?

The way I'm doing currently is:

main.ts: const configService = app.get(CustomConfigService);

configService.get('VAR') // this is my custom method

app.module.ts: const configService = new(CustomConfigService);

configService.get('VAR') // also my custom method

Also... I'm not really sure when should I use DI and when it's okay to just use the new(MyService), could you please enlighten me?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I believe you should always use DI when you are within the NestJS app context, that mean within any module/service/controller or resolver.

for .module, normally when importing module there is a way to inject ConfigService to be used within forRootAsync. For example the following code retrieved from nestjs documentation

MongooseModule.forRootAsync({
  imports: [ConfigModule],
  useFactory: async (configService: ConfigService) => ({
    uri: configService.get<string>('MONGODB_URI'),
  }),
  inject: [ConfigService],
});

the only time when there is no way of doing DI is when you are outside of the NestJS context on main.ts, then you need to get the ConfigService from the initiated app (like the example you have shown for the main.ts)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda