S
S
Speakermen2021-10-06 12:36:38
ORM
Speakermen, 2021-10-06 12:36:38

How to properly set up migrations in typeorm?

Good day. So I could not figure out the migrations in Nest.Js

subscriber what is this for?

ormconfig.json

{
  "type": "mysql",
  "host": "localhost",
  "port": 3306,
  "username": "root",
  "password": "root",
  "database": "app1",
  "entities": ["dist/**/*.entity{.ts,.js}"],
  "synchronize": false,
  "migrations": ["dist/migrations/**/*{.ts,.js}"],
  "cli": {
    "migrationsDir": "src/migrations"
  }
}


Wanted like in Laravel

migrate:fresh Delete all tables and run all migrations again
migrate:install Create migration store
migrate:refresh Reset and rerun all migrations
migrate:reset Rollback all database migrations
migrate:rollback Rollback last database migration
migrate:status Show status of each migration

Well, it turned out that some commands work for some reason only after nest start --watch from dist

"scripts": {
    "typeorm": "ts-node --transpile-only ./node_modules/typeorm/cli.js", //взял с оф сайта TypeOrm example
    "migrate:run": "npm run start && npm run typeorm migration:run",
    "migrate:generate": "npm run start && npm run typeorm migration:generate -- -n",
    "migrate:create": "npm run start && npm run typeorm migration:create -- -n",
    "migrate:revert": "npm run start && npm run typeorm migration:revert",
    "migrate:show": "npm run start && npm run typeorm migration:show",
    "migrate:drop": "npm run start && npm run typeorm schema:drop",
    "migrate:fresh": "npm run start && npm run typeorm schema:drop && npm run typeorm migration:run"
  },


I don't know if dev dependencies are needed ts-node

Works both ways

"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config=ormconfig.json"
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js --config=ormconfig.json",
"typeorm": "ts-node --transpile-only ./node_modules/typeorm/cli.js"
    "typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js --config=ormconfig.json"
typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config=ormconfig.json",


"devDependencies": {
    "ts-node": "^10.0.0",
  },


Migrations

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question