Mutation Patterns Summary
Reviewing Our Work
Section titled “Reviewing Our Work”In this chapter, we stepped beyond reading data and took control of our database. We established an Admin Area to organize our mutating endpoints, and we tackled the two most common ways web applications change data.
1. The Deletion Pattern
Section titled “1. The Deletion Pattern”Deletions are blunt operations, best handled quickly and efficiently.
- The Trigger: Client-side JavaScript (
fetch()) sends an HTTPDELETErequest. - The Router: Pulls the
:idfrom the URL parameters (req.params.id). - The Ops Layer: Executes Mongoose
Model.findByIdAndDelete(id). - The Feedback: The router returns JSON (
res.json()); the client removes the HTML element from the screen without refreshing the page.
2. The Update Pattern
Section titled “2. The Update Pattern”Updates require precision, validation, and a focus on UX.
- The Trigger: A standard HTML
<form>submits an HTTPPOSTrequest (the browser doesn’t easily natively support HTTPPUTorPATCHfrom a standard HTML form element without JS). - The Router: Pulls both the
:idfrom the URL and the new data from the form (req.body). - The Ops Layer: Executes the Load → Modify → Save cycle (
findById()→ reassign variables →save()). - The Feedback: If successful, the router redirects the user back to a view where they can see the change (
res.redirect()). If it fails, the router re-renders the form view, pre-populating it with the user’s failed data so they can try again.
Understanding when to use client-side fetch (for invisible, layout-preserving actions like delete) versus when to use standard form submissions (for heavy data entry that might need server-side error re-rendering) is the mark of a senior engineer.
Extra Bits & Bytes
Section titled “Extra Bits & Bytes”Completed Update and Delete Demo Repository
⏭ Next: Connecting Data Types
Section titled “⏭ Next: Connecting Data Types”We now have isolated Contacts and isolated Projects. But in real applications, data is intertwined. Let’s learn how to link documents together.