Next.js Course
Next.js
/
Advanced

Drizzle ORM

Definition

A lightweight, extremely fast, and highly popular TypeScript ORM that competes directly with Prisma by offering a more SQL-like syntax and better serverless performance.

Explain Like I'm New

Prisma is 'magic' but uses a heavy Rust engine under the hood, making it slightly slow to start up in serverless environments. Drizzle is 'lightweight'. It doesn't use a custom `.prisma` file. You define your tables using pure TypeScript. It feels exactly like writing raw SQL, but with perfect autocomplete.

Real World Example

Deploying a Next.js app to Cloudflare Pages (Edge runtime). Prisma struggles on the Edge. Drizzle runs perfectly on the Edge because it has zero heavy dependencies.

Common Use Cases

  • •Edge rendering
  • •High-performance serverless apps
  • •Developers who prefer SQL

Interactive Example

// 1. Defining the schema purely in TypeScript! (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 }),
});

// 2. Querying in Next.js
import { db } from '@/lib/db';
import { users } from '@/schema';
import { eq } from 'drizzle-orm';

export default async function Profile({ id }) {
  // Looks almost exactly like SQL, but perfectly type-safe!
  const result = await db
    .select()
    .from(users)
    .where(eq(users.id, id));
    
  return <h1>{result[0].fullName}</h1>;
}

Interview Questions

basic

  • What is the main difference in how you define your database schema in Prisma vs Drizzle?

intermediate

  • Why is Drizzle often preferred over Prisma for Edge computing platforms like Cloudflare Workers?

Flash Cards

Question

Schema definition?

Click to reveal answer
Answer

Prisma uses a proprietary, custom language in a `schema.prisma` file. Drizzle uses pure TypeScript objects in a `.ts` file.

Question

Why preferred for Edge?

Click to reveal answer
Answer

Drizzle has zero native Node.js or Rust dependencies. It is just pure JavaScript/TypeScript, making it perfectly compatible with lightweight Edge environments.