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!
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!
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.
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:
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.