Search code examples
node.jstypeorm

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


I'm trying to run TypeORM migrations with ormconfig.json like this

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

via yarn typeorm migration:run
But get an error:

Missing required argument: dataSource

What I have to do? Thank you for your advices!


Solution

  • TypeOrm removed ormconfig.json support in version 0.3.0. You should use new syntax - create ormconfig.ts and specify options for you database, for example:

    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}'],
    });
    

    Then, after running the connection:

    await connectionSource.initialize();
    

    You can get entities by:

    const myRepo = connectionSource.getRepository(SomeEntity)
    

    Also your scripts in package.json should look like this:

    "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",
    

    After the command, just give the name for migration in the console without -n option