Object Relational Mapping

Erica M
3 min readJul 6, 2019

--

Simplify Your Life with ORM

From the moment I saw my first raw SQL query, I knew we weren’t going to get along. I’m a JavaScript developer, so the syntax of SQL just didn’t sit well with me. As a full-stack developer, RDBMs (relational database management systems) are something I have to interact with frequently, so I brace for the impending discomfort of writing SQL.

But then along come ORMs to save the day (and also my time and my sanity). ORM is an acronym for object relational mapping, which basically means that we are given a way of interacting with an RDBMs in an object-oriented manner. ORMs essentially give us a virtual object database, which is much easier for a JavaScript developer to deal with. ORMs provide us with another layer of abstraction so we don’t have to understand the underlying architecture. ORMs, as a tool, allow us to interact with our database using our preferred language instead of SQL.

object relational mapping

Pretty great, right? But there are actually even more great things about ORMs that you should know about. ORMs utilize models which helps us to keep our code DRY (don’t repeat yourself). You only have to write your model once, making it far easier to update and maintain. Additionally, this means that we can extend and inherit from our models.

Not only is querying made easier, but ORMs do a lot of other things for as well to make handling the database easier including handling the underlying database connections. Another great benefit is that because ORMs abstract away the database system, switching from an RDBM like MySQL to PostgreSQL is infinitely simpler.

On top of all that, depending on the particular ORMs, you can get a lot of additional features and support. And finally, many of the queries that you write with an ORM will perform better than if you wrote them yourself. But this final point comes with a caveat.

The caveat is that if you’re actually a really proficient SQL developer and experience in writing SQL queries, you will probably get more performant queries just writing them yourself and not using an ORM. That said, a full-stack developer is probably not going to have the time to get too acquainted with writing raw SQL.

There are a few more drawbacks to using an ORM as well. Certain more complex queries can’t even be performed with an ORM. While ORMs provide users with the option of inserting SQL queries, this still means you have to have knowledge to navigate with both the ORM and SQL.

Probably the biggest drawback, however, is that it can rob a developer of the ability to really understand databases. For any developer, it is important to understand what is happening under the hood and using an ORM avoids this. This could potentially make you a weaker developer in the long run.

SQL query vs. ORM query

But at the end of the day, especially if you’re pressed for time, an ORM is a great tool to use. It will make your code more readable and succinct, as you can see from the code above. The structure and syntax of the bottom query is much more familiar to a JavaScript developer. Of course, for larger applications, raw SQL and a developer experience in using SQL is the way to go, but if that’s not your need, give an ORM a try. You might find that you enjoy something that you though you hate. A few popular ORMs to look out for are Loopback, Waterline, Mongoose (for use with the NoSQL MongoDB), TypeORM(for use with Typescript, and last but not least, Sequelize. So npm install and give one a go.

--

--