Re:Ask HN: Raw SQL vs. ORM?
by timrichard on Wednesday April 11, 2018 @07:12AM
(#16809894)
Just to mention another alternative, I've been using a QueryBuilder library on the last two projects. The code's prettier than it would be with raw SQL, plus there aren't ORM downsides.
Good point. We had great success doing this with our PHP code base. For us it wasn't about pretty or not-pretty code, but rather about type safety. The idea that you can have a method which accepts "WhereClause $constraints" instead of "string $constraints" means you can be much more confident that the code is correct.
We actually wrote a custom query builder because none of the available ones (e.g. Doctrine) had the type safety that we desired. A direct parallel to what we used is the Java library JOOQ, in terms of its querybuilder API [0].
[0] https://www.jooq.org/doc/3.10/manual/sql-building/sql-statem...
Same here, we use Knex.js Querybuilder to build queries and fallback to `knex.raw()` for very specific SQL queries.
Re:Ask HN: Raw SQL vs. ORM?
by timrichard on Wednesday April 11, 2018 @07:56AM
(#16810201)
Ah, coincidence... Knex is the one I've been using too :-)
They can certainly make for cleaner looking code, and are useful for people who don't really understand the database schema, but not worth the added complexity.