尝试 运行 迁移时的 Typeorm:缺少必需的参数:dataSource

Typeorm when trying to run migrations: Missing required argument: dataSource

我正在尝试 运行 使用 ormconfig.json 的 TypeORM 迁移

{
  "name": "default",
  "type": "postgres",
  "host": "ip-is-here",
  "port": 5432,
  "username": "name",
  "password": "12345",
  "database": "db1",
  "synchronize": false,
  "logging": false,
  "entities": ["dist/storage/**/*.js"],
  "migrations": ["dist/storage/migrations/**/*.js"],
  "cli": {
    "entitiesDir": "src/storage",
    "migrationsDir": "src/storage/migrations"
  }
}

通过yarn typeorm migration:run
但是报错:

Missing required argument: dataSource

我必须做什么? 谢谢您的指点!

只要弄清楚您必须定义定义数据源的文件的路径。 就我而言: yarn typeorm migration:run -d dist/datasources/datasource.js

TypeOrm 在更晚的版本中放弃了 ormconfig.json 的使用。你应该使用新的 sintaxis。创建 ormconfig.ts 并为您的数据库指定选项,在我的例子中它看起来像这样:

    export const connectionSource = new DataSource({
        migrationsTableName: 'migrations',
        type: 'postgres',
        host: 'localhost',
        port: 5432,
        username: 'user',
        password: 'pass',
        database: 'somehealthchecker',
        logging: false,
        synchronize: false,
        name: 'default',
        entities: ['src/**/**.entity{.ts,.js}'],
        migrations: ['src/migrations/**/*{.ts,.js}'],
        subscribers: ['src/subscriber/**/*{.ts,.js}'],
    });

运行连接后

    await connectionSource.initialize();

然后你可以使用

获取实体
    const myRepo = connectionSource.getRepository(SomeEntity)

peckage.json 中的脚本也应类似于此示例

"migration:generate": "./node_modules/.bin/ts-node ./node_modules/.bin/typeorm migration:generate -d src/modules/config/ormconfig.ts",
"migration:up": "./node_modules/.bin/ts-node ./node_modules/.bin/typeorm migration:run -d src/modules/config/ormconfig.ts",
"migration:down": "./node_modules/.bin/ts-node ./node_modules/.bin/typeorm migration:revert -d src/modules/config/ormconfig.ts",

在命令后,只需在控制台中输入迁移名称即可,无需 -n 选项