TypeORM

TypeORM - data-mapper ORM for TypeScript and JavaScript (ES7, ES6, ES5). Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, WebSQL databases. Works in Node.JS and Browser.

Migrations

All your migrations should implement MigrationInterface interface and implement up and down methods. You need to setup a directory in your connection options where your migrations will be. To run migrations you can use CLI or run migrations automatically on application startup.

Example

First, create migrations directory, where you'll create a migrations. Also you'll need to setup migrations directory in your connection options:


createConnection({
    driver: {
        type: "mysql",
        host: "localhost",
        port: 3306,
        username: "root",
        password: "admin",
        database: "test"
    },
    entities: [
        __dirname + "/entity/*.js"
    ],
    migrations: [
        __dirname + "/migration/*.js"
    ],
    cli: {
        migrationsDir: __dirname + "/migration"
    }
}).then(connection => {
    // ...
}).catch(error => console.log(error));
        

We also added cli section with migrationsDir to specify to typeorm CLI where to create migrations.

To create migration the easiest way is to use CLI. To use CLI install typeorm globally:

npm i -g typeorm

Now run typeorm -h and you'll get all information about how to run typeorm CLI.

To create a new migration run following command:

typeorm migrations:create -n FirstReleaseChanges

This command should create you a file named 1481548095-FirstReleaseChanges.ts. Inside this file you'll see a class:


import {MigrationInterface, Connection, QueryRunner} from "typeorm";

export class FirstReleaseChanges1481283582 implements MigrationInterface {

    async up(queryRunner: QueryRunner, connection: Connection): Promise<any> {
    }

    async down(queryRunner: QueryRunner, connection: Connection): Promise<any> {
    }

}
        

Here 1481548095 is a current timestamp. It is always different depend of current time.

Next step it to write some code in our new migration:


import {MigrationInterface, Connection, QueryRunner} from "typeorm";

export class FirstReleaseChanges1481283582 implements MigrationInterface {

    async up(queryRunner: QueryRunner, connection: Connection): Promise<any> {
        await queryRunner.renameColumn("post", "title", "name");
    }

    async down(queryRunner: QueryRunner, connection: Connection): Promise<any> {
        await queryRunner.renameColumn("post", "name", "title");
    }

}
        

queryRunner provides api using which you can execute queries in an elegant cross-platform way. Alternatively to renameColumn method you can execute a raw sql query:


import {MigrationInterface, Connection, QueryRunner} from "typeorm";

export class FirstReleaseChanges1481283582 implements MigrationInterface {

    async up(queryRunner: QueryRunner, connection: Connection): Promise<any> {
        await queryRunner.query("ALTER TABLE `post` CHANGE `title` `name` VARCHAR(255)");
    }

    async down(queryRunner: QueryRunner, connection: Connection): Promise<any> {
        await queryRunner.query("ALTER TABLE `post` CHANGE `name` `title` VARCHAR(255)");
    }

}
        

Now you can run your migrations using CLI:

typeorm migrations:run

To revert last migration run you can use CLI too:

typeorm migrations:revert