Module 61: Database Integration
What an ORM is
🏠The everyday picture
An interpreter at a meeting: you speak your language, they speak the other party's. Conversations flow faster, but a good negotiator still learns enough of the other language to notice when something gets lost in translation.
📘What it really is
- An ORM (object-relational mapper) lets you work with database rows as objects in your programming language, writing code instead of SQL for everyday operations:
const customer = await prisma.customer.findUnique({ where: { id } });
- What ORMs give you:
- typed results: the editor knows every field (with TypeScript);
- migrations generated from a schema;
- relations loaded easily;
- automatic parameterisation (safe from SQL injection);
-
less repetitive mapping code (Module 59, Lesson 4).
-
What they can hide:
- inefficient queries, especially N+1 problems (Module 57) when loading related data in loops;
- complex reports, which are often clearer in SQL;
-
database features the ORM doesn't support.
-
Popular choices in TypeScript:
- Prisma: its own schema language, generated client, migrations; very popular, beginner-friendly;
- Drizzle: schema written in TypeScript, queries that look like SQL; lightweight;
-
Kysely: a type-safe SQL query builder rather than a full ORM.
-
Know SQL anyway. With an ORM you still design tables, read query plans, write reports and debug in SQL. Everything in Courses 5.1 and 5.2 still applies.
🖱️Try it
Look at one of your repository functions written in plain SQL. Write down what an ORM would save you (mapping, typing) and what you'd want to keep seeing (the actual SQL, for performance). Read the "Why Prisma" or "Why Drizzle" page on their sites.
✅Quick check
Name two benefits and one risk of using an ORM.
Show the answer
Benefits: typed results, generated migrations, easy relations, automatic parameterisation. Risk: hidden inefficient queries like N+1.
If you use an ORM, do you still need to know SQL?
Show the answer
Yes: for design, reports, query plans and debugging.
