Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 248
Eddie R Avatar asked Aug 30 '25 15:08

Eddie R


2 Answers

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

like image 189
capraljohn Avatar answered Sep 04 '25 11:09

capraljohn


with latest typescript if you are using cli setup as per typeorm setup

then following package.json script will work

"scripts": {
   "typeorm": "typeorm-ts-node-commonjs -d ./src/datasources/PostgresDatasource.ts",
}

Run npm run typeorm migration:generate src/migration/initaltables npm run typeorm migration:run

like image 20
arun-r Avatar answered Sep 04 '25 11:09

arun-r