Popular ORMs in JavaScript, and why you should care.
If you’re reading this, then chances are you have already heard the term ORM. For those of you who happened here by chance, an ORM is defined as ‘Object Relational Mapping’. Which in short, are an alternative method to traditional SQL queries for relational database management. Think of them as a translator from our preferred type of code to another.
This article will briefly break down the benefits of ORMs within the development process and how the most popular of these current ORM libraries can increase the efficiency of your JavaScript-based app.
A basic SQL query might look something like this;
SELECT * FROM Customers WHERE Last_Name='Thompson';
However, these queries typically increase in complexity rapidly and if not performed regularly the syntax can prove cumbersome. As developers, we are always looking for the “easiest” or most efficient way of doing things. The benefit of ORMs is that a developer can use their primary coding language to write these complex queries.
As a result, a fairly simple query like this;
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName
Can become as condensed as this single line query;
Person.joins(:country).where(countries: { gdp: 110 })
#"SELECT "people".* FROM "people"
#INNER JOIN "countires"
#ON "countries"."id" = "people"."country_id"
#WHERE "countries"."gdp" = 110"
This abstraction of SQL query complexity has many benefits for the efficiency of the production team and, as a result, ORM libraries have seen a rise in popularity over recent years.
By this stage you are certainly convinced as to the necessity of ORMs and likely asking yourself “where can I get one of these life-changing libraries?”, and “which one should I choose?”. The next section will provide a breakdown of various highly regarded JS ORMs (in no particular order);
TypeORM is an open-source tool that supports both Active Record and Data Mapper patterns. A key virtue of TypeORM and instrumental in its rapidly growing support base is its accessibility.
It currently supports a wide range of DB management systems including; MySQL, MariaDB, Postgres, CockroachDB, SQLite, Microsoft SQL Server, Oracle, SAP Hana, sql.js as well and MongoDB NoSQL database.
Furthermore, it is compatible with NodeJS, Browser, Ionic, Cordova, React Native, NativeScript, Expo, Electron platforms making it widely adopted across the development community.
It is widely regarded as the first choice for many Typescript users and can be freely downloaded from GitHub.
2. Prisma
Is another open-source database tool kit. It is an excellent option for those using TypeScript or Node.js and even has an auto-generated query builder.
It currently supports PostgreSQL, SQLite, and MySQL.
Although this may exclude it as an option for some, Prisma has excellent documentation, great support channels, and an excellent VSCode plugin.
It is available to try or download from their site, or via GitHub.
3. Sequelize
Is a well known, promise-based ORM for Node.js. with a wide variety of features. It supports a wide variety of dialects including; PostgreSQL, MySQL, SQLite, MSSQL, and MariaDB.
It has decent, easily navigable documentation and a wealth of tutorials and guides. It is also a very stable ORM and highly regarded. Amongst its positives it allows for better management of exceptions or unexpected results, and also has middleware that allows for custom error messages for each field.
Available via GitHub.
4. Bookshelf
Is both a promise based JS ORM and also works with traditional callbacks interfaces. It is built on the Knex.js interface and compatible with PostgreSQL, MySQL, and SQLite3.
Bookshelf separates itself from others by providing a simple library for common tasks when querying databases in JavaScript. Its concise, literate code base makes Bookshelf simple to read, understand, and extend.
It is available on GitHub and has a comprehensive test suite using Travis CI.
5. Mongoose
Mongoose is a NoSQL based Object Data Modelling (ODM) library for MongoDB and Node.js. It provides a straight-forward, schema-based solution to model your application data and provides a great deal of convenience when creating and managing data in MongoDB.
Mongoose supports both promises and callbacks and is designed to work in an asynchronous environment. It can be forked from GitHub.
As you can see there is a wide variety of ORMs available for JavaScript developers to leverage in the endless pursuit of an optimized workflow. It is not the purpose of this article to provide a comprehensive review of all the options available but rather to highlight the benefits of making your CRUD operations using an ORM in your preferred language.
After all, all these libraries can’t be wrong.
If you would like to continue reading about the many benefits of ORMs there are are a few suggestions below.
ORM: Rethinking Data as objects by Jaya
What is ORM by Brian Cline
A Case for ORM, a Case against ORM by Mikhail Khorpyakov