I Would Never Use an ORM

Rate this content
Bookmark

What's an ORM? An Object-Relational Mapping tool (ORM) is a library to map a SQL table to a Class. In most cases, ORMs force the users to structure their code to have Model objects that include both data access and business logic.


Once upon a time, I did several projects using ORMs as I followed the common belief that they would simplify the development and maintenance of projects. I was wrong. ORMs are often a hurdle to overcome for the most complex part of a project.


As the next stop of my journey, I recommended people use the native languages of their databases, e.g., SQL. This works great for the most part, but it creates quite a struggle: there is a lot of boilerplate code to write that can be pretty tedious. I was wrong, again.


Today I'm presenting you Platformatic DB.

This talk has been presented at JSNation 2023, check out the latest edition of this JavaScript Conference.

FAQ

The book 'Accelerate Web Development with Fastify' is coming out on June 9th.

In MVC architecture, a model typically has three main responsibilities: managing persistency, holding data in memory, and implementing business logic.

PlatformaticDB allows writing and running custom SQL queries directly against the database, providing flexibility for complex database operations.

Query builders are favored by the speaker because they provide a helpful tool for writing database queries while avoiding the complexities and pitfalls associated with ORMs.

PlatformaticDB is a backend solution that simplifies building applications by automating route and schema setups. It is built on top of Fastify, utilizing its plugins for functionality.

The speaker suggests structuring code around the features of the system rather than adhering strictly to the MVC pattern, to better support scalability and the transition to microservices.

Yes, PlatformaticDB can be deployed on serverless platforms such as AWS Lambda, though there are considerations regarding cold starts and performance to keep in mind.

PlatformaticDB currently supports SQL databases like PostgreSQL, MariaDB, MySQL, and SQLite. Support for MongoDB and other databases may be considered in the future.

The speaker criticizes ORMs for promoting spaghetti code, being slow and problematic, and ultimately requiring custom SQL for efficiency, thus adding complexity rather than reducing it.

Matteo Collina
Matteo Collina
29 min
01 Jun, 2023

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Welcome to a talk on Object-Relational Mapping (ORM) and its potential pitfalls. The speaker discusses issues with modals and the MVC pattern, as well as the benefits of structuring code around system features instead. They introduce PlatformaticDB as a solution for easy backend development, showcasing its deployment and testing capabilities. The talk also covers integrating with Next.js, writing custom SQL queries, and the speaker's plans for future support and database compatibility.
Available in Español: Nunca usaría un ORM

1. Introduction to ORM

Short description:

Welcome to the talk on Object-Relational Mapping (ORM). ORM promises to map your database to objects cleanly, resulting in fantastic and well-organized code. However, ORM can be a big anti-pattern for your code. Let's explore why.

Hi, everyone. And welcome to the I'm not going to let you sleep talk after lunch. I don't know. Do you have good food today? Hopefully. But hopefully, you will not be sleeping in this talk. So please let's start doing a little bit of stress. You know I'm joking. I'm not that kind of speaker.

So yeah, to be honest, everything was said already. Not much to add. One thing there is a book coming out on Fastify on June 9th. So if you want to take a look, the name is Accelerate Web Development with Fastify. It's on Amazon. It's probably the top of the category at this point. And it's going like crazy. Anyway, apart from that, I haven't even have a slide for it. So I'm so good at promoting.

Anyway, this talk is about a topic that I really, really love. Object-Relational Mapping. I've been using Object-Relational Mapper for probably most of my career, or a good chunk of my career. And yeah, I don't know. So what do ORM promises you? Okay. So they have this kind of metaphor of, oh, we are going to map your database to objects very cleanly. And if you follow these rules, your code would actually be fantastic and well-organized and have no problem whatsoever. You know what happens with spaghetti when you cook it, right? Also, how many of you have used Java in your lifetime? Okay, so for all of you and for all the others, I'm sorry, do you know what happens to your application when it uses an ORM? It hibernates. Sorry, Java joke, only the people that knew Java. I've been trying to get some. I've used hibernate. I seem I'm young, but I'm not. So what do ORM do that's wrong and why it's a big anti-pattern for your code? So whenever you're writing, typically most ORM have this concept of a modal object.

2. Issues with Modals and the MVC Pattern

Short description:

Modals have three main responsibilities: managing persistency, holding data in memory, and implementing business logic. This violates the single responsibility principle. The popularity of the MVC pattern, modal-view-controller, is the reason why everyone is using modals. However, this pattern has its flaws. When coding a feature, you have three boxes: modal, view, and controller. But in reality, the view is often missing, resulting in an excessive number of models. For example, a real-world project had 2,000 models, causing a one-minute load time. ORMs have been delivering spaghetti code since the 90s.

How many of you coded a modal? Everybody, right? I don't know. Who has not ever coded a modal class in their lifetime? Great. Okay, amazing. So good for you. You have dodged a bullet.

So what's wrong with the concept of a modal? The problem with modals is that a modal typically has three main responsibilities. It manages persistency, it holds the data in memory, and it implements business logic, which sounds a little bit too much. There's this thing called single responsibility principle, that is something, one of the engineering things that went somewhat popular. So I don't know, but why is everybody doing this?

So the reason why everybody is doing this is that there's a nice pattern called the MVC pattern, modal-view-controller. What does this thing say? It's a structure to organize your application. Originally it was invented by Smalltalk to develop application on the desktop and then adapted for the front-end. Ouch.

Now what is the problem? So the problem is actually easy to follow. So you typically, when you, if you're coding your Node web app or whatever, you have three boxes. You have the modal, the view, and the controller. You have three boxes. So you want to code a feature. You have three boxes, you put the feature in one of your three boxes. OK? Good, right? Now, do we even have, do we have view still? Probably not. So we have controllers and models. Well, very quickly, you have 2,000 models in your application. It's actually very common, right? You think that 2,000 is a small number. Like, this is number from a real-world project. They had 200 engineers that are working on application with 2,000 models. And in order to have that system start, it takes one minute to load with all those codes. Why? Well, because it's a model view controller. You load all your models at the same time. It does not scale well in complexity, because you only have two things in which you reorganize your boxes. In fact, ORMs are so great that they deliver spaghetti code since the 90s. Okay? Sorry.

QnA