Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typeorm config with .env variables

i have this ormconfig.json:

{
  "type": "postgres",
  "host": "db-pg",
  "port": 5432,
  "username": "spirit",
  "password": "api",
  "database": "emasa_ci",
  "synchronize": true,
  "logging": false,
  "entities": ["dist/src/entity/**/*.js"],
  "migrations": ["dist/src/migration/**/*.js"],
  "subscribers": ["dist/src/subscriber/**/*.js"],
  "cli": {
    "entitiesDir": "dist/src/entity",
    "migrationsDir": "dist/src/migration",
    "subscribersDir": "dist/src/subscriber"
  }
}

and have this env:

SERVER_PORT=4000
DB_HOST=db-pg
DB_PORT=5432
DB_USER=spirit
DB_PASS=api
DB_NAME=emasa_ci

but .env doesn't work in .json and so I don't know how I'm going to use my enviroment variables in my config orm

like image 295
Ming Avatar asked Jun 02 '20 01:06

Ming


People also ask

Can I use variables in .env file?

env file is included to use, so it's easy to have a different configuration based on the environment your app is running on. This gives you the flexibility to have different variables for local, staging, production, and even different developers' machines.

Is .env a config file?

In case you are still wondering what all this means, well, you are probably new to the . env file. It's actually a simple configuration text file that is used to define some variables you want to pass into your application's environment. This file needs a something like a parser to make it work.

Should you use .env in production?

Using environment variables is a somewhat common practice during Development but it is actually not a healthy practice to use with Production. While there are several reasons for this, one of the main reasons is that using environment variables can cause unexpected persistence of variable values.


1 Answers

There is a good documentation. If you want to dig into the source code - there is a class ConnectionOptionReader, which is looking for file ormconfig (with extensions env, js, cjs, ts, json, yml, yaml, xml) or for file .env. See the load function for more details.

1 So the easiest way is to add a line in your .env file, like this:

TYPEORM_URL=postgres://user:pass@host:port/dbname

Or use this sample. TypeORM will parse .env file using dotenv.
Here you can find all available env varibales.

2 If you read your .env file before TypeORM initialization, you can already use your env variables. For example in a Javascript file, instead ormconfig.json. Just export object like this from the file ormconfig.js:

module.exports = {
    "type": "postgres",
    "host": process.env.DB_HOST,
    "port": process.env.DB_PORT,
    "username": process.env.DB_USER,
    "password": process.env.DB_PASS,
    "database": process.env.DB_NAME,
    "synchronize": true,
    "logging": false,
    "entities": ["dist/src/entity/**/*.js"],
    "migrations": ["dist/src/migration/**/*.js"],
    "subscribers": ["dist/src/subscriber/**/*.js"],
    "cli": {
      "entitiesDir": "dist/src/entity",
      "migrationsDir": "dist/src/migration",
      "subscribersDir": "dist/src/subscriber"
    }
};

Another example

like image 68
Konstantin Bogomolov Avatar answered Sep 27 '22 02:09

Konstantin Bogomolov