How to structure search query and for twitter-esque feed?

时间:2016-07-11 18:57:34

标签: firebase firebase-realtime-database

I've seen the firefeed example, but unfortunately it's a little lacking in functionality and I'm not sure how to move forward.

Given a data structure of users and posts:

{
  "posts":
    "12": {
      "uid": 14,
      "message": "This is my message @16 @55"
      "timestamp": 1459361875666
      "mentions": {
        0: 16,
        1: 55
      }
    }
    "13": {  
      "uid": 55,
      "message": "This is another message"
      "timestamp": 1469861245622,
      "mentions": null
    }
  }

  "users": {
    "14": {
      "following": {
        1: 16,
        2: 55
      }
    }
    "16": {
      "following": {
        0: 55
      }
    }
    "55": {
      "following": {
        0: 14
      }
    }
  }
}

How would I construct the query to bring back all the posts for users I'm following, as well as the posts I'm mentioned in? I can't find any recent documentation on advanced queries...

For example, if I am user 16, I should see all posts by user 55 (a user I am following) PLUS the post (id 12) by user 14 since they mentioned me in descending timestamp order.

Should I rethink the way the data is structured for performance purposes? Any help would be greatly appreciated.

EDIT: restructured IDs to be unique

1 个答案:

答案 0 :(得分:1)

all the posts for users I'm following

// Reference
var ref = firebase.database().ref("users").child("some-user").child("following");
// Data call
ref.once("value).then(function(snapshot) {
    // Iterate each following
    snapshot.forEach(function(childSnapshot) {
        // value
        var childData = childSnapshot.val();
        var followRef = firebase.database().ref("posts").orderByChild("uid").equalTo(i);
        followRef.once("value").then(function(grandChildSnapshot) {
            // RETURN OF ALL POSTS
        }
    });
});

Objects must have unique keys inside so for your data structure make sure they're not all called post, I recommend using push() and they would all get unique ids.

posts I'm mentioned in?

"users": {
"user": {
  "id": 14,
  "following": {
    1: 16,
    2: 55
  },
  mentioned: {
     some-post: the-user,
     ...
  }
}
...

You can keep your data structure the way it is, but also add to each follower the posts he is mentioned in.

// When post is pushed, get all users mentioned
...
var ref = firebase.database().ref("users").child(some-mentioned-user).child("mentioned");
ref.update({ <some-post> : <user-who-posted> });

After

var mentionedRef = firebase.database().ref("users").child(some-mentioned-user).child("mentioned");
mentionedRef.once("value").then(function(snapshot) {
    // Iterate each following
    snapshot.forEach(function(childSnapshot) {
        // key
        var childKey = childSnapshot.key();
        var postRef = firebase.database().ref("posts").child(childKey);
        postRef.once("value").then(function(grandChildSnapshot) {
             // RETURN OF ALL POSTS
        })
    });
});

Comment with any questions.