nestjs-prisma-starter icon indicating copy to clipboard operation
nestjs-prisma-starter copied to clipboard

feat(config): one config file and .env file per env

Open Oupsla opened this issue 5 years ago • 6 comments

Following discussion on : https://github.com/fivethree-team/nestjs-prisma-starter/pull/420

The goal of this PR is to be able to have one config file (and one .env file) for each environnement (development, staging, ...).

Little note, I have renamed example.env to .env.development because by default we will require this file

Oupsla avatar Oct 05 '20 08:10 Oupsla

Hi @marcjulian , tell me if this PR is missing something 🙂

Oupsla avatar Oct 11 '20 10:10 Oupsla

Friendly ping @marcjulian 🙂

Oupsla avatar Oct 15 '20 18:10 Oupsla

@Oupsla I haven't had the time to look at your PR. I will come back to it 👍 thanks again for contributing to this project!

marcjulian avatar Oct 16 '20 09:10 marcjulian

@Oupsla nest supports what you are trying to do out of the box. with file based

.env.development .env.local .env.test {....}

@marcjulian there is actually a problem with the way the config is being loaded.

the config.ts file should not use a constant but should return the configuration from the exported function so it builds the return object when it is actually loaded (after dot env as done its magic as part of the nestjs configuration service) rather than how it is now

()=>{ ....config } can load file based .env variables

rather than

const config = {} can't load file based .env variables as the constant is defined before dot-env is initialized by nestJS ()=> config;

epic0tix avatar Oct 18 '20 07:10 epic0tix

@Oupsla nest supports what you are trying to do out of the box. with file based

Yes nest loads the correct .env file based on the NODE_ENV variable (thanks to dotenv package). But the goal was to have a config file for non-secret variables (typed, more structured, ...) and so to have one config file per environnement (and secrets are staying in .env files)

I don't know if nest can load a config file out of the box ?

@marcjulian there is actually a problem with the way the config is being loaded.

I didn't know about that, but you are maybe right, I'll convert it into a function that loads the correct config file 🙂

Oupsla avatar Oct 18 '20 09:10 Oupsla

@adamrogas do you mean it should be loaded like so

export default () => ({
  port: parseInt(process.env.PORT, 10) || 3000,
  database: {
    host: process.env.DATABASE_HOST,
    port: parseInt(process.env.DATABASE_PORT, 10) || 5432
  }
});

It is also described in the docs https://docs.nestjs.com/techniques/configuration#custom-configuration-files

marcjulian avatar Oct 19 '20 06:10 marcjulian