0

I am starting with Firebase and want to know what are the most effective ways to structure data

Let's take the example of a simple social media app where only photos can be shared (like the beginnings of Instagram).

  1. user upload a photo with some meta data (Description)

  2. (Home Feed) the users (followers) will see the post in a chronological way and offcource there will be other functionality like (liking the post , saving it , commenting)

  3. searching and following users

  4. notification about likes and comments

  5. search in comments

what could be a good structure for storing the data and good effcient way to get data ASAP

Vasant Raval
  • 257
  • 1
  • 12
  • 31
  • 1
    This type of question is too open-ended and opinion-based for StackOverflow. You might have better responses on a message board site for Android developers. – Tenfour04 Feb 28 '22 at 20:34
  • 2
    Just a heads up that the search functionality you are looking for is going to be a sticking point while using FIrebase. You might want to look into that early on. – Tristan Feb 28 '22 at 20:40
  • @Tristan how the search functionality can be an obstacle, can you explain it a little bit, actually I just started to work on firebase if I will face issues with the app I can switch to AWS right now in the early stage – Vasant Raval Mar 01 '22 at 05:55
  • If you consider at some point in time to try using [Cloud Firestore](https://firebase.google.com/docs/firestore/), here you can find useful [answer](https://stackoverflow.com/questions/46979375/firestore-how-to-structure-a-feed-and-follow-system/52153332). – Alex Mamo Mar 01 '22 at 07:47
  • Firebase has an extremely limited text search capability. Their own documentation recommends using a different service for this. Namely Algolia, ElasticSearch, etc. https://firebase.google.com/docs/firestore/solutions/search – Tristan Mar 01 '22 at 12:59

1 Answers1

2

I'll go ahead and leave an answer for how I would approach this. My answer will be geared more towards Firestore even though the question is marked as Realtime Database. There are multiple ways to structure the data. This is the general structure I would use given your example:

users
    - name
    - timestamp

posts
    - imageURL
    - description
    - timestamp
    - likeCount
    - commentCount

posts/comments //subcollection
    - userID
    - comment
    - timestamp
    
posts/likes //subcollection
    - userID
    - timestamp
    
savedposts
    - postID
    - userID

followers
    - userID
    - followedID

Some additional notes:

Image Upload

The best option here is to upload the images to cloud storage and utilize a cloud function to generate a public URL and save it to the post document.

Comment / User Search

As stated in my comment, Firebase does not have a great solution for text based searches. The solution I utilized in my project was to utilize a cloud function to keep an Algolia index in sync with my users collection. I then offload the user search to them through a callable cloud function - though you could utilize the Algolia client SDK directly in your app if you wanted. In your scenario, you would also have to keep all of your comments in sync as well. Algolia isn't a cheap service, so I would look into the pros / cons of using the other options listed in the docs.

Document IDs

I generally let Firestore auto ID the documents, but here I would make some exceptions. For the savedposts and followers collections I would utilize a (manual) compound ID of {userID}{postID} and {userID}{followedID} respectively. It allows you to perform simple actions of unliking and unfollowing without querying for the document first. Ex) firestore().collection('postsaves').doc(`${userID}${postID}`).delete()

Final Thoughts

You mention maybe moving to AWS. I have worked much more in Firebase than in AWS, but I have done both. In my opinion, Firebase is unmatched in both usability and documentation. There are some compromises in terms of functionality and fine tuning but I recommend sticking with Firebase if the lack of text searching is the only hurdle.

Tristan
  • 1,608
  • 1
  • 20
  • 34
  • thank you so much for the answer , it means a lot – Vasant Raval Mar 02 '22 at 09:06
  • hey, i just wonder what if I want to implement , reply on a comment and also like option for the comment and the reply – Vasant Raval Mar 02 '22 at 09:08
  • If you don't want nested replies, you can do another subcollection under `posts/comments/replies` to store the info you need. (user, reply, date, etc) If you want nesting, you'd need to have an upper level collection called replies that will hold userid, postid, content, etc. Comment likes will need to be similar to post likes. – Tristan Mar 02 '22 at 13:16
  • TYSM for the comment – Vasant Raval Mar 02 '22 at 13:46
  • Hey, just wondering if it is even possible to learn system design for firebase or can only be done in sql databases. I really want to learn system design for firebase because I am not just planning a very simple social media app, but I want to implement much more functionality into it. – Vasant Raval Mar 02 '22 at 13:49
  • 1
    Not exactly sure what you are looking for. Each implementation is pretty specific to the project. There are plenty of examples and resources online, but I can't give any better guidance than to just Google. I learned through mostly trial and error while building an app for about 4 years now. – Tristan Mar 02 '22 at 14:56
  • Hey , u can watch this 2 videos and you will understand what im trying to do https://youtu.be/QmX2NPkJTKg and https://youtu.be/07BVxmVFDGY – Vasant Raval Mar 02 '22 at 16:13