Live data from Hacker News

To ORM or Not to ORM

eli.thegreenplace.net

261–270 of 300 posts

Re: To ORM or Not to ORM

#261

Somewhere in the middle is best for me. Not quite ORM, not quite raw SQL, but a query builder. In the Node world knexjs.org fits the bill.

Shameless plug: if you're using TypeScript, checkout https://www.npmjs.com/package/@wwwouter/typed-knex I love Knex, but was missing the type safety, so I created a TypeScript wrapper around it. If you look at the code, you can see I started out as a C# developer :) It's being used in production for a few projects, but it would be great if more people started using it. Feedback is more than welcome!

Re: To ORM or Not to ORM

#262
Is there a solution in Python/Postgres where I don't have to have ORM'd tables, but that much of the boilerplate SQL CRUD-type things can be reduced? Basically just beyond writing raw sql.

Re: To ORM or Not to ORM

#263
post #257

Earlier quoted context omitted.

Around 10 years ago, a work collegue improved the ETL processing time of a Java application from minutes to a couple of seconds, by dropping hibernate and using direct JDBC instead. If all one is doing is calling createNativeQuery() all the time, and having stored procedures as well, then it is just needless fat.

If you have a trivial app that just executes a couple queries, then sure use JDBC? I do not understand your point. Most business apps are not trivial.

Who said anything about it being a trivial app?

It was an ETL stack for distributed networking monitoring, and data reporting.

Re: To ORM or Not to ORM

#264
post #13

I believe that People who hate ORMs haven't used a good ORM, so what they really hate is the tool, not the concept. I like using my ORM of choice when it's appropriate, I like just using SQL statements when it's appropriate. Choose the tool appropriate for the task.

I've used many ORM's in multiple languages and on multiple teams and have never had a good experience with any of them, once the complexity grew beyond trivial.

I've been tasked with various database optimisation tasks over the years and ORM's have always gotten in my way when it came to understanding a queries indexing and locking and improving/fixing it. I always end up just looking at the logged SQL, fixing it in SQL and then trying to convert it back to ORM code, which has always been tedious. In my experience, in multiple otherwise talented teams, ORM's seem to encourage less-than-ideal database modelling and overfetching data because its too easy to just make an ORM class do it for you rather than really thinking about your data and the data model you need. This isn't ORM's fault of course, but rather the teams using them, but its happened so frequently that I don't think "discipline" or "better developers" is a valid answer. [1] I used to hate databases and dread working with them for the above reasons.

I recently started loving databases by working with Clojure and the HugSQL library. The reason I love this approach is that Clojure isn't OO, so there's no Object mapping: I deal with plain old data instead, in whatever format I make the query return it (I can then easily transform that data further if I need) and HugSQL lets me write (composable and parameterised) raw SQL. Its fantastic and has been super productive for me. I can think in pure data and model the database how it makes sense and still have a good understanding of the indexing and locking of my queries.

[1] As jandrewrogers said: "in almost every place I've seen them used they've become a way for developers to avoid understanding how databases work, inevitably leading to inexplicable data models and poor performance. In practice, ORMs tend to end up creating crippling technical debt" and this has been my experience too. Sure, you can use an ORM well, but in reality, this seems to happen so rarely that in my opinion, ORM's are a net negative to be avoided if possible. Saying that you just gotta use them right (when few people seem to actually do so) seems like a bit of a no true scotsman argument to me.

Re: To ORM or Not to ORM

#265
post #188

Go with an ORM when your schema is still in flux but transition to raw SQL when you can the tech debt and performance overhead is just insane otherwise.

Only if you don't know how to use your ORM properly.

In my experience, this is (almost) everyone I've ever worked with, in multiple teams, with multiple ORM's, in multiple industries.

Re: To ORM or Not to ORM

#266
What a great article, this paragraph really reasonated with me:

> Any situation where complex functionality is wrapped in another layer runs the risk of increasing the overall complexity when the wrapping layer is itself complicated. This often comes along with leaky abstractions - wherin the wrapping layer can't do a perfect job wrapping the underlying functionality, and forces programmers to fight with both layers simultaneously.

I think we can all agree that a mapping layer from a relational DB to your language is needed in almost every application, so the question is whether to build it yourself or use an off-the-shelf solution.

I work at prisma.io and we've set out to make database workflows as easy as possible for developers, this includes database access (= ORM) and schema migrations.

With polyglot persistence becoming a standard pattern in modern applications, today's ORMs fall short since they only map from relational databases. Our goal at Prisma is to provide a type-safe mapping layer to all your databases at once. This will allow developers to focus on their application logic and not worry about how to access the data from the various data sources.

Re: To ORM or Not to ORM

#267

My preference has been this sort of tool: https://github.com/krisajenkins/yesql

Or HugSQL, which is similar, but actively maintained. Coupled with Clojure's data-centric focus, there's no need for a traditional ORM and you get to work directly in SQL. Yesql and Hugsql made me enjoy using databases for the first time ever.

Re: To ORM or Not to ORM

#268

An ORM is a technology that has the problem of trying to make easy things easier (CRUD) while making difficult things more difficult (complex joins, SQL lock management, indices, etc.). Why spend so much $$$$$ on a fancy database if you are going to make 70-90% of its features impossible to use?

Why spend so much $$$$$ on a fancy database You need to answer that question first. What feature do you really need from that fancy database?

Fine grained control over locks, transaction levels, JSON indexing, etc. In fact, some databases (older versions of Postgres, Oracle, etc.) do not actually have true serializability so explicit locks may be the only way to implement certain logic correctly. I picked on explicit locks since it is quite useful as well as being generally poorly supported by ORMs.

Re: To ORM or Not to ORM

#269

For everyone complaining about orm-something - take a look at elixir's Ecto. It basically allows writing sql via native code. Good for composability, type casting, stuff like that. Account |> where(active: true) |> join(:left, [a], p in Post, on: p.account_id == a.id) |> where([a, p], a.foo == "bar" or p.bar == "foo") |> group_by([a, p], a.id) |> select(...) |> limit(10) ... |> Repo.all() Plus there are changesets fo…

IMO this is the best approach for most scenarios. 9 out of 10 it's lightweight enough and powerful enough.

ORMs tries to hide the impedance mismatch, but the underlying paradigm is still relational, which always leads to a leaky abstraction.

In contrast, the query builder approach builds DSL to address relational paradigm so that programmer can work with relational paradigm better.

Re: To ORM or Not to ORM

#270

I've come to a couple conclusions, over the years. First, when you get down to it, the most-valued feature of ORMs is not the "writing queries in some language other than SQL" feature, it's the "not having to write a mess of mapping code" feature. Second, the biggest drawbacks to ORMs all derive from the "writing queries in some language other than SQL" feature. Fortunately, there are tools out there that solve the "…

I've come to the same conclusion from using a number of query and relation mapping frameworks (e.g. Rails, Spring). I've finally decided to write one that solves the problems that I have with them. It comes down to just a few things: 1. Embrace SQL 2. Handle the ON clauses for JOINs 3. Fetch relations of sets of records (eager or lazy at the call site) in batches without making N+1 queries 4. (bonus) async composabil…

Have you consider posting this as Show HN?
Post reply on HN