how can i specify the migrations directory for typeorm CLI

Viewed 2845

After the new typeorm release a have some troubles to work with migrations.

Some time ago i was using that code and it work

entities: ['./src/modules/**/infra/typeorm/entities/*.ts'],
migrations: ['./src/shared/infra/typeorm/migrations/*.ts'],
cli: {
  migrationsDir: './src/shared/infra/typeorm/migrations'
}

But now i cant specify the cli property. To create a new migrations i have to specify the entire migration path

npm run typeorm migration:create ./src/database/migrations -n SomeTest

is there another way to do that without specify the entire path?

4 Answers

As Jun 2022, the docs is outed -n MigrationName is no longer supported. You can do this instead:

typescript esm: npx typeorm-ts-node-esm migration:create src/database/migration/MigrationFileName where MigrationFileName is the filename you want to create and src/database/migration/ is the path.

typescript commonjs: npx typeorm-ts-node-commonjs migration:create

P.S This might be late but this could save others. P.S I just discover this myself. If this doesn't work in the future, let me know, so I would know as well.

Create ormconfig.ts

import { DataSource } from 'typeorm';

export const AppDataSource = new DataSource({
  type: 'postgres',
  host: 'localhost',
  port: 5432,
  username: 'postgres',
  password: 'password',
  database: 'postgres',
  entities: ['dist/**/*.entity.js'],
  logging: true,
  synchronize: false,
  migrationsRun: false,
  migrations: ['dist/**/migrations/*.js'],
  migrationsTableName: 'history',
});

Install "cross-var" package Add commands in your package.json file

"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli -d ormconfig.ts",
"migration:create": "cross-var ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli migration:create ./src/migrations/$npm_config_name",
"migration:generate": "cross-var npm run typeorm -- migration:generate ./src/migrations/$npm_config_name",
"migration:run": "npm run build && npm run typeorm -- migration:run",
"migration:revert": "npm run typeorm -- migration:revert"

Example command

"npm run migration:create --name=Test1"

Look this project

As of submitting this answer, there seems not to be a way around it. You can specify the path when creating new migration just as you have done

typeorm migration:create -n UserMigration -d src/migrations

I had this problem and I solved it by removing the -n to give the name: yarn typeorm migration:create but it saves the file in the root folder, even specified in the config.json file, to save it in place you have to pass the yarn path typeorm migration:create src/database/migrations/

Related