Home > database >  What are some good ways to structure data in firebase?
What are some good ways to structure data in firebase?

Time:03-03

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

CodePudding user response:

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.

  • Related