typescript 如何为 typeorm 指定 ormconfig.ts?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52187328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:38:15  来源:igfitidea点击:

how to specify ormconfig.ts for typeorm?

javascriptnode.jstypescripttypeorm

提问by Hussain Ali Akbar

I have created a sample typeorm project using the typeorm cli which has ormconfig.json by default:

我使用 typeorm cli 创建了一个示例 typeorm 项目,默认情况下它具有 ormconfig.json:

{
   "type": "postgres",
   "host": "localhost",
   "port": 5432,
   "username": "postgres",
   "password": "postgres",
   "database": "test",
   "synchronize": false,
   "entities": [
      "src/entity/**/*.ts"
   ],
   "migrations": [
      "database/migrations/**/*.ts"
   ],
   "subscribers": [
      "src/subscriber/**/*.ts"
   ],
   "cli": {
      "entitiesDir": "src/entity",
      "migrationsDir": "database/migrations",
      "subscribersDir": "src/subscriber"
   }
}

this is the directory structure:

这是目录结构:

-database
  -migrations
-src
  -entity
-ormconfig.json

This creates the migrations in the database/migrations folder properly as well as executes the migrations from it.

这会在 database/migrations 文件夹中正确创建迁移并从中执行迁移。

I replaced ormconfig.json with the following ormconfig.ts :

我用以下 ormconfig.ts 替换了 ormconfig.json :

export default {
    type: 'postgres',
    host: 'localhost',
    port: 5432,
    username: 'postgres',
    password: 'postgres',
    database: 'test',
    synchronize: false,
    "entities": [
        "src/entity/**/*.ts"
    ],
    "migrations": [
         "database/migrations/**/*.ts"
    ],
    "subscribers": [
        "src/subscriber/**/*.ts"
    ],
    "cli": {
        "entitiesDir": "src/entity",
        "migrationsDir": "database/migrations",
        "subscribersDir": "src/subscriber"
    }
};

this however creates migrations in the root directory instead of inside database/migrations.

然而,这会在根目录中而不是在数据库/迁移中创建迁移。

Can anyone help me in figuring out whats missing here and how i can use ormconfig.ts to generate migrations inside the intended directory?

任何人都可以帮助我弄清楚这里缺少什么以及我如何使用 ormconfig.ts 在预期目录中生成迁移?

Thanks!

谢谢!

回答by Adrien De Peretti

Hey i up this conversation since i can propose you a solution.

嘿,我结束这次谈话,因为我可以向你提出一个解决方案。

You can put the following line in your package.jsonfile:

您可以在package.json文件中添加以下行:

"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config server/environments/database.ts",

And your ts config must export directly the config by doing that:

并且您的 ts 配置必须通过这样做直接导出配置:

export = { /* your config */ };

As you can see, you can also specify the path of your config. No need for your config to be at the root level of your project.

如您所见,您还可以指定配置的路径。您的配置无需位于项目的根级别。

Hope that will help you

希望能帮到你

回答by Siipe

Just remove the defaultwhile exporting. Your ormconfig.tsshould be something like:

只需default在导出时删除。你ormconfig.ts应该是这样的:

import env from './src/env';

export = {
  host: env.DB_CONFIG.host,
  type: 'mysql',
  port: env.DB_CONFIG.port,
  username: env.DB_CONFIG.username,
  password: env.DB_CONFIG.password,
  database: env.DB_CONFIG.database,
  entities: [
    'src/**/**.entity{.ts,.js}',
  ],
  migrations: [
    'src/database/migrations/*.ts',
  ],
  cli: {
    migrationsDir: 'src/database/migrations',
  },
  synchronize: false,
};

In my case I'm using a main env.tsfile, as the database connection needs to be different depending on the environment. Also, don't forget using ts-nodefor dealing with typeorm cliin package.json:

就我而言,我使用的是主env.ts文件,因为数据库连接需要因环境而异。另外,不要忘记ts-node用于处理typeorm cliin package.json

...
"scripts": {
    ...
    "migrate:create": "ts-node ./node_modules/typeorm/cli.js migration:create -n",
    "migrate:up": "ts-node ./node_modules/typeorm/cli.js migration:run",
    "migrate:down": "ts-node ./node_modules/typeorm/cli.js migration:revert"
    ...
  }
...

So creating, running or rolling back migrations should be like:

所以创建、运行或回滚迁移应该是这样的:

npm run migrate:create FileName
npm run migrate:up
npm run migrate:down

回答by Clément Prévost

At the time of writing, TypeORM only look for ormconfig.jsonand ignores ormconfig.ts. There is work in progressto support it though.

在撰写本文时,TypeORM 仅查找ormconfig.json并忽略ormconfig.ts. 目前正在进行的工作来,虽然支持它。

Beside having ormconfig.json you need these commands in your package.json.

除了拥有 ormconfig.json 之外,您还需要在 package.json 中使用这些命令。

"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/.bin/typeorm",
"migration:generate": "npm run typeorm -- migration:generate --config src/config/ormconfig.json --connection  --name ",
"migration:run": "npm run typeorm -- migration:run"