I'm building a chat app with Firebase (and AngularJS) and I have a data structure that is similar to the one on this Firebase documentation page. This structure is good for not having to retrieve huge amounts of unneeded data but I don't seem to understand a very simple thing.
With my data looking like below, when a user connects to my app:
How do I retrieve their 10 most recently updated groups and keep this list updated as new messages are posted in groups?
// An index to track Ada's memberships
{
"users": {
"alovelace": {
"name": "Ada Lovelace",
// Index Ada's groups in her profile
"groups": {
// the value here doesn't matter, just that the key exists
"techpioneers": true,
"womentechmakers": true
}
},
...
},
"groups": {
"techpioneers": {
"name": "Historical Tech Pioneers",
"members": {
"alovelace": true,
"ghopper": true,
"eclarke": true
},
"lastUpdateTime": [SOME TIMESTAMP HERE]
},
...
}
}
More information if you care to read
As you can see, I've added "lastUpdateTime": [SOME TIMESTAMP HERE]
to the code above because it's how I do it for my app. I can't figure out what should be the "refresh process" for a user group list.
If my user has 100 groups, should I retrieve a list of the group IDs and get the actual groups one by one to be able to only keep the 10 most recent (I'm pretty sure this is not the way to go)?
Also, whenever someone posts a message in a group, it will update the lastUpdateTime
in Firebase but how do I keep the user group list synchronized to this?
I've tried some very ugly combinations of child events, orderBys as well as entire chains of functions executing whenever something fires but it doesn't work and seems extremely complicated, for nothing. The whole idea of flattening the data is to keep the queries/fetching to a minimum and I feel that what I have done so far is way too heavy.