简体   繁体   中英

Users & Friends Design and Implementation with Firebase (AngularFire)

I'm trying to use AngularFire for my Google Firebase Ionic app. I'm particularly facing issues with the design of my database and the implementation afterwards.

My goal is to have users, with some basic info like name and e-mail.

Every user should be able to add friends. These friends are stored in a list. What is the best way to go about this in the Firebase Database? Should users have a friendlist node pointing to other user id's?

Example:

User
   User1
       Name
       Email
       Friends
          Friend1
          Friend2

Or should there be a seperate "friendships" tree with nodes that take two user id's and therefore connect them? Example:

Friendships
    Friendship1
        User1
        User2
    Friendship2
        etc...

Also, given the first option I described, which I implemented so far, I retrieve my friends the following way:

    var friendsRef = firebase.database().ref('users/' + firebase.auth().currentUser.uid + '/friends');

    $scope.friends = $firebaseArray(friendsRef);

This works great. The problem becomes, how do I retrieve the data that's associated to each friend in their own database user location, like name and e-mail?

I'm hoping someone can give me some suggestions to help me along the way as I've been trying to figure this out for a long time now. Thanks a lot!

I would go with the first option. But with flattening the data.

  users
    user1
      name
      email
    user2
    user3
  friendlist
    user1
      user2
      user5
      user11
    user2
    user3

that way you won't retrieve the whole data (with friendlist) when querying for a users data.

Use update() method to save data in multiple location at once. If someone wants to delete a friend a user, you can update() to remove both user from each other's list.

Use security rules to control who can see who's data. For example a user1 's data will be visible to user1 or those who have their userid in friendlist/user1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM