Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write .indexOn for dynamic keys in firebase?

I have data set up like this:

Tasks:{
  Group 1:{
    pushid1:{
      date: 18921949021,
      title: "Do something"
    },
    pushid2:{
      date: 18921949021,
      title: "Do something else"
    }
  },
  Group 2:{
    pushid3:{
      date: 18921949021,
      title: "Do another thing"
    }
  }
}

I reference it by date so I only get the tasks made after a certain time.

var now = Date.now();
var taskRef = new Firebase(FB + "/tasks/" + thisGroup);
taskRef.orderByChild('date').startAt(now).on("child_added", function(snap){
  console.log(snap.val());
});

This all works fine, for each group I get a warning message saying something like this:

FIREBASE WARNING: Using an unspecified index.  Consider adding ".indexOn": "date" at tasks/Group 1 to your security rules for better performance

How do I add that to my security rules? I can't add one for each group a user makes, is there a wild card entry of some type?

like image 705
Marty.H Avatar asked Oct 19 '15 05:10

Marty.H


People also ask

How do I get a key in firebase?

Firebase automatically creates API keys for your project when you do any of the following: Create a Firebase project > Browser key auto-created. Create a Firebase Apple App > iOS key auto-created. Create a Firebase Android App > Android key auto-created.

What is key and value in firebase?

In Firebase Database everything is a node, that follows the pattern key: value. Firebase Database provides us with a simple way to generate unique keys. Unique keys create new items while uploading data to a previously stored key will update.

How many indexes types are supported in the firebase realtime database?

Cloud Firestore uses two types of indexes: single-field and composite.


1 Answers

I guess you're looking for:

{   ".read": true,   ".write": true,   "rules": {     "Tasks": {       "$groupid": {         ".indexOn": "date"       }     }   } } 

The Firebase documentation is usually a pretty good read. Spending an hour or less in the security guide, will ensure you have a better understanding of this and many other topics.

like image 144
Frank van Puffelen Avatar answered Sep 21 '22 01:09

Frank van Puffelen