Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

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 Jun 2022, the docs is outdated -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.

I have the following scripts in my package.json to solve this problem.

"migration:run": "typeorm -d src/datasource/datasource.ts migration:run",
"migration:create":"cd ./src/db/migrations && typeorm-ts-node-commonjs migration:create",

So whenever I want to create a new migration file, I could simply run npm run migration:create nameOfMigration without specifying the entire directory.

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/

Unfortunately, the $npm_config feature is not supported by yarn. Thus, I've tried the following scripts below as a workaround and it worked for me.

Yarn does not support setting npm_config env via CLI parameter like NPM

"migration:create":"cd src/migrations && npx typeorm-ts-node-commonjs migration:create",
"migration:generate":"cd src/migrations && npx typeorm-ts-node-commonjs migration:generate -d <YOUR_DATASOURCE_CONFIG_PATH>",

In typeorm version 0.3.12 you should use like this in windows.

"scripts": {
  "migrate:create": "typeorm migration:create ./src/schemas/typeorm/migration/%NAME%"

path example: ./src/schemas/typeorm/migration/%NAME%

command in terminal:

NAME=test2 typeorm migration:create
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.