I have database json structure as:
{
"notifications" : {
"approved" : {
"notification_1" : {..},
"notification_2" : {..},
"notification_4" : {..},
"notification_6" : {..}
},
"pending" : {
"notification_3" : {..},
"notification_5" : {..},
"notification_7" : {..},
"notification_8" : {..}
}
},
"users" : {
"some_user" : {
"sent_notifications" : {
"notification_1" : "notification_1",
"notification_2" : "notification_2",
"notification_4" : "notification_4",
"notification_6" : "notification_6"
}
}
}
Security rules are as:
{
"rules": {
"notifications": {
"approved": {
".read" : true,
".write" : "auth.uid === 'admin'"
},
"pending": {
".write" : "auth != null && (newData.child('by_user').val() === auth.uid || auth.uid === 'admin')",
"$id" : {
".read" : "data.child('by_user').val() == auth.uid || auth.uid == 'admin'"
}
}
},
"users" : {
"$user_id":{
".read" : "auth.uid === $user_id",
".write": "auth.uid === $user_id"
}
}
}
}
Here, 'sent_notifications' stores notification id's which were added by user. On client side(Android), I wish to get list of those objects from notifications/approved
, and notifications/pending
, whose keys are in 'sent_notifications'. When a user sends a notification, that notification data goes in 'pending' and the key of that notification is stored in 'sent_notification'. After moderation admin would then move the data from pending to approved node. With any method I find myself doing a lot of queries on server to get all the nodes one by one. I need a better way to do this.
I am a newbie to firebase database. This may not be the best way to implement the database structure. I needed notifications/approved
to be visible to general public and notifications/pending/$id
to be visible only to user who is sending notification. This is the reference for above structure. If there is better way to implement it, please help me with that as well.