I've made a cloud function for removing the expired alert messages and my data is structured like below:
Alerts
|-{country1}
|-{c1_state1}
|-{key}
|-msg: "bla bla"
|-end: 1601251200000
|-{c1_state2}
|-{key}
|-msg: "bla bla"
|-end: 1601251200000
|-{country2}
|-{c2_state1}
|-{key}
|-msg: "bla bla"
|-end: 1601251200000
|-{c2_state2}
|-{key}
|-msg: "bla bla"
|-end: 1601251200000
Looking at the log messages, I noticed that there are lots of warnings for each of the query in the for loop (states
variable).
[2020-09-29T02:04:28.585Z] @firebase/database: FIREBASE WARNING: Using an unspecified index. Your data will be downloaded and filtered on the client. Consider adding ".indexOn": "end" at /Alerts/BR/RR to your security rules for better performance.
I've searched a lot about setting rules in firebase database, but I couldn't put the rules to work. In my database, I am looping in countries and states and that's why I used wildcards ($coutry
and $state
)
{
/* Visit https://firebase.google.com/docs/database/security to learn more about security rules. */
"rules": {
".read": "auth != null",
".write": "auth != null",
"Alerts": {
"$country": {
"Sstate": {
".indexOn": ["end"]
}
}
}
}
}
My function works and the data get deleted as expected, but the warnings keep coming.
exports.closeAnnouncementsBRTZ3 = functions.pubsub
.schedule('10 0 * * *')
.timeZone('America/Sao_Paulo') // Users can choose timezone - default is America/Los_Angeles
.onRun((context) => {
const expireTime = 1601251200000;
const ref = admin.database().ref().child('Alerts').child('BR');
const states = ['AC', 'AM', 'MS', 'MT', 'RO', 'RR'];
return Promise.all(states.map(async (state) => {
return await ref.child(state).orderByChild('end').endAt(expireTime).once('value', (dataSnapshot) => {
console.log('await dataSnapshot: ', state);
if (dataSnapshot.val() !== null) {
dataSnapshot.forEach(childSnapshot => {
console.log('child to be removed: ', childSnapshot.key);
childSnapshot.ref.remove();
});
}
});
}));
});
So, how to set the rules properly in a way that improves the performance of my queries and without warnings?