Full Stack 35:502025-07-14
Drizzle ORM: The Type-Safe Alternative to Prisma
Drizzle gives you the type safety of Prisma with the control of raw SQL. Learn schema definition, migrations, relationships, and complex queries in this complete guide.
Watch on YouTube
Click to open in YouTube
Video Notes & Code
Key takeaways, code snippets, and resources from this video.
Schema Definition
typescript
// db/schema.ts
import { pgTable, text, timestamp, uuid, integer } from 'drizzle-orm/pg-core'
export const users = pgTable('users', {
id: uuid('id').primaryKey().defaultRandom(),
name: text('name').notNull(),
email: text('email').notNull().unique(),
createdAt: timestamp('created_at').defaultNow(),
})
export const posts = pgTable('posts', {
id: uuid('id').primaryKey().defaultRandom(),
userId: uuid('user_id').references(() => users.id, { onDelete: 'cascade' }),
title: text('title').notNull(),
views: integer('views').default(0),
})Queries with Type Inference
typescript
import { db } from '@/db'
import { users, posts } from '@/db/schema'
import { eq, desc, count } from 'drizzle-orm'
// Fully typed — InferSelectModel infers the return type
const topUsers = await db
.select({ user: users, postCount: count(posts.id) })
.from(users)
.leftJoin(posts, eq(posts.userId, users.id))
.groupBy(users.id)
.orderBy(desc(count(posts.id)))
.limit(10)Found this useful?
Subscribe for weekly content on AI engineering, SaaS building, and full stack development.