Drizzle ORM has rapidly become the preferred choice for TypeScript developers who want type-safe database access without the overhead of traditional ORMs.
Why Drizzle?
- Zero runtime overhead: Compiles to plain SQL
- Type-safe: Full TypeScript inference
- SQL-like syntax: Familiar to SQL developers
- Lightweight: Minimal dependencies
Defining Schemas
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().notNull(),
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),
});
Queries
// Select with relations
const result = await db
.select()
.from(users)
.leftJoin(posts, eq(posts.authorId, users.id))
.where(eq(users.name, "Edrees"));
// Insert
await db.insert(users).values({ name: "John", email: "john@example.com" });
// Update
await db.update(users).set({ name: "Jane" }).where(eq(users.id, 1));
Drizzle vs Prisma
| Feature | Drizzle | Prisma |
|---|---|---|
| Bundle Size | ~50KB | ~2MB |
| SQL Control | Full | Limited |
| Learning Curve | SQL-like | Custom DSL |
Comments (0)
Leave a Comment
No comments yet. Be the first to share your thoughts!