DEV Community

Alex Spinov
Alex Spinov

Posted on

Drizzle ORM Is What Prisma Should Have Been

SQL-Like Syntax, Full Type Safety

Drizzle ORM gives you a SQL-like API that generates perfect SQL. No query engine overhead, no N+1 surprises, no schema push headaches.

Define Schema

import { pgTable, serial, text, integer, timestamp } from "drizzle-orm/pg-core";

export const users = pgTable("users", {
  id: serial("id").primaryKey(),
  name: text("name").notNull(),
  email: text("email").unique(),
  age: integer("age"),
  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)
});
Enter fullscreen mode Exit fullscreen mode

Query

import { db } from "./db";
import { users, posts } from "./schema";
import { eq, gt, desc } from "drizzle-orm";

// Select
const allUsers = await db.select().from(users);
const adults = await db.select().from(users).where(gt(users.age, 18));

// Join
const postsWithAuthors = await db
  .select({ title: posts.title, author: users.name })
  .from(posts)
  .leftJoin(users, eq(posts.authorId, users.id))
  .orderBy(desc(posts.id))
  .limit(10);

// Insert
await db.insert(users).values({ name: "John", email: "john@example.com" });
Enter fullscreen mode Exit fullscreen mode

Why Drizzle Over Prisma

Feature Drizzle Prisma
SQL-like API Yes No (custom syntax)
Bundle size 35KB 800KB+
No query engine Yes Needs Rust binary
Raw SQL First-class Escape hatch
Migrations SQL files Custom format
Edge support Yes Limited
Serverless Native Needs accelerate

More


More from me: 10 Dev Tools I Use Daily | 77 Scrapers on a Schedule | 150+ Free APIs

Top comments (0)