I am working with data stored in firebase, utilizing a flat structure with key-value pairs to establish a many-to-many relationship between events and users (see figure 1). While it's straightforward to look up events associated with a user using pure JavaScript (as shown in figure 2), I am facing challenges when attempting to achieve the same using angularfire and firebaseObject or array. Does anyone have insights on how to execute such a query efficiently?
Figure 1.
{
users: {
user_id1: {
events: {
event_id1: true,
event_id2: true
}
},
user_id2: {
events: {
event_id3: true,
event_id4: true
}
},
user_idN...
},
events: {
event_id1: {
users: {
user_id1: true
}
},
event_id2: {
users: {
user_id1: true
}
},
event_idN...
}
}
Figure 2
// List all of user_id1's events
var ref = new Firebase("https://<<example>>.firebaseio.com/");
// fetch a list of user_id1's events
ref.child("users/user_id1/events").on('child_added', function(snapshot) {
// for each event, fetch it and print it
String groupKey = snapshot.key();
ref.child("events/" + groupKey).once('value', function(snapshot) {
console.log(snapshot.val());
});
});