nestjs-prisma-starter
nestjs-prisma-starter copied to clipboard
feat(config): one config file and .env file per env
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
Hi @marcjulian , tell me if this PR is missing something 🙂
Friendly ping @marcjulian 🙂
@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!
@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;
@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 🙂
@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