Node.js course · Part 9: The project · lab 9 of 9
Project: the quote API, end to end
Build one real API from the pieces of this course: a store with parameters and transactions, a write endpoint that refuses bad input, a read endpoint that cannot leak another person's data, and a router that logs everything and leaks nothing.
By the end you can
- Write a data layer where values can never become instructions and partial writes cannot happen
- Handle a write request through every branch: unauthenticated, malformed, invalid, created
- Return the right status for a resource that exists but is not yours
- Assemble a router with one error boundary and one log line per request
Easier if you did first
step 1 of 4
The store
Start at the bottom, with the only code allowed to touch the database. Everything above it will trust what this returns, so this is where the two data rules live.
First: values are never joined into query text. The pretend database here parses its query exactly as a real one does, so a value that closes its own quote really does become instruction. Parameters make that impossible.
Second: a quote and its line items are one write. Either both land or neither does, because a quote with no lines is a record nobody can price and nobody will notice until a customer asks.
Write the three functions. Keep them boring: the data layer is the one place where clever is a liability.
What these words mean
- data layer
- The only code that talks to the database. Everything else goes through it.
- parameter
- A placeholder filled after the query is understood.
- transaction
- Several writes that succeed or fail together.
- row
- One record, returned as a plain object.
Easy to get wrong
Returning the database row straight to the caller
Returning what the caller needs
Rows gain columns over time, and a column added for internal use is then in your API response. Decide what leaves this layer on purpose.
What we check
- A quote is found by its id
- A missing id gives null, not undefined
- An injection attempt is just a value that matches nothing
- A good create adds exactly one quote
- A bad line rolls the whole thing back
- And the failure is rethrown, not swallowed
- No transaction is left open
- The lookup uses a placeholder
- Failures roll back
Nothing printed yet. Run your code.