2

I have read a good article on how to structure a firebase database for a chat app and I ended app in this structure:

{
  "users":{
    "autoIdUser1":{
        "username":"john",
        "full_name":"John Vincent",
        "groups":{
            "autoIdGroup1":true,
            "autoIdGroup2":true
        }
    },
    "autoIdUser2": ...,
    "autoIdUser3": ...
  }
  "groups": {
     "autoIdGroup1"{
        "group_name":"Administrators",
        "group_description":"Users who can do anything!",
        "members":{
            "autoIdUser1":true,
            "autoIdUser2":true
        }
      },
     "autoIdGroup2"{
        "group_name":"Moderators",
        "group_description":"Users who can only moderate!",
        "members":{
            "autoIdUser1":true
        }
      }
   }
 }

Now I have to retrive from my db all rooms where a member is in

What I tried to do is to do a query to check if in each group is there a specific user id under members but I can't figure how...

I also thanked about another way creating another structure in db like this:

 "membersInGroups":{

     "autoIdGroup1":{
            "autoIdMember1":true,
            "autoIdMember3":true
      }
    "autoIdGroup2":{
            "autoIdMember1":true
      }
  }

and then create a query to retrive all groups id where a user is in and use those to query again all info for each group. The style of query is the same of that I have to make with old structure but is simplier

Which method is the best?

If I add the second structure, can I remove "members" child node??

Can I see an example of the query for the existing structure?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
HaVaNa7
  • 330
  • 1
  • 3
  • 14

1 Answers1

1

Make sure to structure your data as streams whenever possible, meaning long, shallow lists of data. Don’t nest any more than is necessary for your needs.

Also, do not be afraid to duplicate data such as usernames, user ids, object titles, etc. Try to match your data structure to your UI.

Famic Tech
  • 280
  • 5
  • 20