I currently have a collection with users and I want to model friendships between them. The frontend should be able to query all the friends of a user with the profile picture and username of each friend.
I have already read in many places that there isn’t a single way to do this and people often have to choose between storing an array of friends uids in every user document, iterating over it and fetching the friend data more queries, or duplicating data avoiding having to fetch the friend data for each friend uid. I believe the latter option is the one that best follows the reads over writes principle so that’s the one I want to implement. Please correct me if something of what I wrote up to this point is wrong.
What I’m not sure about this solution are the following things:
- To implement this model I have to create a subcollection in each user document called friends. Each document of that subcollection is a friend of the user and it contains that friend’s profile picture and username. The id of each document in ‘friends’ should be the uid of the friend.
- When a user updates it’s profile picture or username, I have to go through every document inside every subcollection ‘friends’ where the id of the document is the user’s id and change the data.
Is that the correct way of implementing this model or am I missing something?
Lastly, there’s another problem. Let’s say a user changes his profile picture, this means the profile picture field will change in every document where the data was duplicated. What happens if one of this writes fail?
I’m not sure if the correct approach is to use batches from firebase api.