Programming
Drizzle ORM: Modern TypeScript ORM
12 ديسمبر 202510 min read
Learn Drizzle ORM, the lightweight Prisma alternative.
Why Drizzle?
Drizzle is lightweight and type-safe. Produces readable SQL, faster than Prisma, no code generation needed.
Schema Definition
import { pgTable, serial, text, timestamp } from 'drizzle-orm/pg-core';
export const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
email: text('email').unique(),
createdAt: timestamp('created_at').defaultNow(),
});
export const posts = pgTable('posts', {
id: serial('id').primaryKey(),
title: text('title').notNull(),
authorId: integer('author_id').references(() => users.id),
});
Query Examples
import { db } from './db';
import { users, posts } from './schema';
import { eq } from 'drizzle-orm';
// Insert
const user = await db.insert(users)
.values({ name: 'John', email: 'j@e.com' })
.returning();
// Select
const allUsers = await db.select().from(users);
const user = await db.select().from(users).where(eq(users.id, 1));
// Join
const usersWithPosts = await db.select()
.from(users)
.leftJoin(posts, eq(users.id, posts.authorId));
Migrations
npx drizzle-kit generate:pg
npx drizzle-kit push:pg
Conclusion
Drizzle is excellent for SQL-lovers. Lightweight, fast, and flexible alternative to Prisma.
Tags
#Drizzle#ORM#TypeScript#Database#SQL