by sklivvz1971 · 2y ago · 0 comments
There's the not-so-common case of needing database independence as well, but at that point DB perf becomes a really hard problem to solve generally...
Sql is awesome and gives immediate access to the full raw power of your database.
It’s worth learning whilst learning some abstraction isn’t.
SQL is ultimately a bit dysfunctional and takes a lot of getting used to. Not arguing it's worth learning but since most of my career I was not truly exposed to it, I preferred the bespoke DSL.
I'll agree using ORMs is indeed going to ridiculous lengths to avoid learning even a smidgen of SQL but there's a nice middle ground.
Plenty of companies end up building the wrong product altogether and need to pivot when they realize customers don't care about feature X and need feature Y instead. In cases like these you hope you get the right product out there and survive long enough to regret building with the fast framework instead of the finely tuned ultra performant version.
I currently spend a large proportion of my time working in a Java code base that uses JDBC directly. There are many places where the complexity of the work to be done means code is being used to assemble the final SQL query based on conditionals and then the same conditional structure must be used to bind parameter values. Yes, in some places there are entire SQL statements as String literals, but that only really works for simple scenarios. There are also many bits of code that wrap up common query patterns, reimplementing some of what an ORM might bring.
I recently implemented soft deletion for one of the main entities in the system, and having to review every query for the table involved to see whether it needed the deleted_at field adding to the where clause took a significant amount of time. I think better architecture supported by a more structured query builder would have made this much easier. For me that’s the main benefit of an ORM.
ActiveRecord also does a terrible job of hiding the database or sql, even compared to other ORMs like Django's.
So the price you pay for the it doesn't even buy you that much benefit via abstraction in the first place.
What's not to understand? ActiveRecord exists to make queries easier to write and more readable - the same reason any library or tool is created.
Why don't we skip using tools like Ruby or Rails and just write everything in machine code instead? /s
It achieves only making queries much harder and much more opaque. It completely fails at that.