1

I have being reading the docs about Cloud firestore Data Model and about Security in Cloud firestore but was unable to find an answer on how to model my data.

I have groups and users and I want to implement a membership relation such that each user can be member in multiple groups and each group could obviously have multiple members.

I want to be able to efficiently be able to tell who are the members of each group and what groups a given user belongs to.

In a relational DB I would have 3 tables, users, groups and user_groups or group_membrs and I am guessing anyone reading this Q could tell how to handle this.

What collections / documents / sub collections should I use to implement the same using firestore?

I am especially concerned with the security setting that would allow a user to remove himself (and only himself) from a group and would allow a group owner/manager to add any user to the group he manages (and only to that group)

Can/Should this be implemented using only collections for users and groups with the memebrship being part of the document data on those collections? or should an additional memeber ship collection be used?

epeleg
  • 10,347
  • 17
  • 101
  • 151

1 Answers1

1

My initial take would be to do this fairly similar to how I recommended modeling many-to-many relationship on the Firebase Realtime Database. The biggest difference with your relational experience, is that you'll typically store the relations in both directions. So in addition to user_groups you'll also have group_users for a total of four collections per entity pair.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807