Object-Relational Mapping (ORM)

Customer Data Analytics
Scraping Valuable Data For Customer Data Analytics
22nd May 2021
vuetify_xpertlab
What is Vuetify?
25th May 2021
Customer Data Analytics
Scraping Valuable Data For Customer Data Analytics
22nd May 2021
vuetify_xpertlab
What is Vuetify?
25th May 2021

As a relative newcomer to the programming world, terms like Object-Relational-Mapper can sound really intimidating. The nice part about ORMs, is that they actually make it easier to write code once you get the hang of them (usually).

In this post, I aim to explain what an ORM does, as well as the pros and cons of using them in your projects.

Let’s get to it!

What is an ORM?

Before we talk about what an Object-Relational-Mapper is, it might be better to talk about Object-Relational-Mapping as a concept first.

Unless you’ve worked exclusively with NoSQL databases, you’ve likely written your fair share of SQL queries. They usually look something like this:

SELECT * FROM users WHERE email = 'test@test.com';

Object-relational-mapping is the idea of being able to write queries like the one above, as well as much more complicated ones, using the object-oriented paradigm of your preferred programming language.

Long story short, we are trying to interact with our database using our language of choice instead of SQL.

Here’s where the Object-relational-mapper comes in. When most people say “ORM” they are referring to a library that implements this technique. For example, the above query would now look something like this:

var orm = require('generic-orm-libarry');var user = orm("users").where({ email: 'test@test.com' });

As you can see, we are using an imaginary ORM library to execute the exact same query, except we can write it in JavaScript (or whatever language you’re using). We can use the same languages we know and love, and also abstract away some of the complexity of interfacing with a database.

As with any technique, there are tradeoffs that should be considered when using an ORM.

Let’s take a look at some of the pros and cons!

What are some pros of using an ORM? 👍

  1. You get to write in the language you are already using anyway. If we’re being honest, we probably aren’t the greatest at writing SQL statements. SQL is a ridiculously powerful language, but most of us don’t write in it often. We do, however, tend to be much more fluent in one language or another and being able to leverage that fluency is awesome!
  2. It abstracts away the database system so that switching from MySQL to PostgreSQL, or whatever flavor you prefer, is easy-peasy.
  3. Depending on the ORM you get a lot of advanced features out of the box, such as support for transactions, connection pooling, migrations, seeds, streams, and all sorts of other goodies.
  4. Many of the queries you write will perform better than if you wrote them yourself.

What are some cons of using an ORM? 👎

  1. If you are a master at SQL, you can probably get more performant queries by writing them yourself.
  2. There is overhead involved in learning how to use any given ORM.
  3. The initial configuration of an ORM can be a headache.
  4. As a developer, it is important to understand what is happening under the hood. Since ORMs can serve as a crutch to avoid understanding databases and SQL, it can make you a weaker developer in that portion of the stack.

What are some popular ORMs?

Wikipedia has a great list of ORMs that exist for just about any language. That list is missing JavaScript, which is my language of choice, so I will throw my hat in the ring for Knex.js.

They’re not paying me to say that, I’ve simply enjoyed working with their software and I don’t have any experience with other JavaScript ORMs. This article might provide more insightful feedback for JavaScript specifically.

How to learn about ORM?

Well, use one. Whichever ORM library you choose, they all use the same principles. There are a lot of ORM libraries around here:

If you want to try an ORM library in Web programming, you’d be better off using an entire framework stack like:

  • Symfony (PHP, using Propel or Doctrine).
  • Django (Python, using a internal ORM).

Do not try to write your own ORM, unless you are trying to learn something. This is a gigantic piece of work, and the old ones took a lot of time and work before they became reliable.