Drizzle ORM in 100 Seconds

Fireship
29 Feb 202402:54

Summary

TLDRDrizzle is a lightweight set of tools that enables type-safe object-relational mapping with databases like MySQL and PostgreSQL. It provides an API and TypeScript that closely matches the underlying SQL code, delivering type safety without abstraction overhead. Dedicated adapters for each database allow Drizzle to connect applications to cloud providers like RDS and Neon. An optional Prisma-like query API keeps data access concise. Drizzle Kit handles migrations. Drizzle Studio offers a GUI for local data management. The video demonstrates using Drizzle and Neon to define a schema, generate migrations, insert seed data, and run type-safe queries.

Takeaways

  • 😀 Drizzle is an ORM that provides type safety without abstraction by closely matching the native SQL
  • 💡It has dedicated adapters for different databases like PostgreSQL, MySQL etc
  • 🌟It works with popular cloud hosts like RDS and Neon
  • 👍It has an optional Prisma-like query API for concise data fetching
  • 🔨It has a CLI called Drizzle Kit for database migrations
  • 🎨It has Drizzle Studio GUI to manage data
  • 🚀It's designed to run on Node.js, Bun, Deno and runtimes like Cloudflare Workers
  • 🔌To use, install Drizzle, a database driver and connect to the DB
  • ✏️Define schema using PG table(), columns and relations()
  • 💾Generate migrations from schema to apply to the database

Q & A

  • What problem does Drizzle aim to solve?

    -Drizzle aims to solve the problem of complexity and performance overhead with traditional object-relational mapping (ORM) libraries when working with SQL databases.

  • How does Drizzle provide a simple yet powerful API?

    -Drizzle provides dedicated adapters for each database, as well as type safety and intellisense, without heavy abstractions on top of the native SQL.

  • What databases does Drizzle support?

    -Drizzle supports popular relational databases like MySQL, PostgreSQL, and SQLite.

  • What tools does Drizzle offer in addition to the core library?

    -Drizzle offers optional tools like a Prisma-like query API, a CLI for database migrations called Drizzle Kit, and Drizzle Studio for data management.

  • What JavaScript runtimes can you use Drizzle with?

    -Drizzle can run on Node.js, Bun, Dino, and edge runtimes like Cloudflare Workers.

  • How do you define the database schema with Drizzle?

    -The database schema is defined using TypeScript code and functions like pgTable() and relations() to set up tables, columns, constraints, and relationships.

  • What does the drizzle-kit generate command do?

    -The drizzle-kit generate command converts the TypeScript schema definitions into SQL migration files that can be applied to the database.

  • What makes Neon a good database choice to use with Drizzle?

    -Neon is easy to use, has a generous free tier, provides a fast serverless database driver, and has a dedicated Drizzle adapter and tutorial.

  • What are some key benefits of using Drizzle?

    -Key benefits are type safety, intellisense, avoiding ORM abstraction drawbacks, dedicated adapters for each database, and optional tools like Drizzle Studio.

  • What is required to get started with Drizzle?

    -To get started you need to install Drizzle and a compatible database driver, connect to the database, define a schema, run migrations to set up tables, and then start executing queries.

Outlines

00:00

😀 Introducing drizzle - a lightweight ORM for relational databases

Paragraph 1 introduces drizzle, a lightweight object-relational mapping (ORM) tool that makes working with relational databases like MySQL, PostgreSQL, and SQLite simpler and more type-safe. It provides closely matched APIs and TypeScript to the underlying SQL code for benefits like intellisense without unnecessary abstractions. It has adapters for different databases and tools for cloud hosts like RDS and Fly.io. It also offers optional query APIs and a CLI for migrations and data management.

Mindmap

Keywords

💡object relational mapping

Object relational mapping (ORM) is a technique in programming to convert data between an object oriented system and a relational database system. The video says most real-world apps rely on databases like MySQL and use ORM to abstract away complex SQL code. Drizzle provides a type-safe ORM API to match the underlying SQL without unnecessary abstractions.

💡type safety

Type safety in programming languages ensures variable values match their declared data types. The video says Drizzle provides type safety through its API so developers can write queries with confidence without runtime errors.

💡intellisense

Intellisense is a coding assistance feature that provides smart completions and suggestions as you type. The video says Drizzle gives you intellisense benefits to make writing queries easier without extra abstractions on top of SQL.

💡schema

A database schema defines the structure and layout for tables and data. The video shows defining a schema in Drizzle using PG table functions to set up tables, columns, constraints before inserting data.

💡relations

Relations refer to associations between database tables linking records based on foreign keys. The video says defining relations in Drizzle schemas simplifies writing relational queries and joins.

💡migrations

Migrations refer to changes made to the database schema over time. Drizzle Kit is a CLI tool that handles schema migrations by generating SQL scripts to update the database.

💡drizzle Studio

Drizzle Studio is a GUI tool for visually managing data. The video says it is used to locally manage your database in conjunction with the Drizzle ORM and query API.

💡Neon

Neon is a sponsor mentioned that provides an easy to use PostgresSQL database. The video uses a Neon database with a Drizzle adapter to demonstrate queries without needing local Postgres.

