Popular ORMs in JavaScript

Joseph Williams
4 min readFeb 6, 2021
Photo by Glenn Carstens-Peters on Unsplash

If you’ve used Ruby on Rails then you’ve probably used ActiveRecord, and you probably loved it. I used it and I thought it was pretty great too, I got to spend less time writing SQL queries and more time doing what I love - trying to find the typo crashing my rails app. In all seriousness though ActiveRecord is a very useful tool that Rails Developers have at their disposal and it is very well integrated into the framework.

But What About Javascript?

So you’re a Javascript Developer, unfortunately ActiveRecord is only available in Rails, luckily for us there are a range of other ORMs available for JS. In this article we are going to take a look at a few of the most popular options that are available for JS Developers.

Sequelize

Sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite and Microsoft SQL Server. It features solid transaction support, relations, eager and lazy loading, read replication and more.

Photo by Michael Dziedzic on Unsplash

A quick google search for Javascript ORMs will tell you that Sequelize is one of the most popular options out there, and for good reason. Sequelize is easy to set up, open source, promised based and supports a variety of databases.

In writing this article I experimented with using Sequelize and found it to overall be a pleasant experience. It is easy to set up/use and comes with robust documentation which is definitely a big positive. For these reasons if you’ve never used a Javascript ORM before I would recommend Sequelize as a good starting point.

Mongoose

Mongoose provides a straight-forward, schema-based solution to model your application data. It includes built-in type casting, validation, query building, business logic hooks and more, out of the box.

Photo by Richard Alfonzo on Unsplash

If you’re using MongoDB then Mongoose could be the way to go. Unlike Sequelize which supports a variety of Database Management Systems Mongoose was developed specifically with MongoDB in mind.

There is a good article on StackChief about some of positives of using Mongoose with MongoDB which you can read here but to summarize some of the advantages of using Mongoose are:

  • Schema: Mongoose defines a schema for your data models so your documents follow a specific structure with pre-defined data types.
  • Validation: Mongoose has built in validation for schema definitions. This saves you from writing a bunch of validation code that you have to otherwise write with the MongoDB driver.
  • Instance Methods: 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.
  • Returning results: Mongoose makes returning updated documents or query results easier.

TypeORM

TypeORM is an ORM 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). Its goal is to always support the latest JavaScript features and provide additional features that help you to develop any kind of application that uses databases — from small applications with a few tables to large scale enterprise applications with multiple databases.

Photo by Carolyn V on Unsplash

TypeORM is an ORM increasingly growing in popularity, it takes advantage of modern JS features and also encourages the use of Typescript. Unlike other JS ORMs in existence TypeORM also supports ActiveRecord patterns so if you do you have prior experience with ActiveRecord it should be a fairly simple transition. TypeORM also provides support for a wide range of Database Management Systems including: MySQL / MariaDB / Postgres / CockroachDB / SQLite / Microsoft SQL Server / Oracle / SAP Hana / sql.js / MongoDB / NoSQL database.

TypeORM boasts a wide range of features these include:

  • Automatically create database table schemes based on your models.
  • Easily insert, update and delete object in the database.
  • Create mapping (one-to-one, one-to-many and many-to-many) between tables.
  • Provides simple CLI commands.

For a more features TypeORM provides an extensive list of features on their documentation here

Others

Photo by Sharon McCutcheon on Unsplash

Whilst the ORMs mentioned above are by quite some way the most popular currently, as with most things in Javascript we have a myriad of different options available to us. Some other notable ORMs are:

Conclusion

As we have seen in Javascript we have a range of different ORMs to choose from and although there is no real one size fits all solution. There should be an appropriate ORM for you and your teams needs. If any of the ORMs above piqued your interest I would recommend visiting the relevant documentation which you can find below:

--

--