Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using different ormconfig.json files depending on env

My ormconfig.json is static of course, it looks like:

{
   "type": "mariadb",
   "host": "localhost",
   "port": 3306,
   "username": "root",
   "password": "moove",
   "database": "moove_db",
   "synchronize": true,
   "logging": false,
   "entities": [
      "dist/entity/**/*.js"
   ],
   "migrations": [
      "dist/migration/**/*.js"
   ],
   "subscribers": [
      "dist/subscriber/**/*.js"
   ],
   "cli": {
      "entitiesDir": "dist/entity",
      "migrationsDir": "dist/migration",
      "subscribersDir": "dist/subscriber"
   }
}

but what if I want to create another config for our production server? Do I create another config file? How do I point typeorm to the other config file?

like image 596
Alexander Mills Avatar asked Jun 27 '18 04:06

Alexander Mills


Video Answer


1 Answers

Don't use the ormconfig.json. You can pass a config object directly to createConnection() like

import { createConnection } from "typeorm";

const config:any = {
       "port": process.env.port || "28017",
       "entities": [
          // ...
       ],
       "migrations": [
          // ...
       ],
       "subscribers": [
         // ...
       ],
       "cli": {
          // ...
       }
    }
    createConnection(config).then(async connection => {
        await loadPosts(connection);
    }).catch(error => console.log(error));
like image 84
Richard Torcato Avatar answered Oct 26 '22 23:10

Richard Torcato