[Firebase] Firebase : rules examples

260 views
Skip to first unread message

Michael Ahern

unread,
Nov 16, 2015, 10:05:30 AM11/16/15
to fireba...@googlegroups.com
Hi, 

I'm new to rules and have seen some back and forth on this question on stackexchange here: http://stackoverflow.com/questions/27179094/using-indexon-with-nested-keys-in-firebase

Is is possible to have an .indexOn that is nested below two dynamic keys, the first being $userid and the second being an $id value under a static subkey.

Thanks in advance for any ideas or suggestions.

The data looks like:

{
    "users" :  {
      "$userid" : {
        "subkey" : "$id" { "timestamp" : "timevalue" }
    }
}

Will this rule work to index queries on "users/$userid/subkey/$id/timestamp" ?

{
    "rules": {
      "users": { 
        "$userid": {
        ".read": "auth !== null && (auth.uid == $userid)",
        ".write": "auth !== null && auth.uid == $userid",
        "subkey": { "$id" : { ".indexOn" : ["timestamp"] } }
        }
      }  
    }
}

Mike Koss

unread,
Nov 17, 2015, 5:28:26 PM11/17/15
to fireba...@googlegroups.com
I think the answer is yes you can.  But it looks to me like you have the .indexOn one level too deep in your rules.  Since subkey is the collection, and $id is an individual instance, the .indexOn should be defined at the collection level (immediate child of the subkey rule).

Documented here:


--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/CANzFfYDqZykdYT-igfSpe20pcWj3O3B52S5oy7GkU_otf8yxSA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Michael Ahern

unread,
Nov 17, 2015, 5:35:58 PM11/17/15
to fireba...@googlegroups.com
Got it, like below then. Thanks!

{
    "rules": {
      "users": { 
        "$userid": {
        ".read": "auth !== null && (auth.uid == $userid)",
        ".write": "auth !== null && auth.uid == $userid",
        "subkey": { ".indexOn" : ["timestamp"] } 
        }
      }  
    }
}

Mike Koss

unread,
Nov 17, 2015, 8:58:07 PM11/17/15
to fireba...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages