Sequelize, TypeORM, and more?
Photo by Kimberly Farmer on Unsplash
ORM (Object-Relational Mapping) is a technique that helps you to query and manipulate data from databases using an object-oriented paradigm. Nowadays developers like to use ORMs due to several reasons like,
- Since we have to write a data model only in one place it is easier to update, maintain, and reuse the code.
- Forces you to write MVC code, which makes your code cleaner.
- No need for writing SQL queries.
- No need for database-level changes frequently.
- Most of the work is automated.
But there are a few disadvantages of ORMs as well.
- Performance issue with complex queries.
- Integration and learning.
Photo by Alvaro Reyes on Unsplash
Sequelize is a well known promise-based Node.js ORM, which can be used with MySQL, MariaDB, SQLite, and Microsoft SQL Server and Postgres. (being a promise-based ORM means that it supports NodeJS promises). Sequelize provides a large set of features to developers and this library has been more famous because of those features.
We can list down a few main advantages/features of Sequelize as follows.
- Promise based
- Supporting many database systems.
- Transaction support
- Database synchronization
- Database migrations
- Model validations
- Raw queries
- Data seeding
- Eager relations
- Easy to test
As you can see there are many pros of using Sequelize as your ORM and it contains rich documentation as well. So, If you are looking for a stable ORM for your project sequelize is one of the best out of all.
Photo by Jeremy Thomas on Unsplash
- It can run in NodeJS, Browser, Cordova, PhoneGap, Ionic, React Native, NativeScript, Expo, and Electron platforms.
- Using multiple database connections.
- Connection configuration in JSON / XML / YML / ENV formats.
- Eager and lazy relations.
- Uni-directional, bi-directional and self-referenced relations.
- Connection pooling.
- Entity manager
- Elegant-syntax, flexible, and powerful QueryBuilder.
You can find a full feature list of TypeORM by reading their documentation and I must state that if you are a TypeScript lover this is the most suitable ORM for you.
Photo by Pawe? Czerwi?ski on Unsplash
I?m sure every one of you has heard or worked with Mongo DB, which is a document-based NoSQL database system. Mongoose is an object modelling tool that is specifically designed for Mongo DB. So this is pretty much different from Sequelize and TypeORM since they supported more than one database management system.
However, Mongo DB is designed to work in an asynchronous environment and it supports both promises and callbacks as well. It is not required to use mongoose with Mongo DB all the time but there are some benefits of using it with Mongo DB.
- Mongoose defines a schema for your data models so your documents follow a specific structure with pre-defined data types.
- Mongoose has built-in validation for schema definitions.
- Mongoose provides optional pre and post save operations for data models. This makes it easy to define hooks and custom functionality on successful reads/writes etc.
- Mongoose makes returning updated documents or query results easier.
On the other hand, using mongoose is not recommended when it comes to advanced queries. In such cases, it is not only hard to write queries using mongoose but also it may reduce the performance of the program as well. If you are using Mongo DB, then why don’t you give a shot with mongoose?
Photo by Aleksei Ieshkin on Unsplash
Some features of Bookshelf are,
- Supports promises.
- Supports callbacks.
- Transaction support.
- Eager/nested-eager relation loading.
- Polymorphic associations
- Support for one-to-one, one-to-many, and many-to-many relations
Photo by Jeff Fielitz on Unsplash
Objection JS is also another rising ORM for NodeJS which helps you to utilize the full power of SQL and its database engine with ease. It is more accurate to introduce objection as a relational query builder. Objection provides all the benefits of an SQL query builder but also a powerful set of tools for working with relations. Same as the bookshelf, objection is also built on the Knex SQL query builder and it supports PostgreSQL, MySQL, and SQLite3 database management systems.
We can point out some specialties of objection as follows,
- Official typescript support.
- Optional JSON schema validation.
- Simple methods to fetch, insert, update, and delete objects using the power of SQL.
- Easy to use transactions.
- Automatic database schema creation and migration from model definitions.
- A fully object-oriented view of your database.
This is just a selection of very useful libraries among many others. If you are interested in using ORMs in your projects, it will be a great advantage for you to be familiar with at least a few important libraries like these ones.
A note In Plain English
Did you know that we have four publications and a YouTube channel? You can find all of this from our homepage at plainenglish.io ? show some love by giving our publications a follow and subscribing to our YouTube channel!