Drizzle ORM and Migrations

Use Drizzle's SQL-like TypeScript API to define schemas, generate migrations and write type-safe queries with no heavy runtime.

Intermediate · about 8 hours · difficulty 3/5

Learn first: Relational Data Modelling in PostgreSQL, TypeScript Fundamentals

Drizzle is a lightweight TypeScript ORM that stays close to SQL. If you like knowing exactly which query runs, Drizzle feels natural.

The workflow

  • Define tables in TypeScript with pgTable, column helpers and relations.
  • Use drizzle-kit to generate SQL migration files from schema changes and apply them.
  • Infer row types directly from table definitions for use across your app.

Querying

  • A SQL-like builder: db.select().from(companies).where(eq(companies.city, "Bengaluru")).
  • A relational query API for nested results without manual joins.
  • Transactions with db.transaction.
  • The sql template tag for anything the builder does not cover.

Why teams pick it

Small runtime, works well on serverless and edge platforms, and queries map almost one to one to SQL, which makes performance easier to reason about. The trade-off is that you need stronger SQL knowledge than with Prisma.

Pitfalls

  • Generating migrations but forgetting to commit or apply them in CI.
  • Writing N+1 loops instead of a join or relational query.
  • Mixing push-based schema sync with migrations in production.

You are ready to move on when your placement portal uses Drizzle with committed migrations, inferred types and list queries that fetch related data in one round trip.

Key concepts

  • Table definitions with pgTable
  • drizzle-kit migrations
  • Select builder and filters
  • Relational queries
  • Inferred types
  • Transactions
  • Serverless drivers

Other options for this step

  • Prisma ORM and Migrations: Define your schema in Prisma, generate a type-safe client, run migrations and write efficient queries from TypeScript.

Mini project

Implement the placement portal data layer in Drizzle: schema, migrations, a seed script and a service listing openings with company details and applicant counts in one query.

Check yourself

What is the practical difference between generating migrations and pushing schema changes directly, and which should production use?

Free resources