ORM (Object-Relational Mapping)
A programming layer that maps relational database tables and rows to application objects and types, providing type-safe CRUD, relations, and migrations
Table of Contents
Structure Diagram
What is ORM (Object-Relational Mapping)?
A programming layer that maps relational database tables and rows to application objects and types, providing type-safe CRUD, relations, and migrations. It is a core concept in Databases & Caching within Web Dev.
Core Principles
Application code models data as typed objects and nested graphs, while relational databases store normalized tables and foreign keys. An ORM (Object-Relational Mapper) bridges this impedance mismatch by translating typed model operations into SQL queries and mapping result rows back into objects.
Beyond reducing boilerplate CRUD code, modern ORMs provide compile-time type safety, versioned schema migrations, and parameterized queries by default to guard against SQL injection.
Practical Takeaways
- Inspect the generated SQL: ORM convenience can hide N+1 loops, wide
SELECT *fetches, and suboptimal joins. Keep SQL logging enabled in dev and profile critical paths withEXPLAIN ANALYZE. - Use escape hatches for complex analytics: Don't fight the ORM for recursive CTEs, window functions, or heavy reporting—drop down to raw SQL or a typed query builder (Drizzle, Kysely) where SQL shines.
- Review migration DDL before production: Always inspect auto-generated migration files so table-locking
ALTERstatements don't cause downtime.
Notable Examples
- Prisma & Drizzle ORM (TypeScript): End-to-end type safety from database schema to API responses.
- JPA/Hibernate, SQLAlchemy, Django ORM: Mature persistence layers powering enterprise backends.