ORM
A library that turns database rows into objects in your language, so you query with code and types instead of raw SQL strings.
See it
What it is
An ORM (object relational mapper) is a library that lets you talk to your database in your own language instead of raw SQL strings. You describe your tables once, and it hands back typed objects: 'user.orders' rather than a hand-written JOIN. Canonical examples are Prisma and Drizzle in TypeScript, ActiveRecord in Rails, SQLAlchemy in Python, and Hibernate in Java. Most also ship migrations, so schema changes are versioned files.
The payoff is autocomplete, compile-time errors when you rename a column, and no string concatenation near user input (which kills a whole class of SQL injection). Reach for one on any app with more than a handful of tables. Note the split: heavyweight ORMs hide SQL behind objects, while 'query builders' like Drizzle and Kysely keep the SQL shape visible and just make it typed. The lighter end is easier to reason about when queries get hairy.
The classic gotcha is the N+1 query: you loop over 50 posts, touch 'post.author' inside the loop, and quietly fire 51 queries. Fix it by eager loading (include, with, joinedload). The second gotcha is believing you will never write SQL again. Every real project eventually hits a window function or a recursive CTE the ORM cannot express, so pick one with a clean escape hatch to raw SQL and use it without guilt.
Ask AI for it
Set up an ORM layer for this project using Prisma (or Drizzle if I want the SQL shape kept visible). Define the schema with explicit primary keys, foreign keys, and relation fields, generate the typed client, and create the initial migration. Replace the existing raw SQL calls with typed ORM queries. Eager load relations with include/with wherever a list is rendered so we do not fire an N+1 query. Keep one escape hatch for raw SQL and add a short comment anywhere I use it explaining why the ORM could not express the query.