代码之家  ›  专栏  ›  技术社区  ›  Alexander Mills

根据env使用不同的ormconfig.json文件

  •  3
  • Alexander Mills  · 技术社区  · 6 年前

    我的ormconfig.json当然是静态的,它看起来像:

    {
       "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"
       }
    }
    

    但是如果我想为我们的生产服务器创建另一个配置呢? 是否创建另一个配置文件?如何将typeform指向其他配置文件?

    2 回复  |  直到 5 年前
        1
  •  5
  •   Alexander Mills    6 年前

    就目前而言,我可以改变 ormconfig.json ,至 ormconfig.js ,然后使用env变量,如下所示:

    module.exports = {
       "port": process.env.port,
       "entities": [
          // ...
       ],
       "migrations": [
          // ...
       ],
       "subscribers": [
         // ...
       ],
       "cli": {
          // ...
       }
    }
    
        2
  •  2
  •   Richard Torcato    5 年前

    不要使用ormconfig.json。可以将配置对象直接传递给createConnection(),如

    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));