1

I'm trying to make a database that has posts posted by a user and only persons subscribed to this person can see the post . the problem here is that I add an event listener on parent node on hope that it returns child nodes that only apply to the read rule written inside the children , I do know about the "Rules are not filters" section in the documentation of fire-base and I can't reach a good time/memory efficient design where I only retrieve posts that apply to my rules. That's my database

{
  "Users" : { // ids of users using the app
    "6Ya6nxqRy1fKt4BUOOh1brCJzvi2" : true,
    "Nxmo8Wlhlcg4GDvh7ZIAnmBbpS82" : true,
  },
  "followers" : {
    "6Ya6nxqRy1fKt4BUOOh1brCJzvi2" : { //followers to the user with this id 
      "Nxmo8Wlhlcg4GDvh7ZIAnmBbpS82" : false,
      "n9jTaFVPDNh6bJSSZZCT20UFA972" : true
    },
    "Nxmo8Wlhlcg4GDvh7ZIAnmBbpS82" : {
      "6Ya6nxqRy1fKt4BUOOh1brCJzvi2" : true,
      "n9jTaFVPDNh6bJSSZZCT20UFA972" : true
    }
  },
  "posts" : {
    "-M3YxODIv6frqFyGM4Nm" : { //post with a random generated Id
      "img" : "cd4aaff5-cab9-44d6-b7d9-7587ed2f19fc",
      "post" : "See this cool character !!",
      "user" : "Nxmo8Wlhlcg4GDvh7ZIAnmBbpS82"
    }
  }
}

} and that's what i want to apply

 {
 "rules": {
        ".write": true ,
        "posts":{
         "$pid":{
             ".read": "data.child('user').val()===auth.uid ||root.child('followers').child(auth.uid).child(data.child('user').val()).val()=== true "
           }
        },
        "followers":{
              ".read": true
        },
        "Users":{    
              ".read": true,
        }
     }
 }

the problem arises when I want to add event listener on "posts" node but as it doesn't have read : true so it doesn't return anything.

the only solution I could think of was to make another tree on same level of posts which will contain all posts names and then i start making single event listener on each post node (replace child(posts) to child(posts + post[i] ))

Is there any better option because in case of 100 post I will make 100 single event task which I think of as time ,memory and Internet unnecessary consuming

Omar Shawky
  • 1,242
  • 1
  • 12
  • 25
  • Firing a separate request for every post is not nearly as slow as you may thing, since Firebase can pipeline those requests over its existing connection. See https://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786 – Frank van Puffelen Apr 01 '20 at 04:14
  • Is it guaranteed to return items in the same order I requested them ?,is there any chance that request 5 return before request 1 ? :( – Omar Shawky Apr 01 '20 at 08:54
  • Yes. Since all requests go over a single connection, they're guaranteed to process in order by the database, and results are guaranteed to come back in order too. – Frank van Puffelen Apr 01 '20 at 14:32

0 Answers0