ORMs for Node, Prisma and Drizzle

Intermediate · 20 min

How Prisma and Drizzle differ in Node, schema-first versus TypeScript-first, how each handles migrations, and when raw pg is still the right call.

Before thisSQL databases with Node and connection pools

Web Dev Simplified · 20 min

ORMs for Node, Prisma and Drizzle

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Web Dev SimplifiedReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Prisma is schema-first: you describe models in a `schema.prisma` file, and a generator produces a typed client. Drizzle is TypeScript-first: your schema is TypeScript code, and queries are built to look like SQL.
  • Both give typed results from your schema and both run on top of a driver and a pool. Neither removes the need to understand the SQL they emit.
  • Prisma's query API (`findMany`, `include`, `select`) hides joins behind objects. Drizzle's `select().from().innerJoin()` keeps the SQL visible, and its `db.query` API offers nested reads once `relations()` are declared.
  • Migrations are generated by diffing your schema against the last known state: `prisma migrate dev` and `drizzle-kit generate` each write plain SQL files you can review, and `migrate` applies them.
  • Prisma 7 changed how setup works: a `prisma.config.ts` file holds the datasource, the generator is `prisma-client` with an explicit `output`, and the client takes a driver adapter such as `@prisma/adapter-pg`. Older tutorials that show `prisma-client-js` and an in-schema `url` are out of date.
  • Choose by team and query style. An ORM pays off for CRUD-heavy apps and typed models. Drop to raw SQL for reports, bulk operations and anything the ORM expresses badly.

What an ORM does for a Node service

The previous lesson used pg directly. That is fine for a few queries, but a real service accumulates dozens, and three problems show up. The SQL lives in strings, so a renamed column breaks at runtime. Result rows come back as untyped objects. And the schema exists in two places, the migration files and the code that reads it, with nothing forcing them to agree.

Object-relational mappers and query builders attack these. You describe the schema once, and the tool gives you typed queries, typed results, and a way to generate migrations from changes to that description. The general trade-offs, including when raw SQL is better, are covered in ORMs, query builders and raw SQL, and how migrations are managed safely in schema migrations and change management. This lesson compares the two tools most new Node projects choose between.

Two philosophies

Prisma is schema-first. You write models in its own language:

generator client {
  provider = "prisma-client"
  output   = "../generated"
}

datasource db {
  provider = "postgresql"
}

model User {
  id    Int    @id @default(autoincrement())
  email String @unique
  name  String
  posts Post[]
}

model Post {
  id       Int    @id @default(autoincrement())
  title    String
  author   User   @relation(fields: [authorId], references: [id])
  authorId Int
}

prisma generate turns that into a typed client in ./generated. Queries are method calls on objects:

const ada = await prisma.user.create({
  data: { email: 'ada@example.com', name: 'Ada', posts: { create: [{ title: 'First' }, { title: 'Second' }] } },
  include: { posts: true },
});

Notice you never wrote a join, an insert order or a foreign key assignment. That is the appeal and the risk: it reads like data, but a SQL query or several are happening underneath, and N+1 problems are still possible if you load relations carelessly.

Drizzle is TypeScript-first. The schema is code, and the query API stays close to SQL:

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

export const users = pgTable('users', {
  id: serial('id').primaryKey(),
  email: text('email').notNull().unique(),
  name: text('name').notNull(),
});
export const posts = pgTable('posts', {
  id: serial('id').primaryKey(),
  authorId: integer('author_id').notNull().references(() => users.id),
  title: text('title').notNull(),
});

const rows = await db
  .select({ title: posts.title, author: users.name })
  .from(posts)
  .innerJoin(users, eq(posts.authorId, users.id))
  .orderBy(desc(posts.id));

Types come straight from the schema file with no generate step. If you know SQL, you can predict what runs. Drizzle also offers a nested read, db.query.users.findFirst({ with: { posts: true } }), but it needs relations() declared alongside the tables. I hit that when I tried it without: it throws instead of guessing. Declaring relations adds no migration, since they describe how to query, not what is in the database.

This is the comparison the video makes, chapter by chapter: what each is, supported databases, the studio app, the CLI, setup steps, schema definitions, CRUD, type safety, performance and docs.

Migrations in practice

Both tools diff your schema against the last recorded state and write SQL.

# Prisma
npx prisma migrate dev --name init     # writes prisma/migrations/<timestamp>_init/migration.sql and applies it
npx prisma migrate deploy              # production: apply pending migrations only

# Drizzle
npx drizzle-kit generate               # writes drizzle/0000_<name>.sql
npx drizzle-kit migrate                # applies it

