nestjs-config icon indicating copy to clipboard operation
nestjs-config copied to clipboard

Use nestjs-config out of services

Open timofeyp opened this issue 4 years ago • 3 comments

Issue type:

  • [ x] question

nestjs-config version 0.6.3

@nestjs/common+core or other package versions

  • @nestjs/common: 7.6.3
  • @nestjs/core: 7.6.12

Excepted behavior

I want to use configuration params out of services and main.ts file. Eg in module.ts files to configure @nestjs/jwt module (secret, expiresTime) or in ormconfig file to configure databse. How can I import config in this files ?

timofeyp avatar Mar 14 '21 09:03 timofeyp

you might want to check next out. This will allow you to do this sort of thing

bashleigh avatar May 17 '21 11:05 bashleigh

you might want to check next out. This will allow you to do this sort of thing

How can new config be used with app.use in bootsrap file, and does it support env schema validation ?

timofeyp avatar Jun 03 '21 16:06 timofeyp

You can still do it with the current version

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';
import { NestExpressApplication } from '@nestjs/platform-express';
import AppConfig from './config/app.config';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);

  app.useGlobalPipes(
    new ValidationPipe({
      transform: true,
      //forbidNonWhitelisted: true,
      whitelist: true,
    }),
  );

  await app.init();

  const config = app.get(AppConfig);

  await app.listen(config.port || 9000);
}
bootstrap();

This was taken from one of my applications

bashleigh avatar Jun 03 '21 20:06 bashleigh