Today I Learned

Impact of TYPEORM_CONNECTION on ormconfig in TypeOrm

When TypeOrm detects that either TYPEORM_CONNECTION or TYPEORM_URL env variables are set up, it will read the config from the env variables and ignore the one that is given by the ormconfig.js file:

// try to find connection options from any of available sources of configuration
if (PlatformTools.getEnvVariable("TYPEORM_CONNECTION") || PlatformTools.getEnvVariable("TYPEORM_URL")) {
   connectionOptions = await new ConnectionOptionsEnvReader().read();

From their docs:

Sometimes, you may want to use multiple configurations using different formats. When calling getConnectionOptions() or attempting to use createConnection() without the connection options, Typeorm will attempt to load the configurations, in this order:

  • From the environment variables. Typeorm will attempt to load the .env file using dotEnv if it exists. If the environment variables TYPEORM_CONNECTION or TYPEORM_URL are set, Typeorm will use this method.
  • From the ormconfig.env.
  • From the other ormconfig.[format] files, in this order: [js, ts, json, yml, yaml, xml].

Tip for the future: to use the ormconfig.js file, do not set up the TYPEORM_CONNECTION and TYPEORM_URL env vars.