0

In my Firebase I have the idea of users and friends.

I am unsure what the best solution to storing and retrieving friends against a user is.

Currently, I have a users node which each user is stored with a unique id from social auth.

After reading about storing data and not to nest data I have come up with this so far. Each user has a 'friends' node which contains the unique id of another user (see below)

"users" : {
"facebook:10207817038895574" : {
  "appearOffline" : false,
  "avatar" : "https://scontent.xx.fbcdn.net/hprofile-xap1/v/t1.0-1/p100x100/10314019_10207798442030664_281467953303549494_n.jpg?oh=ac439386757bf533b56a7ea92022e304&oe=57732581",
  "email" : "chris-barnett@live.co.uk",
  "friends" : {
    "google:110510990218136599589" : true
  },
  "id" : "facebook:10207817038895574",
  "lastActive" : "2016-04-18T15:11:16.463Z",
  "location" : {
    "lastUpdate" : "",
    "lat" : 0,
    "lng" : 0
  },
  "name" : "Chris Barnett",
  "status" : "Offline"
},
"google:110510990218136599589" : {
  "appearOffline" : false,
  "avatar" : "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg",
  "email" : "app.hub.cb@gmail.com",
  "friends" : {
    "facebook:10207817038895574" : true
  },
  "id" : "google:110510990218136599589",
  "lastActive" : "2016-04-18T14:48:23.238Z",
  "location" : {
    "lastUpdate" : "",
    "lat" : 0,
    "lng" : 0
  },
  "name" : "Hub App",
  "sentFriendRequests" : {
    "twitter:95970727" : false
  },
  "status" : "Offline"
},
"twitter:95970727" : {
  "appearOffline" : false,
  "avatar" : "https://pbs.twimg.com/profile_images/692823737683578883/RxMpiuco_normal.jpg",
  "email" : "-",
  "id" : "twitter:95970727",
  "lastActive" : "2016-04-18T14:30:40.160Z",
  "location" : {
    "lastUpdate" : "",
    "lat" : 0,
    "lng" : 0
  },
  "name" : "Chris Barnett",
  "receivedFriendRequests" : {
    "google:110510990218136599589" : false
  },
  "status" : "Offline"
}

}

What is the best approach for displaying all data for each friend for a user? Currently I would have to download the entire users node and do a check to see if each users unique id is in a users list of 'friends' and if it is then display their data in my view. I guess this would be ok for a small amount of users but if this was to scale then I imagine it would be very inefficient?

I am using the JavaScript SDK.

Many thanks.

Chris
  • 3,113
  • 5
  • 24
  • 46
  • @FrankvanPuffelen hi Frank, apologies. Have added the JSON tree. thanks – Chris Apr 18 '16 at 15:15
  • @FrankvanPuffelen also Frank, I have seen you have answered quite a few questions relating to Firebase.. what do you think of my structure currently? Any feedback is welcome! Thanks – Chris Apr 18 '16 at 15:17
  • Thanks for adding that JSON. Cleaning up comments. – Frank van Puffelen Apr 18 '16 at 16:02
  • 1
    "What is the best approach for displaying all data for each friend for a user?" The best approach is the one that works for all of your use-cases. Since we can't know those (and likely neither do you at this moment), aiming for a best approach is a case of premature optimization. That said: you'd loop over the friend ids and look them up with a `once()`. If you're worried about performance, read [this answer](http://stackoverflow.com/a/35932786/209103). And in general, read [this guide](https://www.firebase.com/docs/web/guide/structuring-data.html)/ – Frank van Puffelen Apr 18 '16 at 16:05

0 Answers0