Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify the migrations directory for typeorm CLI?

After the new typeorm release I am having some troubles to work with migrations.

Some time ago I was using this code and it worked -

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

But now I can't specify the CLI property. To create a new migration, 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 specifying the entire path?

like image 895
Matheus Santos Araújo Avatar asked Mar 03 '26 02:03

Matheus Santos Araújo


1 Answers

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

like image 187
Bohdan Salenik Avatar answered Mar 04 '26 15:03

Bohdan Salenik