DEV Community

Discussion on: ORM vs Query Builders vs Raw SQL

Collapse
 
dmfay profile image
Dian Fay • Edited

Ted Neward called O/RMs "the Vietnam [War] of computer science" nigh on fifteen years ago ("a quagmire which starts well, gets more complicated as time passes, and before long entraps its users in a commitment that has no clear demarcation point, no clear win conditions, and no clear exit strategy"). He was right then and he's still right now. People try, but it's impossible to cordon off the database entirely: it's a machine for organizing the very information your code manipulates, and you treat it as storage-and-nothing-more at your own peril.

I find query builders a little too barebones for my liking. I don't just want to generate SQL, I want to run it and operate on results; and I want to invoke and manipulate and organize around my data model through some kind of abstracting layer, the more convenient the better.

There's a happy medium, and it's treating your database like an API. You're going to write SQL at some point, but that doesn't mean you have to write all your SQL. You can generate the everyday stuff, you can make views and database functions accessible; if you've a mind to, you can even introspect the system catalogs and translate the schema into something application code can interact with. The first project I ran into that worked toward this was the Java data mapper MyBatis (then iBatis). In JavaScript, I develop my own for Postgres, MassiveJS.