Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add index rules for Firebase database?

I keep getting firebase messages on console regarding to add indexOn for user 4321 at chat rules. Below is my database.

enter image description here

And my rules in firebase is like:

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null",
    "users": {
      ".indexOn": ["name", "email","uid"],
      "$uid": {
        ".write": "$uid === auth.uid"
      }
    },
    "chats": {
      "$key": {
        ".indexOn": ["uid"] 
      }
    }
   }
}

I'm not sure why the indexing not working. And how can I improve indexing for overall database?

like image 996
Soheil Avatar asked Aug 23 '16 04:08

Soheil


2 Answers

Your current data structure only allows to easily list all members of a chatroom, not the other way around. That may be the reason you get that message, because if you want to list all chats that user belongs to, you have to search through all /chats records.

You probably need to duplicate the chat room membership data both at /chat/<chat-id>/members and /users/<uid>/groups. Your case is almost identical to the one in the Firebase guide here -- read particularly the description below code in the section linked, but it's best to read the whole guide, it really helped me to understand how the database works.

Btw: your rule in chats: ".indexOn": ["uid"] doesn't do anything with the sample data you posted. It says to "index chat rooms by their uid attribute", but your chat rooms don't have an uid key inside (meaning uid: 'someid', not 'someid': true). See the indexing guide on more info how indexing works.

like image 107
quezak Avatar answered Nov 15 '22 06:11

quezak


There are two types of indexing orderByValue: and orderByChild

Indexing with orderByValue::

{
  "rules": {
    "scores": {
      ".indexOn": ".value"
    }
  }
}

JSON Tree

{
  "employee": {
    <"employee1Key">: {
      "empName": Rohit Sharma,
      "Designation": SW Developer,
      "empId": EMP776,
      "branchDetails": {
        "branch": Software,
        branchLocation: Pune,
        branchId: BR556
      }
    },
    <"employee2Key>": {
      "empName": Vira tKholi,
      "Designation": Accountant,
      "empId": EMP908,
      "branchDetails": {
        "branch": Finance,
        branchLocation: Dheli,
        branchId: BR557
      }
    },
    <"employee3Key">: {
      "empName": MS Dhoni,
      "Designation": Sales Manager,
      "empId": EMP909,
      "branchDetails": {
        "branch": Sales and Marketing,
        branchLocation: Pune,
        branchId: BR556
      }
    }
  }
}

Indexing with orderByChild:

{
  "rules": {
    "employee": {
      ".indexOn": ["empName", "empId", "designation"]
    }
  }
}

Multilevel Indexing:

{
  "rules": {
    "employee": {
      ".indexOn": ["empName","empId","branchDetails/branch",
        "branchDetails/branchLocation","branchDetails/branchId"]
    }
  }
}

You can also refer to this link for the Firebase Real-Time Database Data Indexing.

Firebase Real-Time Database Data Indexing

like image 30
Vikram Kodag Avatar answered Nov 15 '22 07:11

Vikram Kodag