Database migration

  1. Database Migration

    Database migrations provide a way to incrementally update the database schema and keep it in sync with the application data model, while preserving the existing data in our database. Build, run, and restore migrations. TypeORM provides us with a dedicated CLI that we can use.

    The migration classes are separate from the Nest application source code. This is because their lifecycle is maintained by the TypeORM CLI. Therefore, we cannot take advantage of dependency injection and other Nest specific features for database migrations.


  1. Migration Configuration

    Before creating a new migration, we need to create a new TypeORM configuration file and set up our database connection correctly.

    First create an ormconfig.js file in the root directory to configure all the ports, passwords, etc. we use from the Docker Compose file. There are some additional key values to let the TypeORM migration know where our entities and migration files will be. TypeORM migrations need to process compiled files, which Nest will output in the /dist/folder folder.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    module.exports = {
    type: 'postgres',
    host: 'localhost',
    port: 5432,
    username: 'postgres',
    password: 'pass123',
    database: 'postgres',
    entities: ['dist/**/*.entity.js'],
    migrations: ['dist/migrations/*.js'],
    cli: {
    migrationsDir: 'src/migrations',
    },
    }

  1. Create Migration File

    Command: npx typeorm migration:create -n filename. npx allows us to use executable packages without installation.

    This directive generates a new migration file in the /src/migrations directory.

    The up() is where we want to indicate what needs to be changed and how, and the down() method is where we want to undo or roll back any of those changes.


  2. Migration Instructions

    npm typeorm migration:run migration;

    npm typeorm migration:revert reverts.

    The commands in the courtCanva project are npm run migrate-up ; npm run migrate-down .


  3. Syntax

    When naming the file, use time + status + table name.js


    insertOne

    1
    2
    3
    4
    5
    module.exports = {
    async up(db) {
    await db.collection("documentName").insertOne({})
    }
    }

    insertMany

    1
    2
    3
    4
    5
    module.exports = {
    async up(db) {
    await db.collection("documentName").insertMany([])
    }
    }

    updateOne

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    module.exports = {
    async up(db) {
    await db
    .collection("documentName")
    .updateOne({ fieldName: "value" },
    { $set: { field1: "value1", field2: "value2",}},
    { $unset: ["field3", "field4"] },
    );
    },
    };

  1. Precautions

    The default value/initial value of the schema in the database will not be generated when the database is migrated, such as isDeleted, updatedAt, etc., so you need to write it yourself. The creation time can be used with createdAt: new Date() .

Share