我已经使用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"
   }
}

这是目录结构:
-database
  -migrations
-src
  -entity
-ormconfig.json

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

我将ormconfig.json替换为以下ormconfig.ts:
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"
    }
};

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

谁能帮助我找出此处缺少的内容,以及如何使用ormconfig.ts在预期目录内生成迁移?

谢谢!

最佳答案

在撰写本文时,TypeORM仅查找ormconfig.json,而忽略ormconfig.ts。虽然有work in progress支持它。

除了拥有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"

09-17 00:15