MODULE 12 · LESSON 12.2
Fetch only required fields, avoid N+1 patterns and group related writes.
The production problem this solves
Treat Prisma Queries and Transactions as an engineering decision with consequences for the user, the next layer and the person debugging it later. This lesson defines an application trust boundary, where an explicit contract is safer than framework convention or an undocumented assumption.
Here, that decision supports a specific checkpoint: Replace the in-memory CourseFlow repository with PostgreSQL and Prisma. A reviewable result should include a repeatable request, automated test, query result and failure response rather than a claim that the feature simply works.
Prisma Queries and Transactions workflow
- 1Select And Include
- 2Relation Queries
- 3Transactions
- 4Query Logging
A practical model for prisma queries and transactions
Fetch only required fields, avoid N+1 patterns and group related writes. The useful unit of understanding is the boundary: who owns the decision, which input crosses it, what result is visible and how a failure is reported.
- Select And Include: Decide what belongs in code, configuration, data or documentation and explain why.
- Relation Queries: Name its input, observable result and most likely failure in this lesson.
- Transactions: Locate this responsibility in CourseFlow and defend the boundary you chose.
- Query Logging: Implement one behavior that another learner can reproduce without reading your mind.
Explain each moving part
Do not copy the sample yet. First explain why select and include is handled at this boundary and what would break if it moved.
const courses = await prisma.course.findMany({
select: { id: true, title: true, _count: { select: { lessons: true } } }
});Point to the exact line or command where relation queries enters the example and where its result becomes observable.
Trace the implementation boundary
- 1Select And Include
Run the focused example and save the output, trace, query or screenshot that confirms the result.
- 2Relation Queries
Break one assumption on purpose, make recovery clear and record the trade-off you accepted.
- 3Transactions
Name the caller and the owner of this behavior before changing the implementation.
- 4Query Logging
Compare expected and actual output before editing; the difference tells you where to investigate.
Mistakes that create hidden coupling
- Treating select and include as vocabulary instead of defining the behavior it must produce.
- Testing the expected path while ignoring an empty, invalid, repeated or unauthorized case around relation queries.
- Allowing transactions to cross a boundary without an explicit contract or useful error.
- Changing several layers before capturing the first piece of evidence, which makes the original cause harder to see.
Debug from the boundary inward
- Reduce the problem to the smallest failing Prisma Queries and Transactions case.
- Capture the actual input and output at the select and include boundary.
- Read the first relevant error, request, trace or query rather than the loudest downstream symptom.
- Test one explanation for the failure in relation queries; avoid changing two variables together.
- Keep a regression check that would expose the same defect if it returned.
Security decision
Validate external input, authorize the requested action, use parameterized data access, and keep credentials out of responses, source control and logs.
Performance decision
Bound queries and collections, inspect the actual request or query plan, and optimize only the slow boundary confirmed by evidence.
PRACTICE
Build something you can inspect
Implement course detail and atomic enrollment operations, then inspect generated query behavior.
Stretch challenge
Build a second implementation of select and include, compare it with the first, and defend the choice you would ship.
Definition of done
- The behavior around select and include works with realistic input.
- A failure involving relation queries is handled clearly and without leaking sensitive detail.
- The implementation remains keyboard-usable when it produces an interface.
- Your evidence directly supports the claim made in the exercise.
- The README records the important trade-off without pretending the solution is universal.
Check your reasoning
How can an ORM still produce an N+1 query problem?
Answer by naming the expected select and include behavior, the layer responsible for it and the evidence that would confirm your explanation.
Where would you investigate the first failure?
Start where relation queries crosses a boundary. Compare the actual input and output there before following downstream symptoms.
What would make this work reviewable?
Show the focused change, repeatable steps, the result of your check and one honest trade-off connected to transactions.
What to carry into the next lesson
- Fetch only required fields, avoid N+1 patterns and group related writes.
- Keep select and include visible at the boundary where it can be tested.
- Use evidence from relation queries before widening the implementation.
References and related reading
Progress is stored only in this browser.
Share this page
Share this page with the people who will use it next.
Discussion
No comments yet. Add the first useful question or observation.
You must log in to post a comment.