DEV Community

Sharukhan Patan
Sharukhan Patan

Posted on

Mastering Inserts in Drizzle ORM: A Practical, Developer-Friendly Guide

If you’ve been playing around with modern TypeScript tooling, you’ve probably crossed paths with Drizzle ORM — the lightweight, SQL-first, type-safe ORM that feels like it was built for developers who hate ORMs… but still need one.

Inserts are one of the first things you touch when building anything real: users, posts, sessions, logs — everything starts with putting data into your database.

But Drizzle’s take on insert() is a little different from the usual ORM flavor. It’s cleaner, more explicit, and honestly kind of refreshing.

Let’s walk through the actually useful stuff you need to know.

Why Drizzle’s API Feels Good

Most ORMs try to hide SQL so much that you eventually forget what SQL even looks like. Drizzle flips that:
you write code that looks like SQL, behaves like SQL, and still gives you full TypeScript safety.

Basic Insert: The “Hello World” of Drizzle

Here’s how you insert a single row — nice and simple:

await db.insert(users).values({
  name: "John Doe",
  email: "john@example.com",
});
Enter fullscreen mode Exit fullscreen mode

What’s happening here?

  • db.insert(users) → tells Drizzle which table you’re inserting into.
  • .values({...}) → passes actual column data. Drizzle does not surprise you with implicit behavior. If you didn’t define a column or default, Drizzle won’t silently inject anything.

Insert Multiple Rows at Once

Bulk inserts? Also clean:

await db.insert(users).values([
  { name: "Alice", email: "alice@mail.com" },
  { name: "Bob", email: "bob@mail.com" },
]);
Enter fullscreen mode Exit fullscreen mode

Each object is validated using your schema. No more “undefined column” errors at runtime.

Returning Inserted Data (super useful)

Sometimes you want the newly inserted ID. Or the whole row. Drizzle has your back:

const result = await db.insert(users)
  .values({ name: "Sam", email: "sam@mail.com" })
  .returning();

console.log(result);
Enter fullscreen mode Exit fullscreen mode
  • .returning() gives you the inserted row(s)
  • Works on Postgres, SQLite, MySQL variants that support it You can also return only specific fields:
.returning({ id: users.id, email: users.email })
Enter fullscreen mode Exit fullscreen mode

Neat and efficient.

Summary

Drizzle’s insert API is one of those features that makes you go:
“Ahhh, this is how ORMs should have been built from day one.”
It lets you:

  • write SQL-like code
  • keep full type safety
  • avoid ORM magic
  • stay close to the database

Top comments (0)