ORMs do not ORM
The discourse of whether using an ORM is good is making the rounds again on Twitter. Let me introduce our contestants:
“You don't need an ORM. Just write SQL.”
VS
“You still need the code that goes around the SQL. You’re just building your own ORM.”
I think the debate comes from people not having the same mental model of what an ORM is. ORM stands for “Object Relational Mapper”.
Let’s look at a snippet in a made-up language:
class Person(id: Int, name: String)
let person = new Person(-1,“Mr Reader”)
person.save()
Now let's compare that to another snippet, writing SQL in the same made-up language:
sql.query(“INSERT INTO person (name) VALUES ($)").args(“Mr Reader”).run()
You can think of the two snippets as similar. They are definitely in the same category, in the sense that they are addressing the same problem.
If we come back to the “Object Relational Mapping”, none of them do it. A “mapping” would imply a conversion between the two worlds and you have both sides of it. And none of the two snippets do that, do they?
In the ORM snippet you don't have the sql side of things. What's the query that runs under the hood? When is it run? Are queries running in the order I write them? I wish this last question was a joke, but I have worked with an ORM that reordered queries sometimes if you didn't forceFlush
. So it tries to hide the database from you.
In the SQL snippet there's no mapping. It's up to you to take the values and put them onto the SQL. If you add a new field to your class, you won't have the compiler telling you you’re missing an argument to your query. You don't have more type-safety than in the other snippet.
So next time you engage in a debate or ORM vs no ORM, you can now be aware of what the other side is valuing.
A middle ground are query builders, that just provide a DSL for building SQL queries and code to help do the mapping. Some of them make people from both sides less annoyed and they can finally work together to bring world peace.