Drizzle: A Lightweight Object Relational Mapping Tool
Introduction
Most apps in the real world rely on databases like MySQL, PostgreSQL, and SQLite to store their critical user data. However, working with raw SQL can be painful and even life-threatening when mistakes are made. Over the years, numerous libraries have been developed to abstract away SQL code and hide its complexity within your favorite object-oriented programming language. This technique is known as object relational mapping (ORM). While ORM is great, it can often introduce unnecessary performance overhead and lead to leaky abstractions because developers are unaware of how their underlying SQL code functions. Drizzle is an ORM that takes a different approach by providing an API and TypeScript that closely mirrors native SQL code. This allows for type safety and intellisense without the need for complex abstractions.
Features of Drizzle
Drizzle provides dedicated adapters for various databases, including MySQL, PostgreSQL, and SQLite, as well as tools for popular cloud hosts like RDS. It offers optional Prisma-like query APIs to keep data fetching and join code concise and readable. Additionally, Drizzle includes a CLI for handling database migrations called Drizzle Kit. The standout feature of Drizzle is Drizzle Studio, a GUI tool that can be run locally to manage your data.
Getting Started with Drizzle
To get started with Drizzle, you first need to set up a relational database. For this tutorial, we will use PostgreSQL with Neon, a popular database sponsor. Neon offers an easy-to-use platform with a generous free tier and a fast serverless driver compatible with Drizzle.
Install Drizzle and the corresponding database driver, and then connect to the database in your TypeScript code. Next, create a schema file and define tables using the PGTable
function. Each table should have a name and columns defined within a JavaScript object. Columns can have constraints specified using JavaScript functions equivalent to their SQL counterparts.
Once the tables are defined, you can establish relationships between them using foreign keys and the relations
function. This simplifies relational queries and joins within the database.
Using Drizzle in Practice
Convert your TypeScript code into PostgreSQL by running the Drizzle Kit generate
command, which generates migration files. These migration files can then be applied to Neon using the migrate
function. With the schema strongly typed, writing incorrect queries becomes impossible. Additionally, the relations defined in the schema allow for easy fetching of records based on various types of relationships.
Drizzle’s strength lies in its ability to provide type safety and intellisense without sacrificing the raw power of SQL. By closely mirroring native SQL code, developers can gain a better understanding of how their database interactions work. The optional query APIs and CLI tools make working with Drizzle a seamless experience.
Conclusion
In conclusion, Drizzle offers a lightweight and efficient approach to object relational mapping. By providing a close match to native SQL code and dedicated adapters for different databases, Drizzle allows developers to work with their favorite relational databases in a type-safe and intuitive manner. With features like Drizzle Studio and the Prisma-like query API, managing and querying data becomes a straightforward task. Give Drizzle a try in your next project and experience the benefits of type-safe object relational mapping.
Check out this video for the full tutorial https://youtu.be/hIYNOiZXQ7Y
This looks identical to Knex, aside from the GUI. Been using Knex for a project. Quite a nice tool.
When you realize Fireship's 100 seconds videos are way longer than 100 seconds
Nice. But it does look like Django with sugarcoated SQL.
I'm realizing how long it's been since I last touch Javascript…and how little I've worked with SQL. Guess it's a good of time as any to get back on the webpage design!
When are you gonna showcase some actual innovation like Deepkit?
I shouldn't say this as a Frontend Developer myself, but only a Javascript Developer could have the idea to even control a Database with Javascript. (Or Typescript, not my point)
Laravel DB facade, but for typescript. Okay
nah
"working with raw SQL can be painful". Lies. SQL is quite simple and fast to use. Why some people try to replace it with slow, overly and unnecessarily complex ORM tools is a mystery to me.
Nah
There's nothing like raw dogging some sql
New devs Drizzle is still very rough to debug use prisma. Thank me later
Hi. wats "S"Q"L" I've only herd its sequel . . .
Too bad drizzle is dead…
Actually it's a query builder library, rather then orm.
Query builders are good, orm – evil.
Isn't this mostly just a query builder?
Day 3 of not mentioning AI. You’re doing great Jeff
Interesting. But for the moment I am sticking with MikroORM, which I really recommend as an substitute for TypeORM.
Looks nice. SQLx in Rust looks nicer. Good vid as always though!
Drizzle is awesome until you need a GIN index… then all hell breaks loose
So basically Linq2DB
I don't want to be writing my backend in JS but everyone is offering free tier runtimes in JS so my small-time projects have no other home 😢
This to me looks like MongoDB with extra steps
reminds me of kanex (is that what it's called?)
jooq does the same for Java since many years
But SQL is more clear, quick and easy
The annoying part about all ORM's is backwards incompatible migrations in a zero-downtime release setup. How does this one fix that issue?
I was interested, and I then I saw “typescript” and suddenly I no longer am interested haha
Sooo… just use Prisma?
ORM will never work. it’s always going to be hell
I don't understand anything, i love the voice
Is it just me, or does anyone else think that ORMs are absolutely overrated? I believe ORMs make SQL queries more unreadable and complex. If you know a bit about relational databases, it's probably one of the easiest things in computer science. Of course, you can delve deeply into databases, but for 99% of programs, you don't need to. I think of ORMs like centering a div or exiting Vim…
gewy vs Gui, like this comment for GUI and dislike for gewy.
Prisma left the chat
But the million dollar question: Does it support native SQL joins (unlike Prisma, which does separate queries and then merges them, rather than using a SQL join).
ah yes my favourite danish word: worm
If you are using ORMs, your team is learning the wrong things. Use SQL directly instead.
Laravel almost a decade ago…
This is literally what I've been doing manually for Perl for years
Looks very similar to jOOQ, which is very cool.
There is some haunting sound in the background at 2:26 which sent shivers down my spine :O
You good Jeff?
Seeing that table definition as TEXT, kind of make me worried. I wonder if it will scale well.