Skip to content

pavlokobyliatskyi/typeorm-migrations

Repository files navigation

TypeORM Migrations 😅

In this example I am using full paths for better understanding

Here is the nx project which has one app cars-service.

The directory is called database and is located in the cars-service app, which is used only for working with migrations in dev and prod.

The parameters for connecting to the database are located in the .env file in the root of the project.

In dev we work with .ts files in prod only with .js.

Goals

  • Does not affect the code and can be removed at any time
  • Unified database connection settings
  • Typing support during development

Dev

Change NODE_ENV to dev in .env file

Creating Migrations

Here init is the migration name

npx typeorm migration:create ./apps/cars-service/src/database/migrations/init

Generating Migrations

Here cars-create-table is the migration name

npx ts-node-dev -P ./apps/cars-service/tsconfig.app.json ./node_modules/typeorm/cli.js migration:generate ./apps/cars-service/src/database/migrations/cars-create-table -d ./apps/cars-service/src/database/typeorm.config.ts

Show migration

npx ts-node-dev -P ./apps/cars-service/tsconfig.app.json ./node_modules/typeorm/cli.js migration:show -d ./apps/cars-service/src/database/typeorm.config.ts

Applying Migrations

npx ts-node-dev -P ./apps/cars-service/tsconfig.app.json ./node_modules/typeorm/cli.js migration:run -d ./apps/cars-service/src/database/typeorm.config.ts

Rolling Back Migrations

npx ts-node-dev -P ./apps/cars-service/tsconfig.app.json ./node_modules/typeorm/cli.js migration:revert -d ./apps/cars-service/src/database/typeorm.config.ts

Build

If you want to create a docker image, just create a Dockerfile and do the same

Build the cars-service app

nx run cars-service:build

Build typescript.config.ts and migrations

npx tsc ./apps/cars-service/src/database/*.ts ./apps/cars-service/src/database/**/*.ts --outDir ./dist/apps/cars-service/database

Prod

Change NODE_ENV to prod in .env file

Applying Migrations

typeorm -d ./dist/apps/cars-service/database/typeorm.config.js migration:run

Rolling Back Migrations

typeorm -d ./dist/apps/cars-service/database/typeorm.config.js migration:revert

If the .env file is in a special location, use env-cmd.

./node_modules/.bin/env-cmd -f ./.env typeorm -d ./dist/apps/cars-service/database/typeorm.config.js migration:run

Nx Executors (Commands)

You can set up commands to work with migrations, for example

"migration:create": {
  "executor": "nx:run-commands",
  "options": {
    "command": "npx typeorm migration:create src/database/migrations/{args.name}",
    "cwd": "apps/cars-service"
  }
}

Or

"migration:generate": {
  "executor": "nx:run-commands",
  "options": {
    "command": "npx ts-node-dev -P ./tsconfig.app.json ./../../node_modules/typeorm/cli.js migration:generate ./src/database/migrations/{args.name} -d ./src/database/typeorm.config.ts",
    "cwd": "apps/cars-service"
  }
}

And then call the command through the terminal

nx run cars-service:migration:create --name=init

Or

nx run cars-service:migration:generate --name=cars-add-color-column