ORM or Object Relation Mapping is a process of mapping between relational database systems and objects. It is a translator between the relational representation of your data and objects in the code.
If you are currently working with TypeORM and want to start using Sequelize, this article is for you.
What is typeORM
TypeORM is an advanced object-relations-management module that can be run in NodeJS, React Native, Cordova, Browser and can be used with JavaScript (ES5, ES6, ES7, ES8) and TypeScript.
It was made to support the latest JavaScript features while providing some additional ones to help you develop any kind of application with databases.
TypeORM supports Data Mapper and Active Record patterns - unlike other JavaScript ORMs. Thanks to it you can write high quality, scalable and maintainable applications in the most productive way. TypeORM is influenced by other ORMs like Hibernate, Entity Framework and Doctrine.
TypeORM features:
- written in TypeScript - this means it has great out of the box support for typings, which includes decorators
- support for Repository and ActiveRecord patterns
- support for Entities referencing database views
- this is super helpful when creating applications that make use of categories - this makes it much easier for developers to build such a feature. Also, there are several solutions for tree handling so you can pick best option for your case
- Generating migrations based on changes in code
- Database relations management - although very poor when it comes to conditionally fetching relations. Even slightly complex queries require using query builder and joining tables “by hand”
- Query builder allowing sub-queries - this allows performing more complex queries
What is Sequelize
Sequelize is an ORM for NodeJS providing access to MySQL, MariaDB, PostgreSQL or SQLite databases. It uses JavaScript syntax and is easy to write and read.
Sequelize is a quite young ORM with all the necessary features and is constantly developed. It has good support for database synchronization, associations, transactions and migrations. Sequelize is easy to test with frameworks like Mocha.
Sequelize features
- has TypeScript support, but with lots of manual typing. Luckily there’s sequelize-typescript that adds decorator support and reduces the amount of code required
- great soft-delete feature
- easy to configure (requiring basically 1 parameter in the model definition) and very easy to use (WHERE conditions for deleted records are always automatically applied - even when fetching relations)
- support for defining and using relations - you can add multiple conditionals that are relation-specific, add and remove fields.
- support for sub-queries, but no query builder, so you have to write everything by hand
Why we made the switch
Long story short: we wanted to use ActiveRecord pattern and ORM APIs as much as possible, but it was impossible to write our complex queries without query builder in TypeORM. This resulted in large, unreadable query-builder objects - these objects were defeating the purpose of using ActiveRecord. Apart from that, TypeORM documentation is lacking in several places.
Our application had a vast number of many-to-many relationships. We needed to restrict fetching the given resource based on relation values. With TypeORM for that case, we had to create a custom query using query builder, which resulted in writing lots of leftJoin(), innerJoin() etc. by hand.
With Sequelize we were able to utilize rich finders API - instead of coding our joins, we were utilizing relationships defined in the model, referencing them by name and applying all conditionals as method parameters.
Finders API also allows more complex operations like findOrCreate or findOrBuild which simplifies application codebase by getting rid of checking find result.
Sequelize is much more mature and a safer option to choose in the long-term (at least at the moment). It has twice as much weekly and monthly downloads as TypeORM, there are more contributors and releases are more frequent.
Besides, even TypeORM docs page isn’t quite up to date, as roadmap says the release 1.0.0 is planned for Autumn 2018 (and it’s May 2020 already).
It was also a nice opportunity to refactor and clean-up our queries and server responses during the migration process.
What was challenging
In the end, we used Sequelize with sequelize-cli and sequelize-typescript.
We faced a few challenges along the way with rewriting the codebase and setting up the environment for new packages.
When rewriting the codebase we had to make sure to create model definitions to use new decorators. Apart from that, we created migrations to use the new query interface and factories to use new models for tests.
We set the environment with two steps:
- the first one was about setting up sequelize-cli to accept TypeScript syntax through ts-node (I can provide more technical details if needed)
- and the second one to set up sequelize-cli to pick up .ts files when running migrations (by default it only accepts .js files - again, can provide codebase)
There is no possibility to build sub-queries with query-builder, so to keep code clean we started creating database views. This however raised another small issue: there is no option to mark that Model is read-only / it references database view. In tests, we were truncating every table in the database. Because of these two reasons combined we had to filter out models referencing view tables because these cannot be truncated.
We were not using Tree Entities from TypeORM so the switch was very easy because we didn’t have to re-create its behaviours.
Summary
All in all, Sequelize is considered to be an easy-to-use ORM for Node.js while TypeORM is an ORm that can run in NodeJS and others. Sequelize belongs to Object Rational Mappers category while TypeORM can be primarily classified as a Microframework.
TypeORM supports Active Record and Data Mapper patterns unlike other JavaScript ORMs allowing you to write high quality, scalable and maintainable applications productively.
Sequelize has solid relations, read replications and transaction support and appears to be more popular among GitHub users.
If you are looking for help with your app development, contact our custom software development teams.