ORMs
A library that lets your code work with database rows as objects and writes the SQL for you.
What is it?
Raw SQL strings in app code work, but they get repetitive and error-prone: building queries by joining strings, mapping columns back into objects by hand, and retyping the same SELECT/INSERT/UPDATE patterns.
An ORM (Object-Relational Mapper) bridges that gap. You work with rows as regular objects in your language (create a User, set its properties, call .save()), and the ORM turns those actions into SQL. You say what you want in the language you already use; the ORM generates the how in SQL.
Explain like I'm 10
An ORM is like a translator at a meeting. You speak your language (app code), the translator (the ORM) turns your requests into the language the database understands (SQL), then translates the reply back.
Examples
Raw SQL vs. an ORM (JavaScript, using a Prisma-style ORM)
// Raw SQL
const result = await db.query(
"SELECT * FROM users WHERE id = $1",
[userId]
);
const user = result.rows[0];
// With an ORM
const user = await prisma.user.findUnique({
where: { id: userId },
});Both fetch the same row. The ORM version reads like normal app code and returns a ready-to-use object, with the SQL generated for you.
Creating and updating a row through an ORM
const user = await prisma.user.create({
data: { name: "Amara", email: "amara@example.com" },
});
await prisma.user.update({
where: { id: user.id },
data: { email: "amara@newmail.com" },
});The same as an INSERT then an UPDATE, written as method calls instead of SQL strings.
How it works
An ORM maps your language's classes or object shapes to the database's tables and columns. When you call a method like .create() or .findUnique(), it builds the SQL, sends it, and turns the returned rows into objects shaped like your app's data.
Most ORMs also handle migrations (versioned scripts that change the schema over time) and connection management. Many let you "drop down" to raw SQL for the rare query that's awkward in the ORM's API.
Why does it exist?
Hand-writing SQL for every operation in a large app means lots of repetitive string-building, manual type conversion, and more room for mistakes (like leaving a query open to SQL injection). ORMs remove the repetition, keep you in one language most of the time, and give safer defaults (like automatically parameterizing values).
When to use it
ORMs fit most everyday app code: simple CRUD, standard relationships, typical filtering. There, the productivity and safety gains outweigh the cost of the abstraction.
When not to use it
For complex, performance-critical queries (heavy aggregations, deeply nested joins, database-specific features), ORM-generated SQL can be slow or awkward. Raw SQL, which most ORMs allow as an escape hatch, is often clearer and faster.
Common mistakes
Assuming an ORM writes efficient SQL. It can generate slow queries (like fetching related rows one at a time in a loop) as easily as hand-written code.
Never learning the underlying SQL, which makes slow or wrong queries much harder to debug.
Fighting the ORM to force an awkward query through it instead of using raw SQL for that one case.
Practice exercises
- Easy:
In your own words, explain what an ORM does between your app code and the database.
- Medium:
Write the raw SQL equivalent of an ORM call that finds all orders where total_cents is greater than 1000.
- Hard:
Describe a scenario where raw SQL is a better choice than an ORM's query builder.
Interview questions
What is an ORM?
A library that lets you work with database rows as objects in your language, turning those object operations into SQL.
What's a downside of relying entirely on an ORM?
It can generate inefficient SQL for complex queries, and developers who never learn SQL struggle to debug or tune slow queries.
Do ORMs prevent you from writing raw SQL when needed?
No. Most ORMs have an escape hatch for running raw SQL when their query API doesn't fit.