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.

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 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.

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

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

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

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.

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

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

It's a Jungle Out There: What's Really Going on Inside Your Node_Modules Folder
Node Congress 2022Node Congress 2022
26 min
It's a Jungle Out There: What's Really Going on Inside Your Node_Modules Folder
Top Content
The talk discusses the importance of supply chain security in the open source ecosystem, highlighting the risks of relying on open source code without proper code review. It explores the trend of supply chain attacks and the need for a new approach to detect and block malicious dependencies. The talk also introduces Socket, a tool that assesses the security of packages and provides automation and analysis to protect against malware and supply chain attacks. It emphasizes the need to prioritize security in software development and offers insights into potential solutions such as realms and Deno's command line flags.
Towards a Standard Library for JavaScript Runtimes
Node Congress 2022Node Congress 2022
34 min
Towards a Standard Library for JavaScript Runtimes
Top Content
There is a need for a standard library of APIs for JavaScript runtimes, as there are currently multiple ways to perform fundamental tasks like base64 encoding. JavaScript runtimes have historically lacked a standard library, causing friction and difficulty for developers. The idea of a small core has both benefits and drawbacks, with some runtimes abusing it to limit innovation. There is a misalignment between Node and web browsers in terms of functionality and API standards. The proposal is to involve browser developers in conversations about API standardization and to create a common standard library for JavaScript runtimes.
ESM Loaders: Enhancing Module Loading in Node.js
JSNation 2023JSNation 2023
22 min
ESM Loaders: Enhancing Module Loading in Node.js
ESM Loaders enhance module loading in Node.js by resolving URLs and reading files from the disk. Module loaders can override modules and change how they are found. Enhancing the loading phase involves loading directly from HTTP and loading TypeScript code without building it. The loader in the module URL handles URL resolution and uses fetch to fetch the source code. Loaders can be chained together to load from different sources, transform source code, and resolve URLs differently. The future of module loading enhancements is promising and simple to use.
Out of the Box Node.js Diagnostics
Node Congress 2022Node Congress 2022
34 min
Out of the Box Node.js Diagnostics
This talk covers various techniques for getting diagnostics information out of Node.js, including debugging with environment variables, handling warnings and deprecations, tracing uncaught exceptions and process exit, using the v8 inspector and dev tools, and generating diagnostic reports. The speaker also mentions areas for improvement in Node.js diagnostics and provides resources for learning and contributing. Additionally, the responsibilities of the Technical Steering Committee in the TS community are discussed.
Node.js Compatibility in Deno
Node Congress 2022Node Congress 2022
34 min
Node.js Compatibility in Deno
Deno aims to provide Node.js compatibility to make migration smoother and easier. While Deno can run apps and libraries offered for Node.js, not all are supported yet. There are trade-offs to consider, such as incompatible APIs and a less ideal developer experience. Deno is working on improving compatibility and the transition process. Efforts include porting Node.js modules, exploring a superset approach, and transparent package installation from npm.
Multithreaded Logging with Pino
JSNation Live 2021JSNation Live 2021
19 min
Multithreaded Logging with Pino
Top Content
Today's Talk is about logging with Pino, one of the fastest loggers for Node.js. Pino's speed and performance are achieved by avoiding expensive logging and optimizing event loop processing. It offers advanced features like async mode and distributed logging. The use of Worker Threads and Threadstream allows for efficient data processing. Pino.Transport enables log processing in a worker thread with various options for log destinations. The Talk concludes with a demonstration of logging output and an invitation to reach out for job opportunities.

Workshops on related topic

How to Solve Real-World Problems with Remix
Remix Conf Europe 2022Remix Conf Europe 2022
195 min
How to Solve Real-World Problems with Remix
Featured Workshop
Michael Carter
Michael Carter
- Errors? How to render and log your server and client errorsa - When to return errors vs throwb - Setup logging service like Sentry, LogRocket, and Bugsnag- Forms? How to validate and handle multi-page formsa - Use zod to validate form data in your actionb - Step through multi-page forms without losing data- Stuck? How to patch bugs or missing features in Remix so you can move ona - Use patch-package to quickly fix your Remix installb - Show tool for managing multiple patches and cherry-pick open PRs- Users? How to handle multi-tenant apps with Prismaa - Determine tenant by host or by userb - Multiple database or single database/multiple schemasc - Ensures tenant data always separate from others
Relational Database Modeling for GraphQL
GraphQL Galaxy 2020GraphQL Galaxy 2020
106 min
Relational Database Modeling for GraphQL
Top Content
WorkshopFree
Adron Hall
Adron Hall
In this workshop we'll dig deeper into data modeling. We'll start with a discussion about various database types and how they map to GraphQL. Once that groundwork is laid out, the focus will shift to specific types of databases and how to build data models that work best for GraphQL within various scenarios.
Table of contentsPart 1 - Hour 1      a. Relational Database Data Modeling      b. Comparing Relational and NoSQL Databases      c. GraphQL with the Database in mindPart 2 - Hour 2      a. Designing Relational Data Models      b. Relationship, Building MultijoinsTables      c. GraphQL & Relational Data Modeling Query Complexities
Prerequisites      a. Data modeling tool. The trainer will be using dbdiagram      b. Postgres, albeit no need to install this locally, as I'll be using a Postgres Dicker image, from Docker Hub for all examples      c. Hasura
Node.js Masterclass
Node Congress 2023Node Congress 2023
109 min
Node.js Masterclass
Top Content
Workshop
Matteo Collina
Matteo Collina
Have you ever struggled with designing and structuring your Node.js applications? Building applications that are well organised, testable and extendable is not always easy. It can often turn out to be a lot more complicated than you expect it to be. In this live event Matteo will show you how he builds Node.js applications from scratch. You’ll learn how he approaches application design, and the philosophies that he applies to create modular, maintainable and effective applications.

Level: intermediate
Build and Deploy a Backend With Fastify & Platformatic
JSNation 2023JSNation 2023
104 min
Build and Deploy a Backend With Fastify & Platformatic
WorkshopFree
Matteo Collina
Matteo Collina
Platformatic allows you to rapidly develop GraphQL and REST APIs with minimal effort. The best part is that it also allows you to unleash the full potential of Node.js and Fastify whenever you need to. You can fully customise a Platformatic application by writing your own additional features and plugins. In the workshop, we’ll cover both our Open Source modules and our Cloud offering:- Platformatic OSS (open-source software) — Tools and libraries for rapidly building robust applications with Node.js (https://oss.platformatic.dev/).- Platformatic Cloud (currently in beta) — Our hosting platform that includes features such as preview apps, built-in metrics and integration with your Git flow (https://platformatic.dev/). 
In this workshop you'll learn how to develop APIs with Fastify and deploy them to the Platformatic Cloud.
Building a Hyper Fast Web Server with Deno
JSNation Live 2021JSNation Live 2021
156 min
Building a Hyper Fast Web Server with Deno
WorkshopFree
Matt Landers
Will Johnston
2 authors
Deno 1.9 introduced a new web server API that takes advantage of Hyper, a fast and correct HTTP implementation for Rust. Using this API instead of the std/http implementation increases performance and provides support for HTTP2. In this workshop, learn how to create a web server utilizing Hyper under the hood and boost the performance for your web apps.
0 to Auth in an Hour Using NodeJS SDK
Node Congress 2023Node Congress 2023
63 min
0 to Auth in an Hour Using NodeJS SDK
WorkshopFree
Asaf Shen
Asaf Shen
Passwordless authentication may seem complex, but it is simple to add it to any app using the right tool.
We will enhance a full-stack JS application (Node.JS backend + React frontend) to authenticate users with OAuth (social login) and One Time Passwords (email), including:- User authentication - Managing user interactions, returning session / refresh JWTs- Session management and validation - Storing the session for subsequent client requests, validating / refreshing sessions
At the end of the workshop, we will also touch on another approach to code authentication using frontend Descope Flows (drag-and-drop workflows), while keeping only session validation in the backend. With this, we will also show how easy it is to enable biometrics and other passwordless authentication methods.
Table of contents- A quick intro to core authentication concepts- Coding- Why passwordless matters
Prerequisites- IDE for your choice- Node 18 or higher