I ran both against a local Postgres 14. Drizzle’s output for the two tables above was a CREATE TABLE for each, plus an ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY line. Prisma’s was the same idea. Running generate a second time after adding only relations() reported “No schema changes, nothing to migrate”.

The important habit is the same for both. The generated SQL is a file in your repository. Read it, because a rename can come out as drop-and-create, which loses data. Commit it. In production, run only the apply step, never a command that regenerates.

Prisma 7 changed the setup

The video is from March 2024, and the biggest practical gap is how Prisma is wired up. I ran the examples against Prisma 7.10.1 (the latest stable line; npm’s latest tag currently points at an 8.0 release candidate, so check which you get). The pieces that differ from older tutorials:

  • A prisma.config.ts file holds the schema location and the datasource URL, instead of the URL sitting in schema.prisma.
  • The generator is prisma-client with an explicit output, replacing prisma-client-js and the hidden node_modules/.prisma folder.
  • The client no longer bundles a Rust query engine. You pass a driver adapter, so Prisma uses the same kind of pg connection underneath:
import { PrismaPg } from '@prisma/adapter-pg';
import { PrismaClient } from './generated/client.ts';

const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL });
const prisma = new PrismaClient({ adapter });

That ran directly on Node 26 as a .mts file, because modern Node can strip TypeScript types itself (see running TypeScript without a build step). Because of these changes, treat any performance comparison made before Prisma 7, including the video’s, as describing the old Rust-engine architecture rather than the current one.

Drizzle is still a 0.x library (0.45 when I tested), so config details have moved over time and may again. Its config goes in drizzle.config.ts or .js. I found .mjs was not picked up, so use one of the others.

TypeORM, Sequelize and friends

You will meet older projects on TypeORM (decorator-based entities, Active Record or Data Mapper style) and Sequelize (JavaScript-first models). They work and are widely deployed. Newer projects mostly pick Prisma or Drizzle, or a query builder like Kysely, for stronger type inference. If you inherit one, the concepts above carry over: model definitions, a migration generator, a query API over a pool.

Choosing

  • Prisma suits teams that want one clear way to do things, good tooling (a studio app, a thorough migration flow) and don’t mind a schema DSL and a generate step.
  • Drizzle suits teams comfortable with SQL who want the schema in TypeScript and little between them and the query.
  • Raw pg is still right for reports, bulk operations and database-specific features. Mixing is normal. Both ORMs expose a raw query escape hatch over the same pool.

What the video covers

Web Dev Simplified compares Prisma and Drizzle across what they are, supported databases, the studio apps, the CLIs, setup, schema definitions, CRUD, type safety, performance, documentation, and a conclusion. It is a comparison, not a tutorial, and it predates Prisma 7, so the setup and performance sections reflect the older engine. The migration commands, the Prisma 7 setup and the relations note are carried by this prose and were run locally. The “go deeper” links add a Drizzle crash course and Prisma’s own migrations walkthrough. The latter predates Prisma 7 too, so expect config files to look different.

Where this fits

ORMs target relational databases. SQLite in Node is a relational database with no server at all, and MongoDB and Mongoose is the document-database alternative.

Check yourself

  1. 01What is the practical difference between Prisma's schema file and Drizzle's schema?

    Prisma uses its own `.prisma` language and a generate step that creates a typed client from it, so a schema change needs `prisma generate`. Drizzle's schema is ordinary TypeScript (`pgTable(...)`), so types flow directly with no generated client, and the same file doubles as the input to migration generation.

  2. 02You add a column to your schema. How does each tool get that change into the database?

    Each diffs the schema against the previous migration state and writes a SQL file. With Prisma, `prisma migrate dev --name add_column` creates and applies it. With Drizzle, `drizzle-kit generate` writes the SQL and `drizzle-kit migrate` applies it. In both, the SQL file is committed and reviewed like code, and production runs only the apply step.

  3. 03A tutorial uses `provider = "prisma-client-js"` and `new PrismaClient()` with no arguments. What changed in current Prisma?

    Since Prisma 7 the recommended generator is `prisma-client` with an explicit output folder, the connection URL lives in `prisma.config.ts`, and you construct the client with a driver adapter, for example `new PrismaClient({ adapter: new PrismaPg({ connectionString }) })`. The client no longer ships a Rust query engine binary.

  4. 04When would you skip the ORM and write SQL with `pg`?

    For reporting queries with window functions or CTEs, bulk inserts and updates, database-specific features, and any query where the ORM generates something slow that you cannot shape. Most projects mix both: the ORM for ordinary reads and writes, raw SQL for the hard queries, over the same connection pool.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark