Stop 08 · ORMs & Query BuildersTopics · 1/10

What is an ORM

Part of the Backend Developer Roadmap

In short

Writing raw SQL strings for every query gets tiring and error-prone. An ORM lets you work with database rows as normal objects in your own language.

Object-Relational Mapping — a library that lets you interact with your database using your programming language's objects/classes instead of writing raw SQL by hand.

The blob chats with a small robot interpreter next to a filing cabinet of grid cards.

Think of it like this

The friendly interpreter

You speak Hindi; the clerk only speaks Tamil. An interpreter in between turns your request into Tamil and the reply back into Hindi. An ORM is that interpreter: you write JavaScript objects and method calls, it speaks SQL to the database and hands objects back.

Watch it happen

From method call to rows and back

Your code
ORM
Postgres

findMany({ city })

1/5Your code asks for users in Pune with a method call, not a SQL string.

The big ideas

What you need to know

Rows become objects

A users row turns into a User object. Columns become properties with the right types.

Safer by default

ORMs use parameterised queries, so user input can't sneak in as SQL.

Schema and migrations

Most ORMs describe tables in code and can generate migrations when you change them.

It can hide slow queries

You still need SQL basics. An ORM can quietly run one query per row if you're careless.

See it in code

Walk through the code

ts
1// Raw SQL with the pg driver2const { rows } = await pool.query(3  "SELECT id, name FROM users WHERE city = $1 ORDER BY name LIMIT 10",4  ["Pune"],5);6 7// The same query through an ORM (Prisma)8const users = await prisma.user.findMany({9  where: { city: "Pune" },10  select: { id: true, name: true },11  orderBy: { name: "asc" },12  take: 10,13});

1/2With a driver, you write the SQL string yourself and get untyped rows back.

Both run the same SQL in the end. The ORM version gives autocomplete and a type error if you misspell a column.

Save yourself the pain

Common mistakes

Oops

Learning an ORM instead of learning SQL.

Do this instead

Learn SQL first. When an ORM query is slow, you'll need to read the SQL it generated.

Oops

Assuming the ORM always writes efficient queries.

Do this instead

Turn on query logging and check what actually runs, especially inside loops.

Your mission

Driver vs ORM

~25 min

Quick check

What's the main job of an ORM?

Go deeper

  1. 1Compare the same query written in raw SQL versus an ORM's query API.
  2. 2List the trade-offs: ORMs speed up development but can hide what's actually happening in the database.
  3. 3Learn that understanding raw SQL first makes you far better at using any ORM.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge