I'm currently working on an app quite similar to Instagram.
The relationships are:
- Users follow other users
- Users follow hashtags
They should then get a personalized feed with posts according to their subscriptions.
It took me a long time to come up with a fitting database structure and I'm still not sure if it is the best approach. Please have a look and let me know what you think.
users (col)
- user_id (doc)
- posts (col)
- post_id (doc)
-> contains only post id
- followed_users (doc)
- user_id (doc)
-> contains only user id
- feed (col)
- post_id (doc)
-> contains only post id
hashtags (col)
- hashtag_id (doc)
- posts (col)
- post_id (doc)
-> contains only post id
posts (col)
- post_id (doc)
-> contains full post
I would then write a cloud function to populate the feed collection of each user which will be triggered every time a new post is added.
In the frontend there will be multiple db calls when a feed is loaded: one call to get the post IDs (users/feed) and another call for the content of each post (posts/id) like in this mockup code:
posts = getData($userid/feed).limit(10)
posts.each:
getData(posts/$post.id)
Is this a viable solution for cost efficiency as well as performance?