Assuming I have the following firebase structure
{
"users":{
"user1":{
"username":"john",
"full_name":"John Vincent",
"created_at":"9th Feb 2015",
"groups":{
"group1":true,
"group3":true
}
"last_logins":...
},
"user2": ...,
"user3": ...
}
"groups": {
"group1"{
"group_name":"Administrators",
"group_description":"Users who can do anything!",
"no_of_users":2,
"members":{
"user1":true,
"user3":true
}
},
"group2"{
"group_name":"Moderators",
"group_description":"Users who can only moderate!",
"no_of_users":1,
"members":{
"user2":true
}
}
}
}
Now if i want to get a list of users in group 1, it would be easy, but how do i get a list of users in group 1 with their username, full_name etc? do i have to get a list of ids first and then do a foreach to get each individual user info and then add them to an array? I assume there must be a better way.
For example: If i click on a group, I want to show a list of users with their full_name displayed first, so I modified the above structure:
{
"users":{
"user1":{
"username":"john",
"full_name":"John Vincent",
"created_at":"9th Feb 2015",
"groups":{
"group1":true,
"group3":true
}
"last_logins":...
},
"user2": ...,
"user3": ...
}
"groups": {
"group1"{
"group_name":"Administrators",
"group_description":"Users who can do anything!",
"no_of_users":2,
"members":{
"user1": {
"full_name":"John Vincent"
}
"user3": {
"full_name":"John Doe"
}
}
},
"group2"{
"group_name":"Moderators",
"group_description":"Users who can only moderate!",
"no_of_users":1,
"members":{
"user1": {
"full_name":"John Vincent"
}
}
}
}
}
Now full_name is duplicated at 3 different places for user1, possibly even more depending on how many groups he's in, how do I then do a firebase multi-location update? I've read about multi-location update online, it seems the ending "id" for the multi-location update needs to be the same. In the above case, if i update user1 full_name, how do i update it inside different groups too???
UPDATE
Based on Frank's answer:
In your code a simple multi-location update would write these locations:
"/users/user1/full_name": "New Name", "/groups/group1/members/user1/full_name": "New Name" "/groups/group2/members/user1/full_name": "New Name"
The above code assumes the user is in group1 and group2, but he could possibly in other groups too, how are you suppose to get a list of groups because you never know which group he is in without checking each one, so it ends up in a foreach loop again checking the user groups first? And thanks again Frank, Yes i assumed doing a foreach loop would be a very time consuming process, I guess that's not the case with firebase. I will experiment with this.
UPDATE 2
So i am testing this using angularfire2, here is my original code:
tours: any[];
this.firebase.get_user_tours().snapshotChanges()
.takeUntil(this.ngUnsubscribe).subscribe(tours => {
this.tours = tours
}
and now i am using this because i am changing my database structure.
tours = [];
this.firebase.get_user_tours().snapshotChanges()
.takeUntil(this.ngUnsubscribe).subscribe(tours => {
tours.forEach(tour => {
this.firebase.get_tour(tour.key).snapshotChanges()
.takeUntil(this.ngUnsubscribe).subscribe(tour => {
this.tours.push(tour)
})
})
}
both works, but with pushing an object into an array, everytime i make an update to a tour, it adds a new object into the array instead of updating the original one, which makes sense, but is this what i'm suppose to do?