CodingNic

Capstone: A Full CRUD API with a Real Database

Exercises

Capstone: A Full CRUD API with a Real Database 45 min read

Exercises

Objectives

By the end of this lesson, you should be able to:

  • Build a complete, MVC-structured REST API backed by a real database, from scratch
  • Model a relationship as part of that API, in either PostgreSQL or MongoDB
  • Confirm the API’s data survives a server restart

⚠️ A note on verification: the Prisma and Mongoose code patterns referenced in this exercise reflect each library’s stable, documented API, as throughout this course’s ORM-specific lessons. The overall MVC structure follows the pattern verified end-to-end with a real, running Express server in Node.js & Express Foundations. Build and run this exercise yourself, end to end, against a real database.

Capstone Exercise: A Contacts API

Choose either PostgreSQL with Prisma, or MongoDB with Mongoose, for this exercise, following whichever pattern (Lesson 2 or Lesson 3) fits the choice.

a) Model it. Build a Contact resource (name, email, phone) and a related Note resource, a contact can have many notes (content, createdAt). Choose the relationship shape appropriate to your database: a foreign key and migration for Prisma (Module 5), or embedding versus referencing for Mongoose (Module 9), and briefly justify the choice.

b) Build the full MVC structure.

text
contacts-api/
├── models/
│   ├── contactModel.js
│   └── ...
├── controllers/
│   └── contactController.js
├── routes/
│   └── contactRoutes.js
└── app.js

c) Implement full CRUD for contacts, GET /api/v1/contacts, GET /api/v1/contacts/:id, POST, PUT, DELETE, following the exact controller and error-handling pattern from Lesson 2 or Lesson 3.

d) Add notes to a contact. Implement POST /api/v1/contacts/:id/notes, adding a note to a specific contact, and GET /api/v1/contacts/:id/notes, listing a contact’s notes, wired through your chosen relationship approach.

e) Add filtering. Implement GET /api/v1/contacts?search=<name>, filtering contacts by a partial name match (Module 6’s contains for Prisma, or a regular expression filter for Mongoose).

f) Confirm persistence. Create a few contacts and notes, restart the server, and confirm every one of them is still there, exactly as Lesson 2 demonstrated.

g) Write a short reflection. In a few sentences, compare this project to the very first version of a similar API from Node.js & Express Foundations, backed by an in-memory array. What changed in the model layer? What, if anything, changed in the controller, routes, or overall structure?

Recap

This capstone closes out Databases & ORMs for Node.js by building, end to end, exactly what the course set out to do: take the MVC structure from Node.js & Express Foundations and give it real, persistent storage, in either a relational or a document database, with the model layer as the only part that genuinely had to change.

This is the final module of Databases & ORMs for Node.js. From here, the next course in this track picks up production-readiness: authentication, validation, security, and testing.