7 min read
Original source

Database migrations with TypeORM

When working with relational databases, we define the structure of the data rather strictly. For example, we need to specify the format of every table along…

When working with relational databases, we define the structure of the data rather strictly. For example, we need to specify the format of every table along with fields, relations, indexes, and other structures. By doing that, we also tell the database how to validate the incoming data. It is crucial to think about the structure of our database carefully. Even if we do that, the requirements that our application has to meet change. Because of the above, we rarely can avoid having to modify the structure of our database. When doing that, we need to be careful not to lose any existing data. With database migrations, we can define a set of controlled changes that aim to modify the structure of the data. They can include adding or removing tables, changing columns, or changing the data types, for example. While we could manually run SQL queries that make the necessary adjustments, this is not the optimal approach. Instead, we want our migrations to be easy to repeat across different application environments. Also, we need to acknowledge that modifying the structure of the database is a delicate process where things can go wrong and damage the existing data. Fortunately, writing database migrations includes committing them to the repository. Therefore, they can undergo a rigorous review before merging to the master branch. In this article, we go through the idea of migrations and learn how to perform them with TypeORM. Working with migrations using TypeORM When configuring TypeORM, we can set the synchronize property to true. This causes TypeORM to synchronize the database with our entities automatically. However, using it in production is highly discouraged because it might lead to unexpected data loss. Instead, TypeORM has a tool that helps us create and run migrations. Unfortunately, its migration documentation is outdated and does not match the latest version. Configuring the TypeORM CLI To start working with migrations using TypeORM, we need to properly configure its command line interface (CLI). To do that, we need to create a designated configuration file. typeOrm.config.ts import { DataSource } from 'typeorm'; import { ConfigService } from '@nestjs/config'; import { config } from 'dotenv'; config(); const configService = new ConfigService(); export default new DataSource({ type: 'postgres', host: configService.get('POSTGRES_HOST'), port: configService.get('POSTGRES_PORT'), username: configService.get('POSTGRES_USER'), password: configService.get('POSTGRES_PASSWORD'), database: configService.get('POSTGRES_DB'), entities: [], }); Above, we use dotenv to make sure the ConfigService loaded the environment variables before using it. We also need to add some entries to the scripts in our package.json. package.json "scripts": { "typeorm": "ts-node ./node_modules/typeorm/cli", "typeorm:run-migrations": "npm run typeorm migration:run -- -d ./typeOrm.config.ts", "typeorm:generate-migration": "npm run typeorm -- -d ./typeOrm.config.ts migration:generate ./migrations/$npm_config_name", "typeorm:create-migration": "npm run typeorm -- migration:create ./migrations/$npm_config_name", "typeorm:revert-migration": "npm run typeorm -- -d ./typeOrm.config.ts migration:revert", ... } Unfortunately, the $npm_config feature is not supported by yarn. Generating our first migration Let’s define a straightforward entity of a post. post.entity.ts import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; @Entity() class PostEntity { @PrimaryGeneratedColumn('identity', { generatedIdentity: 'ALWAYS', }) id: number; @Column() title: string; @Column() content: string; } export default PostEntity; If you want to know more about identity columns, check out Serial type versus identity columns in PostgreSQL and TypeORM There is a significant caveat regarding the entities directory in our configuration. Let’s take a look at our NestJS database configuration. database.module.ts import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { ConfigModule, ConfigService } from '@nestjs/config'; @Module({ imports: [ TypeOrmModule.forRootAsync({ imports: [ConfigModule], inject: [ConfigService], useFactory: (configService: ConfigService) => ({ type: 'postgres', host: configService.get('POSTGRES_HOST'), port: configService.get('POSTGRES_PORT'), username: configService.get('POSTGRES_USER'), password: configService.get('POSTGRES_PASSWORD'), database: configService.get('POSTGRES_DB'), entities: [], autoLoadEntities: true, }), }), ], }) class DatabaseModule {} export default DatabaseModule;The @nestjs/typeorm library implements the autoLoadEntities that analyzes our NestJS application and identifies all of our entities. Unfortunately, the basic TypeORM configuration can’t do that. We still need to add the entities we don’t use through TypeOrmModule.forFeature() to the entities array. Because of the above, we need to manually add the PostEntity to our entities array in our CLI configuration. typeOrm.config.ts import { DataSource } from 'typeorm'; import { ConfigService } from '@nestjs/config'; import { config } from 'dotenv'; import PostEntity from './src/posts/post.entity'; config(); const configService = new ConfigService(); export default new DataSource({ type: 'postgres', host: configService.get('POSTGRES_HOST'), port: configService.get('POSTGRES_PORT'), username: configService.get('POSTGRES_USER'), password: configService.get('POSTGRES_PASSWORD'), database: configService.get('POSTGRES_DB'), entities: [PostEntity], }); We might be able to figure out a better approach if the CLI would support asynchronous DataSource creation. Once the pull request with the improvement is merged, we could create a NestJS application in our typeOrm.config.ts file and take advantage of the autoLoadEntities property. Once we have all of the above set up, we can use the migration:generate command to let TypeORM generate the migration file.npm run typeorm:generate-migration --name=CreatePostRunning the migration command creates a file with the code that can bring our database from one state to another and back. Its filename consists of the current timestamp followed by the name provided when using the migration:generate command. 1658694616973-CreatePost.ts import { MigrationInterface, QueryRunner } from 'typeorm'; export class CreatePost1658694616973 implements MigrationInterface { name = 'CreatePost1658694616973'; public async up(queryRunner: QueryRunner): Promise { await queryRunner.query( `CREATE TABLE "post_entity" ("id" integer GENERATED ALWAYS AS IDENTITY NOT NULL, "title" character varying NOT NULL, "content" character varying NOT NULL, CONSTRAINT "PK_58a149c4e88bf49036bc4c8c79f" PRIMARY KEY ("id"))`, ); } public async down(queryRunner: QueryRunner): Promise { await queryRunner.query(`DROP TABLE "post_entity"`); } } I used prettier on the generated file. Above, there are two methods: up – performs the migration, down – reverts it. Running the migrations To run a migration, we must add it to the migrations array in our typeOrm.config.ts file. Unfortunately, using strings with the migrations array is deprecated and will stop working in TypeORM 0.4. Because of that, we should import the migration classes manually. typeOrm.config.ts import { DataSource } from 'typeorm'; import { ConfigService } from '@nestjs/config'; import { config } from 'dotenv'; import PostEntity from './src/posts/post.entity'; import { CreatePost1658694616973 } from './migrations/1658694616973-CreatePost'; config(); const configService = new ConfigService(); export default new DataSource({ type: 'postgres', host: configService.get('POSTGRES_HOST'), port: configService.get('POSTGRES_PORT'), username: configService.get('POSTGRES_USER'), password: configService.get('POSTGRES_PASSWORD'), database: configService.get('POSTGRES_DB'), entities: [PostEntity], migrations: [CreatePost1658694616973], });Once we have the migration added to the migrations array, we can run the command to execute it.npm run typeorm:run-migrations The above command yields the following logs:query: SELECT * FROM current_schema() query: SHOW server_version; query: SELECT * FROM "information_schema"."tables" WHERE "table_schema" = 'public' AND "table_name" = 'migrations' query: SELECT * FROM "migrations" "migrations" ORDER BY "id" DESC 0 migrations are already loaded in the database. 1 migrations were found in the source code. 1 migrations are new migrations must be executed. query: START TRANSACTION query: CREATE TABLE "post_entity" ("id" integer GENERATED ALWAYS AS IDENTITY NOT NULL, "title" character varying NOT NULL, "content" character varying NOT NULL, CONSTRAINT "PK_58a149c4e88bf49036bc4c8c79f" PRIMARY KEY ("id")) query: INSERT INTO "migrations"("timestamp", "name") VALUES ($1, $2) -- PARAMETERS: [1658694616973,"CreatePost1658694616973"] Migration CreatePost1658694616973 has been executed successfully. query: COMMIT Running the migration command does a few things. First, it identifies that the migrations array contains a migration that wasn’t executed yet. It runs the up method and creates the posts table. Besides that, it also adds an entry to the migrations table in the database. It indicates that the migration was executed. Reverting migrations To revert a migration, we need to use the migration:revert command.npm run typeorm:revert-migration The above command produces the following logs:query: SELECT * FROM current_schema() query: SHOW server_version; query: SELECT * FROM "information_schema"."tables" WHERE "table_schema" = 'public' AND "table_name" = 'migrations' query: SELECT * FROM "migration

Database migrations with TypeORM | NestJS.io