DEV Community

Alex Spinov
Alex Spinov

Posted on

Drizzle ORM Has a Free API: TypeScript ORM That Feels Like Writing SQL

Why Drizzle

Drizzle ORM gives you type-safe database access that looks like SQL — not another query builder abstraction. Zero overhead, edge-ready, and generates migrations from your schema.

Install

npm install drizzle-orm postgres
npm install -D drizzle-kit
Enter fullscreen mode Exit fullscreen mode

Define Schema

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

export const users = pgTable('users', {
  id: serial('id').primaryKey(),
  name: text('name').notNull(),
  email: text('email').notNull().unique(),
  age: integer('age'),
  active: boolean('active').default(true),
  createdAt: timestamp('created_at').defaultNow(),
});

export const posts = pgTable('posts', {
  id: serial('id').primaryKey(),
  title: text('title').notNull(),
  content: text('content'),
  authorId: integer('author_id').references(() => users.id),
  createdAt: timestamp('created_at').defaultNow(),
});
Enter fullscreen mode Exit fullscreen mode

Queries (SQL-Like)

import { drizzle } from 'drizzle-orm/postgres-js';
import { eq, gt, and, desc } from 'drizzle-orm';
import postgres from 'postgres';

const db = drizzle(postgres('postgres://localhost/mydb'));

// Select
const allUsers = await db.select().from(users);
const activeUsers = await db.select().from(users).where(eq(users.active, true));

// Insert
const newUser = await db.insert(users).values({
  name: 'Alice',
  email: 'alice@example.com',
  age: 30,
}).returning();

// Join
const userPosts = await db
  .select()
  .from(users)
  .leftJoin(posts, eq(users.id, posts.authorId))
  .where(gt(users.age, 25))
  .orderBy(desc(posts.createdAt));

// Update
await db.update(users).set({ active: false }).where(eq(users.id, 1));

// Delete
await db.delete(users).where(eq(users.id, 1));
Enter fullscreen mode Exit fullscreen mode

Migrations

# Generate migration from schema changes
npx drizzle-kit generate

# Apply migrations
npx drizzle-kit migrate

# Studio (visual database browser)
npx drizzle-kit studio
Enter fullscreen mode Exit fullscreen mode

Key Features

  • SQL-like syntax — not hiding SQL, enhancing it
  • Type safe — full TypeScript inference from schema
  • Zero overhead — compiles to efficient SQL
  • Migrations — auto-generated from schema diffs
  • Drizzle Studio — visual database browser
  • Edge ready — works with Cloudflare D1, Neon, PlanetScale
  • MIT license — free

Resources


Need to extract database schemas, ORM configs, or migration data? Check out my Apify tools or email spinov001@gmail.com for custom solutions.

Top comments (0)