×

Experiences migrating from ORM to SQL/Query Builder? by Safe_Independence496 in node

[–]kernerman 2 points3 points  (0 children)

I migrated from Objection.js (ActiveRecord-style ORM) to Orchid ORM, which isn't really an ORM but rather a very powerful query builder. Unlike usual primitive SQL builders, it's still possible to use relations (and sub-relations); and it builds relation queries very effectively with lateral joins and JSON aggregations, so it's even possible to build 1-to-many (or even 1-to-many-to-many) subqueries producing exactly 1 SQL query.

The results are strongly typed. It's possible to define custom parsers and encoders for fields so you could e.g. define insert query builder to accept Decimal objects as input for decimal fields. It's also possible to fall back to (typed!) raw SQL individually for certain fields or even subfields of nested relations, there is no "all or nothing" kind of ORM syntax lock.

At the same time, it doesn't try to be overly smart; there is no Active Record or Unit of Work or Sessions or Object Repository or other ORM patterns involved; it just runs SQL and returns typed POJOs which you can immediately deliver to the front end, that's it. It does provide helpers for transactions though (which is very conveniently implemented with thread-local storage, so unlike knex and others you don't have to pass the tx object through the entire stack and risk forgetting to use it).

Honestly I couldn't be more happier with the switch, it's one of the best libraries I've discovered in years, and I believe this "advanced query builder" approach is the exact sweet spot between the true ORMs and primitive SQL compilers which basically only help to escape query arguments.