We come now to the pinnacle of our sample application: the status feed of microposts. Appropriately, this section contains some of the most advanced material in the entire tutorial. The full status feed builds on the proto-feed from “Manipulating microposts Section” by assembling an array of the microposts from the users being followed by the current user, along with the current user’s own microposts. Throughout this section, we’ll proceed through a series of feed implementations of increasing sophistication.
Motivation and strategy
The basic idea behind the feed is simple. The purpose of a feed is to pull out the microposts whose user ids correspond to the users being followed by the current user (and the current user itself)
Although we don’t yet know how to implement the feed, the tests are relatively straightforward, so we’ll write them first. The key is to check all three requirements for the feed: microposts for both followed users and the user itself should be included in the feed, but a post from an unfollowed user should not be included.
test/models/user_test.js
Of course, the current implementation is just a proto-feed, so the new test is failing
A first feed implementation
With the status feed design requirements captured in the test, we’re ready to start writing the feed. Since the final feed implementation is rather intricate, we’ll build up to it by introducing one piece at a time. The first step is to think of the kind of query we’ll need. We need to select all the microposts from the microposts table with ids corresponding to the users being followed by a given user (or the user itself).
app/models/user.js
The test suite should be successful
Conclusion
With the addition of the status feed, we’ve finished the sample application for the Node On Train Tutorial.