1. Concept library
  2. Backend with Node.js
  3. Module 6: Data access layers

Concept

Query builders and ORMs

A query builder composes SQL safely from code; an ORM maps rows to objects and writes the SQL for you. Both save typing on the common paths, and you still need to see the SQL they generate.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain what a query builder and an ORM each do for you
  2. Write a query with a builder and read the SQL it generates
  3. Spot the N+1 loading through an ORM and batch it

In practice

A few lines that show Query builders and ORMs doing its job.

TypeScript
// Kysely: typed query builder
const rows = await db
  .selectFrom("orders")
  .innerJoin("customers", "customers.id", "orders.customer_id")
  .select(["orders.id", "orders.total_cents", "customers.email"])
  .where("orders.status", "=", "paid")
  .orderBy("orders.created_at", "desc")
  .limit(20)
  .execute();           // rows is typed from the schema

The common mistake

Watch for

Loading a list of parents and then each one's children in a loop through the ORM. It is the N+1 problem again: one query per row. Join or batch-load the children.

What to know first

Your roadmap puts these before Query builders and ORMs, unless you already know them.

How it stays learned

Once a session shows you understand Query builders and ORMs, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

Where it leads

Topics that build on Query builders and ORMs.

Next in the moduleMigrations →