Migrations Over Synchronize in TypeORM

Uthpala Pitawela
The Startup
Published in
4 min readNov 7, 2020

TypeORM is an Object Relational Mapping technique which is used to connect object code to a relational database. Let’s see what is known as Object Relational Mapping(ORM).

Object Relational Mapping (ORM)

ORM is the bridge between the object code and the relational database. It is a programming technique in which a metadata descriptor is used to connect object code to a relational database.

TypeORM

TypeORM is an Object Relational Mapping technique that can run in NodeJS, Browser, Cordova, PhoneGap, Ionic, React Native, NativeScript, Expo, and Electron platforms and can be used with TypeScript and JavaScript (ES5, ES6, ES7, ES8). The goal of TypeORM is to support the latest Javascript features and provide additional features that can be used in any application that uses databases.

TypeORM supports Active Record and Data Mapper patterns.

Features of TypeORM

  • Creates data table schemes automatically based on the model
  • Can do insert/update/delete operations transparently to the database objects
  • Maps selection from tables to Javascript objects and maps table columns to object properties
  • Easily create one-to-one, many-to-one, one-to-many, and many-to-many relations between tables

Configure TypeORM

First, install TypeORM

npm install typeorm -g

Then, you can configure a typeorm project in your desired folder. Here I configure mysql as the database but you can go for one of the following as per the need; mariadb, postgres, cockroachdb, sqlite, mssql, oracle, mongodb, cordova, react-native, expxo, nativescript.

typeorm init --name <Project Name> --database mysql

As the next step go to the project folder and type;

npm install

Synchronize in TypeORM

Synchronize makes the entity sync with the database every time you run your application. Hence, whenever you add columns to an entity, create a new table by creating a new entity, or remove columns from an existing table by modifying an entity it will automatically update the database once the server is started.

Let’s see the behavior of synchronize when adding a new column to an entity.

First of all, you need to check whether the “synchronize” attribute of ormconfig.json is set to true.

This is the entity and database structure before adding the new column ‘address’.

Let’s add the new column ‘address’.

Then start the server using npm start.

Finally, you can notice that the user table got the new column ‘address’ in the user table.

Before synchronize

After synchronize

Migrations in TypeORM

Even Though synchronization is a good option to synchronize your entity with the database, it is unsafe for production databases. Therefore migrations can be an alternative solution for safer migrations in production databases.

When doing a migration, you should follow the below steps.

  1. Update the ormconfig.json file and package.json file

You should change the synchronize attribute to false in ormconfig.json as the first step to prevent schema synchronization.

Then add the following command to the scripts attribute under the package.json file.

“typeorm”: “ts-node ./node_modules/typeorm/cli -f ./ormconfig.json”

2. Generate the migration

npm run typeorm migration:generate -n <migration-name>

Here you can give a name to your migration. After you run the command you will find a migration file under migrations with the name <timestamp><migration-name>.

In the migration file, there are two functions namely up and down where up function responsible for running the migration and down for reverting the migration.

I have added the address field to the User table and generated the migration file.

3. Run the migration

npm run typeorm migration:run

This command will run the migration which you have already created in the above command. When you run this command, it will execute the up function in the migration file.

After the execution, the new address field is added to the database.

4. Revert the migration

npm run typeorm migration:revert

This command will revert the migration which you have already executed in the above command. When you run this command, it will revert all the migrations which you have already done. Basically, it will run the down command of the migration file.

After reverting the address field

Conclusion

TypeORM easily facilitates to connect object code with a relational database. Even though synchronization is a good option, migration provides a safer mechanism mostly for production databases.

--

--