I am a long-time Firebase Realtime Database user and currently starting to use Firebase Firestore for some of the new things. In the RTDB, it was common to duplicate some things to aid in querying and to avoid multiple lookups as well. In Firestore, it is not recommended to duplicate things but in some places, we may have to. I have created a data model that looks like below:
There are two top level collections enterpriseUsers
and enterpriseAccounts
. Each enterprise account document contains a bunch of sub-collections. One of them is members
. It is a list of enterprise users associated with an enterprise account. Currently, I am storing an id (which is the enterprise user id for the member) and a couple of other things for a member. I am not storing the member's name and email in the sub-collection as that information is present in the enterprise user document. Now I have to show the list of members presents in an enterprise account in the UI. So, for that, I get the members
sub-collection. But now I also need to get the enterprise user document for each member to show their names etc.
Is that the recommended approach for the data model in Firestore or should I be duplicating the member name in the member sub-collection as well? That would also mean that if an enterprise user changes their name, then I will need to find which all enterprise account that user is a member of and update the member name in those accounts as well.
Similarly, the members
are stored in many places. For e.g., they are part of a team as well. Please let me know if you observe any other problems in the data model.