Exercise 3: A Versioned PATCH Route — Possible Solution ========================================================= 1. schema.prisma — add to Post: version Int @default(0) Then: npx prisma migrate dev --name add-post-version npx prisma generate (Existing posts get version 0 from the default.) 2. Make GET /posts/:slug include version in its select, so the editor knows which version it loaded. 3. The route: app.patch("/posts/:slug", async (req, res) => { const { title, content, version } = req.body; if (typeof version !== "number") { return res.status(400).json({ error: "version is required" }); } const result = await prisma.post.updateMany({ where: { slug: req.params.slug, version }, data: { title, content, version: { increment: 1 } }, }); if (result.count === 0) { const exists = await prisma.post.findUnique({ where: { slug: req.params.slug } }); if (!exists) return res.status(404).json({ error: "Not found" }); return res.status(409).json({ error: "This post was changed by someone else. Reload and try again.", currentVersion: exists.version, }); } const post = await prisma.post.findUnique({ where: { slug: req.params.slug } }); res.json(post); }); Simulating two editors (both loaded version 0): $ curl -X PATCH localhost:3000/posts/hello-api -H "Content-Type: application/json" \ -d '{"content":"Ada fixed a typo","version":0}' {... "version":1 ...} $ curl -X PATCH localhost:3000/posts/hello-api -H "Content-Type: application/json" \ -d '{"content":"Grace rewrote a paragraph","version":0}' {"error":"This post was changed by someone else. Reload and try again.","currentVersion":1} WHY THIS WORKS AS AN ANSWER ------------------------------ updateMany is used because update's where clause is aimed at finding a single record, while updateMany returns a count we can check. A count of 0 means either the post doesn't exist or its version moved on; one extra lookup tells the two apart so the client gets the right status (404 or 409). Grace's stale save is refused instead of silently overwriting Ada's fix, and nothing is locked while people are editing.