I am a new Firebase user, and I am not sure how to approach the writing of relationships in the database. Let's say I have users that want to save posts. (please let me know if any of my assumptions are incorrect)
From what I understand, my data needs to be as shallow as possible, with duplicate stuff wherever it's needed. (if I want to show posts for a given user, I will have to store whatever i want to show - title for example - with the user-data. And the other way around, if I want to show the user for a post I will store the username within the post.)
So for storing a post two write actions are needed:
- store post (+ user id)
- update user-data by adding the post id
Now my question is: who should be doing do these two updates?
- Clientside. This seems like the most obvious option, but is this a valid approach? If something goes wrong after the first write action, the second one is never handled.
- Transactions. So still cientside, but I doubt if this is a common usecase for transactions. Although i think it would work...?
- serverside API. Create something serverside that wraps all needed write-actions into 1 call (so client doesnt talk directly to firebase). This would obviously work, but seems to invalidate the reason to use Firebase at all.
- Firebase Cloud Functions. I could have a listener to newly added posts, and make sure it gets added to the user as well