Skip to content

Drizzle ORM: Quick Reference#

Essential Drizzle ORM queries you'll use 80% of the time.

Basic Queries#

Select All#

const users = await db.select().from(usersTable);

Select One by ID#

const user = await db
  .select()
  .from(usersTable)
  .where(eq(usersTable.id, 1))
  .limit(1);

Pagination#

const users = await db
  .select()
  .from(usersTable)
  .limit(10)    // Max 10 rows
  .offset(20);  // Skip first 20 rows

Insert#

const newUser = await db
  .insert(usersTable)
  .values({ name: "Alice", email: "alice@example.com" })
  .returning();

Update#

const updated = await db
  .update(usersTable)
  .set({ name: "Bob" })
  .where(eq(usersTable.id, 1))
  .returning();

Delete#

await db
  .delete(usersTable)
  .where(eq(usersTable.id, 1));

⚠️ Important Notes#

DB-Agnostic vs DB-Specific#

Feature DB-Agnostic DB-Specific
db.select().from() ✅ Works on all -
uuid(), pgEnum(), timestamp({ withTimezone: true }) ❌ Only Postgres
Indexes / Constraints Mostly yes Sometimes DB-specific

Table Definitions are DB-Specific#

// PostgreSQL
import { pgTable, uuid, varchar, timestamp } from "drizzle-orm/pg-core";

export const usersTable = pgTable("users", {
  id: uuid("id").defaultRandom().primaryKey(),
  name: varchar("name", { length: 255 }).notNull(),
  createdAt: timestamp("created_at", { withTimezone: true }).defaultNow(),
});

// Queries work across databases, but table schemas don't!

Complete Guide#

For full Drizzle ORM documentation including setup, schema definition, and advanced queries, see the Drizzle Guide.

Next Steps#