ORM and ODM — A Brief Introduction

Deepanshu Dhruw
Spider R&D
Published in
5 min readNov 11, 2020
Photo by Franki Chamaki on Unsplash

If you have ever worked with databases, you will know how complex the queries can become as the project gets bigger and how difficult it can become when it comes to debugging! How do you think programmers deal with it? Do you know that simple SQL is actually not used in most of the projects in recent days? If you have developer friends, you might have heard the terms ORM and ODM! What are those? Well, you’re in the right place!

There are countless resources about these databases. So, we’re not going into the details of those in this article. Here, our main focus is understanding what is meant by ORM and ODM, and what’s the difference between the two. Let us first begin by understanding what ORM and ODM are, and then we shall discuss the differences between the two of them. So sit back on that chair and follow along!

What is ORM?

Interaction between backend code and database through an object relational mapper
Object Relational Mapper

ORM is Object Relational Mapping, basically a technique to query or perform CRUD (Create, Read, Update, Delete) operations to the database, mainly RDBMS (Relational Databases), using an object-oriented paradigm.

With the help of ORM, you don’t actually need to use SQL at all. You can directly interact with the database and perform queries in the same language you are using for your back-end code!

The structure of SQL databases consists of tables and views.

Let’s take an example to understand:
Here we are using MySQL (Open-source RDBMS) in NodeJs:

Inserting data in a MySQL table:

In the above code example, we used the SQL query to insert a single data into a table (inserted as a row in the table).

If you check the database mydb for the Users table, as you can see in the below output, the data is inserted.

Users:

But what if someone mistakenly enters a field that doesn't exist, in the SQL query?

So, let's see what happens when we change the query on line 25 of mysql.js to something like the below code:

When we run the mysql.js code with the above query we would get an error like this in the console:

A small mistake resulting in such a big error message!

The query we used in mysql.js code, is a basic one, so it's easy to debug, but the SQL queries can be complicated sometimes, hard to read, and will really be difficult to debug if we get an error. So, using an ORM (Object Relational Mapper) saves us from such disasters!

Inserting data using an ORM such as Sequelize:

Running above code, and then check the database:

Data Mapper, executing an application object into a database.

Here, using Sequelize (ORM), we created an object model for table Users, and using this model, we inserted user data using the member function create(). You may have noticed in line 18 of sequelize.js code we tried to insert a column named — imposter, and we didn't get any error while running that code as we got for mysql.js code when we changed the query, that's because Sequelize mapped all the data according to our User model and then executed the query ignoring imposter property and thus no error.

What is ODM?

ODM is Object Document Mapping. It is like an ORM for non-relational databases or distributed databases such as MongoDB, i.e., mapping an object model and NoSQL database (document databases, graph database, etc.).

Note: In document-oriented NoSQL databases, documents are stored in JSON format in the collection (a table in SQL databases is like a collection in NoSQL databases).

Let’s take some examples:
Here we are using MongoDB in NodeJs:

Inserting a document in collection Users:

After running the above code, we check the collection users in mydb database in the MongoDB shell:

If you see the code and the second document in collection users, we were able to add a field whatever this is a garbage data, we don’t want any such field in users' documents. Using an ODM takes care of fields such as the ‘imposter’ field!

Inserting document using an ODM such as Mongoose for MongoDB:

In the above code (see line 19), we tried to add a field hello which is not in our User model. After running the above code, we check the collection users in the mydb1 database in the MongoDB shell:

As we can see in the database the field hello is not inserted, it's because in our User model we don’t have such a field, and Mongoose mapped the data according to the User model to the database.

ORM vs ODM

Now that we’ve discussed each of them, let us proceed to discuss the differences between the two.

The key difference between the two is the type of database these data-mappers are used for. Suppose we have a relational database such as PostgreSQL, MySQL, etc. in that case, we will use an ORM. On the other hand, for non-relational databases such as MongoDB, Cassandra, Redis, etc. we will use an ODM to achieve the same results.

How would you decide whether you need to use a relational or non-relational database?

Photo by Brendan Church on Unsplash

Relational vs. Non-relational Databases

  1. Depends on the type of data you will be using, if your data fits comfortably in rows and columns, use a relational database. If your data needs flexible space, a non-relational database would be a better option for you to achieve your goals.
  2. The bigger the data set, the more likely a non-relational database is a better fit. Non-relational databases can store unlimited sets of data of any type and have the flexibility to change the data type. But relational databases work best when performing intensive read/write operations on small- or medium-sized data sets.

This article is published under Spider Research and Development Club, NIT Trichy on a Web Wednesday!

--

--

Deepanshu Dhruw
Spider R&D

SWE Intern @Peakflo (YC W22) • ex-SWE Intern @Hoppscotch • Github Extern (Winter)’21 @Hoppscotch