38

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!

6 Answers 6

50

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

Sign up to request clarification or add additional context in comments.

6 Comments

This wanago.io/2022/07/25/api-nestjs-database-migrations-typeorm is a far more useful tutorial than the official docs
I am using NestJS, and I had to create an extra file that exports the DataSource object just for this purpose.
For my Nestjs projects, I also create a separate database configuration file to generate the migrations. I haven't found a way to make TypeOrm read configurations from a module yet, but it looks like TypeOrm can't do that. Since we use our own module system in Nestjs, if we were using Express, for example, we would need to create such a file one way or another, and there would be no problem. But at least it works
I wish this was written in the documentation
Then how can we import typeormmodule options in the AppModule? Or we have to write again all the configuration there?
|
16

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

1 Comment

Very nice answer .Thank you very much
7

Please don't forget --

If you need to pass parameter with dash to npm script, you will need to add them after --. For example, if you need to generate, the command is like this:

npm run typeorm migration:generate -- -n migrationNameHere

Docs here.

1 Comment

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
6

I got the same error while following their doc

npx typeorm-ts-node-commonjs migration:run -- -d ./src/data-source.ts

But it worked removing --

npx typeorm-ts-node-commonjs migration:run -d ./src/data-source.ts

So, I updated my package.json a little bit

"scripts": {
  "migration:run": "typeorm-ts-node-commonjs migration:run -d ./src/data-source.ts",
  "migration:revert": "typeorm-ts-node-commonjs migration:revert -d ./src/data-source.ts"
}

1 Comment

How to add scripts for generate command for migration in package.json. By the below way --- "generate": "typeorm migration:generate -n PostRefactoring". It is not working.
2

Just figure out that you have to define the path to the file where your Datasource is defined. In my case: yarn typeorm migration:run -d dist/datasources/datasource.js

1 Comment

hi,in my case this is have the path:dist/data-source.js but it still not work,can you have another way?
1

Use the App - Datasource method to initialize your connection and it's quit easier from there

export const AppDataSource = new DataSource({
    type: "postgres",
    host: "localhost",
    port: 5432,
    username: "postgres",
    password: "ROOT",
    database: "userLog",
    synchronize: true,
    logging: true, 
    entities: [User, Student],
    migrations: ["src/migration/**/*.ts"],
    migrationsTableName: "custom_migration_table",
    subscribers: ["src/migration/**/*.ts"],
})

Initialize your connection.

AppDataSource.initialize()
  .then(async () => {
    // do anything here like connecting to your express server or adding document to your db
  }

If it's a Javascript project, use this CLI command - typeorm migration:run

But if your project uses typescript, you can use ts-node in conjunction with typeorm to run .ts migration files. use the following commands

"create": "typeorm migration:create ./src/migration/learningMigration"
"generate": "typeorm migration:generate -n PostRefactoring"
"migrate": "npx typeorm-ts-node-commonjs migration:run -d src/data-source", 
"revert": "npx typeorm-ts-node-commonjs migration:revert -d src/data-source",

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.