FirebaseExtended / bolt

Bolt Compiler (Firebase Security and Modeling)
Apache License 2.0
897 stars 107 forks source link

feature request: Treat colon as part of string for dot notation #192

Closed WhatsThatItsPat closed 7 years ago

WhatsThatItsPat commented 7 years ago

With flat data structures / denormalization in mind, I use a variation of dot notation for higher detail collections. But in Firebase, dots are reserved and can't be in node keys, so I use the colon.

So instead of:

"collection": {
  "$itemId": {
    "member1": "1",
    "member2": "2",
    "member3": "3"
  }
}

I flatten it to:

"collection": {
  "$itemId": {
    "member1": "1"
  }
}

"collection:member2": {
  "$itemId": "2"
}

"collection:member3": {
  "$itemId": "3"
}

I like this organizational practice and it makes the database in the FB console look pretty. The one slight annoyance in Bolt is that the colons force me to use bracket notation to access the flattened collections:

prior(root).path.to.collection[itemId].whatever
// vs.
prior(root).path.to.["collection:member2"][itemId].whatever

Would it be possible to allow the colon (and other non-reserved characters) to be treated as \w characters so dot notation could be used for property access? Then we could do:

prior(root).path.to.collection:member2[itemId].whatever

The colon presents no issues in path declarations:

path / {

  /collection/{itemId} {
    read() {true}
    write() {true}
  }

  /collection:member2/{itemId} {
    read() {true}
    write() {true}
  }

  ...

}

It would be nice to look at my Bolt file and immediately know that in any brackets I am intentionally creating a string from variables, instead of taking a second to see if I'm just escaping one of my colon separators.

I'm considering switching to the underscore as my separator, but that conflicts with other uses for the underscore.

rockwotj commented 7 years ago

While possible, I don't think this leads to a clear/maintainable rules file, besides : is an allowed key as well as _ or most other characters other than what is documented here: https://firebase.google.com/docs/database/web/structure-data#how_data_is_structured_its_a_json_tree