I would like to give multiple users access to fetch "messages" they have access to in a Firebase Realtime database. The database fetch reads "/messages" and returns two collections of all messages the user has access as "author" and "member".
Database structure looks like this:
"messages" : {
"msg_id1": {
"access": {
"author": "user-id-1",
"members": {
"user-id-2": true
}
}
"data" : {
"theData": "Hello world!"
}
}
}
I have created the following rule:
{
"rules": {
"messages": {
".read": "
auth.uid !== null &&
query.equalTo === auth.uid
"
}
}
The corresponding database queries are:
database.ref("messages")
.orderByChild("access/author")
.equalTo(`${user.uid}`)
.once('value', (snapshot) => {
snapshot.forEach( (data) => {
console.log("OWNER", data.val().data.title)
})
})
database.ref("messages")
.orderByChild(`access/members/${user.uid}`)
.equalTo(true)
.once('value', (snapshot) => {
snapshot.forEach( (data) => {
console.log("MEMBER", data.val().message.data.title)
})
})
I wish to minimise the overhead and cost of calling the database twice.
Is there a smart way to optimise the database queries into one single database call?
Kind regards /K