首页 > 解决方案 > 如何在 typeorm 中指定 ormcofig.js

问题描述

我有typescript项目并在项目中创建以下ormconfig.js文件。

module.exports = {
  type: 'mysql',
  host: 'db',
  port: 3306,
  username: 'root',
  password: 'test',
  database: 'db',
  entities: ['dist/entities/**/*.entity.js'],
  migrations: ['dist/migrations/develop/**/*.js'],
  logging: true,
};

在我的项目中,以下tsconfig.json也在项目目录中创建。

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true
  }
}

npm run build

.ts编译到以下目录。

-dist
  -migrations
    -develop
  -entities
    -entity1.js
    -entity2.js

然后我尝试了以下typeorm migration

npm run typeorm migration:create -- -n migration

迁移文件是生成根目录而不是指定的迁移目录。

ormconfig.js似乎没有指定。

package.json正在关注

"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config ormconfig.js"

错在哪里?我该如何解决?

谢谢

标签: javascriptnode.jstypescriptnestjstypeorm

解决方案


迁移文件是生成根目录而不是指定的迁移目录。

cli: { migrationsDir: "dist/migrations/develop" }您可以通过添加到文件来设置生成迁移的目录ormconfig.js
migrations属性仅指定 typeorm 将从何处加载迁移。
查看文档

ormconfig.js 似乎没有指定。

我不知道你的意思是什么,你能详细说明一下吗?


推荐阅读