CoachnestCoachnest
Sign InGet Started
Back to course

Mastering CRUD: Build Full-Stack Database Applications

…
—
Contents
1

What Is CRUD and Why It Matters

Reading12mFree
2

CRUD, REST, and HTTP Verbs

Reading14mFree
3

The Data Lifecycle of a Record

Reading11m
4

Course Project Tour: TaskFlow

Video9m
5

Chapter 1 — Quiz

Quiz8m
6

Tables, Rows, Columns & Types

Reading14m
7

Primary Keys & IDs (Auto-increment vs UUID)

Reading13m
8

Relationships: One-to-Many & Many-to-Many

Reading16m
9

Normalization & Schema Design Principles

Reading14m
10

Modeling TaskFlow with Prisma

Reading13m
11

Chapter 2 — Quiz

Quiz8m
12

INSERT — Creating Rows

Reading13m
13

SELECT — Reading & Filtering

Reading16m
14

UPDATE — Changing Rows Safely

Reading12m
15

DELETE — Removing Rows

Reading11m
16

Live SQL: A Full CRUD Session

Video15m
17

Chapter 3 — Quiz

Quiz9m
18

REST API Design for CRUD Resources

Reading14m
19

HTTP Status Codes That Tell the Truth

Reading12m
20

Scaffolding the API (Express & Next.js)

Reading16m
21

Connecting an ORM (Prisma) to Your Routes

Reading13m
22

Chapter 4 — Quiz

Quiz8m
23

Building the Create Endpoint End-to-End

Reading15m

Reading a Single Resource

Reading11m
25

Listing Collections

Reading13m
26

Live Coding: Create & Read

Video16m
27

Chapter 5 — Quiz

Quiz8m
28

PUT vs PATCH: Full vs Partial Updates

Reading13m
29

Authorization: Who Can Change This Row?

Reading12m
30

Soft Delete, Hard Delete & Restore

Reading14m
31

Idempotency & Concurrency Control

Reading13m
32

Chapter 6 — Quiz

Quiz9m
33

Input Validation with Zod

Reading14m
34

Mass Assignment & Over-Posting

Reading11m
35

SQL Injection & Safe Queries

Reading13m
36

Consistent Error Handling

Reading12m
37

Chapter 7 — Quiz

Quiz9m
38

Offset vs Cursor Pagination

Reading15m
39

Filtering & Dynamic WHERE Clauses

Reading13m
40

Safe Sorting & Full-Text Search

Reading14m
41

Indexing for Fast Reads

Reading13m
42

Chapter 8 — Quiz

Quiz9m
43

Forms & Creating Records from the UI

Reading14m
44

Fetching & Displaying Data

Reading13m
45

Optimistic Updates & Deletes

Reading14m
46

Building the TaskFlow UI

Video17m
47

Chapter 9 — Quiz

Quiz8m
48

Transactions & Data Integrity

Reading15m
49

Testing Your CRUD Endpoints

Reading14m
50

Caching, N+1 & Performance

Reading13m
51

Deploying & Migrating Safely

Reading14m
52

Chapter 10 — Final Quiz

Quiz10m
←→navigate lessons
Chapter 5 of 10·Chapter 5 — Create & Read in Practice
Lesson 24 of 52Reading11 min

Reading a Single Resource

Reading a Single Resource¶

GET /tasks/:id looks trivial, but doing it right covers existence, authorization, and shaping.

The Handler¶

ts
23 lines
1export async function GET(
2  _req: NextRequest,
3  { params }: { params: Promise<{ id: string }> },
4) {
5  const { id } = await params;
6  const session = await getSession();
7  if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
8
9  const task = await prisma.task.findUnique({
10    where: { id },
11    include: { owner: { select: { id: true, name: true } }, tags: true },
12  });
13
14  // Exists?
15  if (!task) return NextResponse.json({ error: "Not found" }, { status: 404 });
16
17  // Allowed?
18  if (task.ownerId !== session.userId) {
19    return NextResponse.json({ error: "Not found" }, { status: 404 });
20  }
21
22  return NextResponse.json({ data: task });
23}

Three Decisions in Every Read-One¶

  1. 1.Does it exist? If not → 404.
  2. 2.Is the caller allowed to see it? If not → 403 or, to hide existence, 404.
  3. 3.What shape do they get? Include related data they need; select away anything sensitive.

Include vs. Select¶

  • include adds whole related records.
  • select picks exact fields and excludes the rest.

Use select on anything with secrets (users, payment methods) so a password hash never rides along.

Avoid N+1 in the List Version¶

When you later list tasks with their owners, fetch the relation in one query (include), not one query per row. The ORM batches it into a single JOIN-style fetch. We tune this in Chapter 8.

Previous

Building the Create Endpoint End-to-End

Next

Listing Collections

Use ← → arrow keys to navigate between lessons