I just play with firebase, storing user blog posts and the number of likes and dislikes,
I store a message
/posts/$uid/$key
(for example: /posts/XFSDFSFSDFSDF/-KKm1gBQRivx1x-thh8m)
And to query for all posts, I also retain the same data /timeline/$key
(for example: /timeline/-KKm1gBQRivx1x-thh8m)
Like dislike, my safety rules are something like this.
{
"rules": {
".read": true,
"posts":{
"$uid":{
".write": "$uid === auth.uid"
}
},
"timeline":{
"$key":{
".validate": "root.child('posts/'+ auth.uid +'/'+$key).exists()",
".write": "auth != null"
}
},
"user-likes": {
"$uid":{
"$post":{
".write": "$uid === auth.uid",
".validate":"root.child('timeline/'+$post).exists() && newData.child('type').exists()"
}
}
},
"likes": {
"$post":{
"$key":{
".validate": "root.child('user-likes/'+ auth.uid +'/'+$post+'/'+$key+'/ type').val()==newData.child('type').val()",
".write": "auth != null",
".indexOn": ["type"]
}
}
}
}
}
Is there a problem with saving data twice? Is there any better solution for this?
source
share