Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore social network data structure

How to structure a Social Network database structure like for example twitter where we can follow a users and get all their tweets in our timeline, i have already checked this Firestore - how to structure a feed and follow system but the solutions in the post look flawed.

Firestore is different where it requires redundant data to access data efficiently, but suppose i am following 1000 people and if i need to get the posts of all those users by querying data for each 15 users i am following and using limit(10) method then orderBy(timeStamp) there may be unread posts between Queries, because we are getting the post using the last post timeStamp , how to structure the data for a social media app in Firestore

like image 290
VINNUSAURUS Avatar asked Dec 05 '22 09:12

VINNUSAURUS


2 Answers

When modeling a use-case on a NoSQL database, you tend to optimize for the features of your application, and for frequent read-operations.

So in a social media application your main feature may be that the user sees the recent posts of everyone they follow. To optimize this operation for frequent reads, you'll want to store the posts that the each user should see in a document for that user. So when compared to Twitter, you'd pretty much have a document containing the twitter feed for each user. Or if there's too much data for a single document, you might want to put that in a collection. I often explain this as modeling the screens of your app in the database.

This is very different from the typical data model in a relational database, so it's normal that it takes time to get used to. For a good introduction, I recommend:

  • Reading NoSQL data modeling.
  • Watching Firebase for SQL developers, even though it's for the Realtime Database, it explains how to map common SQL concepts to Firebase's NoSQL model.
  • Watching Getting to know Cloud Firestore
like image 113
Frank van Puffelen Avatar answered Jan 13 '23 13:01

Frank van Puffelen


To develop a social media app like Twitter. The Firestore queries are not enough. Twitter generates a personalized timeline for every user. This is where the cloud functions come into the picture.

You need a cloud function that monitors for new posts and copies them in their following user's timelines. You don't need to copy the entire tweet data. You can just copy the tweet id and other fields which require ordering, like timestamp.

So when I query my timeline, I will get all the tweet ids. Then I can just load the original tweet when the user is about to scroll. Because the likes and dislikes should affect the original tweet.

like image 44
Subhas kadam Avatar answered Jan 13 '23 12:01

Subhas kadam