💡serverless

Serverless refers to cloud services that abstract infrastructure for you. Neon offers a fast serverless Postgres driver compatible with the Drizzle ORM.

💡Cloudflare Workers

Cloudflare Workers is an edge computing platform for running code. The video says Drizzle & Neon both run on Workers for low-latency database access.

Highlights

Drizzle is an ORM that provides a SQL-like API and TypeScript types without complex abstractions

Instead of a single API, Drizzle has dedicated adapters for each database along with tools for popular cloud hosts

It also provides an optional Prisma-like query API for concise data fetching and joins

Drizzle Studio is a GUI for managing your data locally

You'll first need a relational database like Neon, which has a generous free tier

Define schema tables with PG table function - columns get SQL constraints via JS functions

References between tables create relationships with foreign keys

Relations function simplifies relational queries and joins

Drizzle kit generate creates migration files to apply schema to the database

Strongly typed schema prevents incorrect queries

Query API handles one-to-one, one-to-many and many-to-many relationships

Check out Neon's Drizzle tutorial to learn more

Drizzle solves object-relational mapping without unnecessary overhead or leaky abstractions

Keeps data access code type-safe with intellisense while matching the underlying SQL

Optional higher-level abstractions keep application code concise and readable

Transcripts

play00:00

drizzle om a lightweight set of tools

play00:02

that makes type safe object relational

play00:04

mapping with your favorite relational

play00:06

database fun again most apps in the real

play00:08

world rely on databases like MySQL

play00:11

postgressql and SQL light to store their

play00:13

critical user data but working with raw

play00:15

SQL can be painful and even

play00:17

life-threatening when you screw up over

play00:19

the decades hundreds of libraries have

play00:20

been built to abstract away SQL code to

play00:23

hide its complexity inside your favorite

play00:25

objectoriented programming language this

play00:27

technique is called object relational

play00:29

mapping it's great and null but can

play00:31

often bring unnecessary performance

play00:32

overhead and cause leaky abstractions

play00:35

because the developers have no idea how

play00:36

their underlying SQL code actually works

play00:39

drizzle is an omm that takes the

play00:41

opposite approach it provides an API and

play00:42

typescript that closely matches the

play00:44

native underly SQL code this gives you

play00:47

the benefits of type safety and

play00:48

intellisense without all the crazy

play00:50

abstractions but how is that possible

play00:52

when every database is different instead

play00:54

of trying to jam everything into a

play00:55

single API drizzle provides dedicated

play00:57

adapters for each one of them along with

play00:59

dedicated tools for popular Cloud hosts

play01:01

like RDS and neon but if this code looks

play01:04

too raw and uncensored for you it also

play01:06

provides an optional Prisma likee query

play01:08

API that keeps your data fetching and

play01:10

join code concise and readable in

play01:12

addition it provides an optional CLI for

play01:14

handling database migrations called

play01:16

drizzle kit but the cherry on top is

play01:18

drizzle Studio A guey you can run

play01:20

locally to manage your data to get

play01:21

started you'll first need a relational

play01:23

database I'm going with neon my go-to

play01:25

pick for a postgress database and the

play01:27

sponsor of today's video neon is awesome

play01:29

because it's easy to use there's a

play01:31

generous free tier and it even has a

play01:33

fast serverless driver with a drizzle

play01:35

adapter you don't even need to install

play01:36

postgress locally just create a new

play01:38

database on the dashboard both drizzle

play01:40

and neon are designed to run on any

play01:42

JavaScript runtime like node.js bun and

play01:44

Dino as well as Edge run times like

play01:46

cloudflare workers now install drizzle

play01:48

and your database driver then connect to

play01:50

the database in your typescript code we

play01:52

could start making queries now but we

play01:53

don't have any data yet first we need a

play01:55

schema create a schema file then Define

play01:57

tables with the PG table function each

play01:59

table table has a name followed by

play02:01

columns defined in this JavaScript

play02:03

object columns are given constraints

play02:04

with JavaScript functions that match

play02:06

their SQL equivalents once a table is

play02:08

defined we can reference it in other

play02:10

tables to create relationships with

play02:12

foreign keys and optionally we can use

play02:14

the relations function to simplify

play02:16

relational queries and Joints now let's

play02:18

convert this typescript code into

play02:19

postgressql by running the drizzle kit

play02:22

generate command that creates a

play02:23

directory with our migration files which

play02:25

we can then apply to Neon with this

play02:27

migrate function and now we can use

play02:28

drizzle SQL like a to insert data into

play02:31

the database and because the schema is

play02:33

strongly typed it's impossible to write

play02:35

incorrect queries in addition because we

play02:37

also created a relation in our schema we

play02:39

can use the query API to fetch records

play02:41

based on one to one one to many or many

play02:43

to many relationships this has been

play02:45

drizzle in 100 seconds shout out to Neon

play02:47

for making this video possible and check

play02:49

out their drizzle tutorial to learn more

play02:51

thanks for watching and I will see you

play02:53

in the next one