Exercise: Pet Listings Data Layer
Move the Maple Street Pet Adoption Center's API from an array to MongoDB by finishing its data layer with the official driver, from the paged and filtered list to the right status for a bad id and a missing pet and the indexes the list needs, and let the tests check every request on a throwaway MongoDB.
Journeyman · MongoDB · Node.js · Updated
The Maple Street Pet Adoption Center's API from Exercise: Pet API Warm-Up works, but every restart forgets the pets. Its routes, its app and its server are now ready for MongoDB; only the data layer is left. You'll write it with the official driver: the pet list with its filters and pages, one pet by its id, adding and changing a pet, telling a real id from text that can't be one, and the two indexes the collection needs. Tests send every request to the API on a throwaway MongoDB and check each answer.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-mongodb-pet-listings-data-layer, also linked from the Exercise box beside the lesson. Clone it into your mongodb-course folder. In VS Code, open the mongodb-course folder, open the terminal with Terminal > New Terminal, and type:
git clone https://github.com/ask-codey/exercise-mongodb-pet-listings-data-layer.git
Git makes a folder, exercise-mongodb-pet-listings-data-layer, with the exercise code inside. Open that folder in VS Code (File > Open Folder), open its terminal, and install the packages:
npm install
Then copy .env.example to a new file named .env (Copy-Item .env.example .env in PowerShell, or cp .env.example .env on a Mac), as in Connecting from Node: MongoClient and the Connection String.
The folder holds:
data/pets.js: the data layer, with six functions for you to finish.routes/pets.js,app.js,problems.jsandserver.js: the API, ready to use. The routes call only the data layer.pets.http: requests to try, with the answers to expect.test/: the tests, the helper that starts their throwaway MongoDB, and the center's 12 pets intest/seed/pets.json. You don't need to change any of them.
What to do
Open data/pets.js. useDatabase, toPet and toListing are ready to use. Each of the six functions after them has a comment that says exactly what it should give back, with examples, and a placeholder that stops with an error for now:
/*
* 3. Give back the whole pet whose id is id, made with toPet, or null when no pet has it.
* id is text that isPetId says yes to, such as "6740c0000000000000000002": the routes check it first.
*
* getPet("6740c0000000000000000002") -> { id: "6740c0000000000000000002", name: "Mochi", species: "cat", age: 2,
* traits: ["calm", "indoor only"], status: "available" }
* getPet("6740c0000000000000000099") -> null
*/
export async function getPet(id) {
// For now it stops with an error, so GET /pets/:id answers 500.
throw new Error("getPet isn't built yet.");
}
Replace each placeholder with your own code:
isPetId(id):truefor text that can be an ObjectId, such as"6740c0000000000000000002", andfalsefor text that can't, such as"2". The routes answer 400 when it says no.listPets({ species, goodWithKids, page }): one page of 5 pets, sorted by name, made withtoListing. Only one species whenspeciesisn'tundefined; only pets whose traits include"good with kids"whengoodWithKidsis true. Page 1 of all 12 pets is Bean, Biscuit, Clover, Hazel and Juniper; page 3 is Rocket and Ziggy.getPet(id): the whole pet, made withtoPet, ornullwhen no pet has the id. The route answers 404 fornull.addPet(pet): saves{ name, species, age, traits }with the status"available", and gives the pet back with its new id.updatePet(id, changes): sets the fields inchanges, such as{ status: "adopted" }, and gives back the changed pet, ornullwhen no pet has the id.createIndexes(): makes two indexes,{ species: 1, name: 1 }for the list, and{ name: 1 }withunique: true, so a second Biscuit answers 409.
To see the API run against your own MongoDB, load the 12 pets into maple_street first: start mongosh in the exercise folder, type use maple_street, then load("test/seed/load-into-mongosh.js"). Then npm start, and try the requests in pets.http.
Check your work
Run the tests from the exercise folder:
npm test
At first all 17 tests fail. That's expected: each function you finish turns its tests green. When a test fails, its message says what it sent and what came back, for example GET /pets should answer 200, but it answered 500 with {"status":500,"title":"Internal Server Error","detail":"Something went wrong on our side."}.
The tests start their own throwaway MongoDB and put the 12 pets back before every test, so they never change your databases. If you installed MongoDB in Setup: MongoDB, mongosh and Compass, they use it.
Hints
- Start with
isPetId. Tests 1, 11 and 15 need only it, and When the Database Says No: Bad ids, Duplicates and Not Found shows the driver call that answers it without asking the database. - Build the list's filter piece by piece. Start with an empty object and add a field only when the address asked for it, as A Data Layer: One File That Talks to the Database does. Then sort, skip and limit before
toArray(), as in Sorting and Paging: sort, limit and skip. - Turn the id into an ObjectId before you ask. Text never equals an ObjectId: From mongosh to the Driver: find, insertOne and toArray shows the difference.
insertOnedoesn't give back the pet. It gives back the new_id. Put it with the fields you saved before you hand the document totoPet.- Give back the pet as it is after the change. Upserts and Replacing a Whole Document has the call that changes a document and hands it back, and the option that says "after".
- Indexes are the same calls as in mongosh. Compound Indexes and the Order of Fields and Unique and Text Indexes show both.
When you're done
Every test is green, and npm test prints:
✔ 1) isPetId("6740c0000000000000000002") gives true, and isPetId("2") gives false (1058.0516ms)
✔ 2) listPets({ species: "cat", goodWithKids: true, page: 1 }) gives Juniper, Olive and Pickles (13.5434ms)
✔ 3) listPets gives listings: Bean is { id, name, species, age, status }, with his id as text (10.9343ms)
✔ 4) GET /pets answers 200 with the first 5 pets in name order, Bean to Juniper (26.5574ms)
✔ 5) GET /pets?page=3 answers 200 with Rocket and Ziggy, the last 2 pets (16.1763ms)
✔ 6) GET /pets?species=cat answers 200 with the 5 cats, Juniper to Ziggy (14.2422ms)
✔ 7) GET /pets?species=dog&goodWithKids=true answers 200 with Bean, Biscuit and Maple (13.6256ms)
✔ 8) GET /pets?species=dog&page=2 answers 200 with an empty list: the 5 dogs fit on page 1 (14.2736ms)
✔ 9) GET /pets/6740c0000000000000000002 answers 200 with Mochi, whole (14.2761ms)
✔ 10) GET /pets/6740c0000000000000000099 answers 404 with "There's no pet 6740c0000000000000000099." (12.856ms)
✔ 11) GET /pets/2 answers 400 with "2 isn't a pet id." (11.3938ms)
✔ 12) POST /pets with Waffles answers 201 with Waffles, a new id and the status "available", and saves him (21.2035ms)
✔ 13) PATCH /pets/6740c0000000000000000006 with { status: "adopted" } answers 200 with Rocket adopted, and saves it (13.8944ms)
✔ 14) PATCH /pets/6740c0000000000000000099 answers 404 with "There's no pet 6740c0000000000000000099." (11.9422ms)
✔ 15) PATCH /pets/not-a-pet answers 400 with "not-a-pet isn't a pet id." (11.587ms)
✔ 16) createIndexes() adds the index { species: 1, name: 1 }, and the cats' list query uses it (39.1839ms)
✔ 17) After createIndexes(), POST /pets with a second Biscuit answers 409 with "Another pet already has that name." (46.2675ms)
ℹ tests 17
ℹ suites 0
ℹ pass 17
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 1834.7444
The times in parentheses will be different on your computer. Now open routes/pets.js and search it for ObjectId, collection or mongodb. You won't find one: if the center moved to another database tomorrow, which file would change, and which would stay exactly as it is?
Submit your exercise
The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.
On GitHub, click +, then New repository. Name it (for example
exercise-mongodb-pet-listings-data-layer), choose Public, and click Create repository. Leave out the README: your folder already has one.In VS Code's terminal, in your exercise folder, send your work to your new repo. Point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-mongodb-pet-listings-data-layer.git git add . git commit -m "My solution" git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-mongodb-pet-listings-data-layer, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.
Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.
So what
A data layer behind real routes, the right answer for every bad request, indexes for the questions the app asks, and tests that prove it on a throwaway database: that's the database half of a real API, and it's yours now.