typeorm config with .env variables

Viewed 19354

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

3 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

Since ormconfig is deprecated, I suggest another approach using TypeORM DataSource.

I'm using Heroku to deploy my server, so I created an environment file containing the same variable as created in Heroku Dynos, named .development.env:

DATABASE_URL="postgres://user:password@localhost:5432/main"

Note that you can place this file anywhere in your project tree.

Then I created a datasource file:

import dotenv from 'dotenv';
import { DataSource } from 'typeorm';

// Load env file
dotenv.config({ path: '../api/.development.env' });

DataSource definition
const AppDataSource = new DataSource({
  type: 'postgres',
  url: process.env.DATABASE_URL,
  logging: true,
  entities: ['../api/dist/**/*.entity.js'],
  migrations: ['./migrations/*.js'],
  subscribers: [],
});

export default AppDataSource;

This way, you can store in your environment your database connection settings.

You could hide ormconfig.json and put your secrets directly in there, or viceversa, load TypeORM's configuration from your .env file. Is there a precise reason why you need them to be separated? If yes, then we can work out a solution.

Related