Drizzle ORM: A Headless TypeScript ORM for Modern Web Development

Summary
Drizzle ORM is a modern, lightweight, and headless TypeScript ORM designed for NodeJS, TypeScript, and JavaScript environments. It offers robust support for various SQL databases, including serverless options, and emphasizes type-safety and extensibility. With over 31,000 stars, Drizzle ORM is a popular choice for developers seeking a performant and flexible solution for their data interactions.
Repository Info
Tags
Click on any tag to explore related repositories
Introduction
Drizzle ORM is a powerful and lightweight headless ORM built for TypeScript and JavaScript. It provides a modern approach to interacting with SQL databases, focusing on performance, type-safety, and developer experience. Supporting PostgreSQL, MySQL, and SQLite, Drizzle ORM is designed to work seamlessly across various environments, including serverless functions and edge runtimes. Its zero-dependency architecture and tree-shakeable design make it an efficient choice for any project.
Installation
Getting started with Drizzle ORM is straightforward. You can install the core package using npm or yarn:
npm install drizzle-orm
# or
yarn add drizzle-orm
For database migrations and schema management, it's recommended to also install drizzle-kit:
npm install drizzle-kit
# or
yarn add drizzle-kit
Examples
Drizzle ORM allows you to define your SQL schemas using TypeScript and build both relational and SQL-like queries. Here's a basic example of defining a schema and performing a simple select query:
// schema.ts
import { pgTable, serial, text, varchar } from 'drizzle-orm/pg-core';
export const users = pgTable('users', {
id: serial('id').primaryKey(),
fullName: text('full_name'),
phone: varchar('phone', { length: 256 }),
});
// query.ts
import { eq } from 'drizzle-orm';
import { db } from './db'; // Assume db is an initialized Drizzle client
import { users } from './schema';
async function getUserById(userId: number) {
const result = await db.select().from(users).where(eq(users.id, userId));
console.log(result);
}
getUserById(1);
Why use Drizzle ORM?
Drizzle ORM stands out for several reasons:
- Lightweight and Zero Dependencies: At only ~7.4kb minified+gzipped, it's highly efficient with no external dependencies.
- Serverless-Ready: Designed to work in all major JavaScript runtimes, including NodeJS, Bun, Deno, and Cloudflare Workers, making it ideal for serverless architectures.
- Broad Database Support: Compatible with every PostgreSQL, MySQL, and SQLite database, including popular serverless providers like Turso, Neon, PlanetScale, and Vercel Postgres.
- Type-Safe and Extensible: Offers a robust TypeScript experience for declaring SQL schemas and building queries, balancing type-safety with flexibility.
- Powerful Ecosystem: Comes with
Drizzle Kitfor hassle-free migrations andDrizzle Studiofor effortless data browsing and manipulation. - Performance: Engineered for speed, allowing developers to build fast applications without introducing data proxies.