3

I have this structure

"Following": {
      ".validate": newData != null && newData.child(count).val() == root.child(Following).child($User).child(count).val() + 1),
      "$User": {
        "$Following": {
          "created": {}
        },
        "count": {}
      }
    }

i want to prevent that the count variable can be incremented or decremented without the creation or the delete of a following

for prevent the increment without the creation of a new following i add this rule

".validate": newData != null && 
             newData.child(count).val() == 
                     root.child(Following).child($User).child(count).val() + 1)

but i have problem to prevent the decrement without the delete of a following.

the my ask is this

how can I decrease the count variable only if I know that the Following is going to be deleted ?

sorry for bad english :/

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
DThink
  • 429
  • 2
  • 18
  • 1
    Hey @DThink. Could you take some time explaning a little better what exactly you are trying to do so we can help? :) – adolfosrs Jun 21 '16 at 17:48
  • very sorry for my bad english now is comprensible? – DThink Jun 21 '16 at 18:46
  • much better! IMO you should not be storing this count stuff on the database. Keep the `Followings` data as you want and to count them use [numChildren()](https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#numChildren) on the application side. – adolfosrs Jun 21 '16 at 19:21
  • yes in the past i try this but this function retrieve all data and after you can count it. for example. if you have 100.000 photos you must get all photo address and after you can use the numChildren() function. i think that is a underperforming method. Unfortunately i see that in firebase not exist a count query like sql language. im not a firebase expert for you not exist a solution in server side? – DThink Jun 21 '16 at 19:43
  • I just added an update to my [answer here](http://stackoverflow.com/questions/37954217/firebase-database-security-issue/37956590#37956590) that will likely help you. – Frank van Puffelen Jun 24 '16 at 19:35
  • very thanks perfect :) – DThink Jun 30 '16 at 13:56

1 Answers1

0

What do you think of this method?

    "Event": {
          "$User": {
            "counter": {
              ".validate": "newData.hasChildren(['value', 'tmp'])",
              "value": {},
              "tmp": {},
              "$other": {
                ".validate": "false"
              },
              ".write": "data.val() != null && newData.val() != null && (newData.parent().child(newData.child('tmp').val()).val() != null && newData.child('value').val() == data.child('value').val() + 1 || root.child('Event').child($User).child(newData.child('tmp').val()).val() != null && newData.parent().child(newData.child('tmp').val()).val() == null && newData.child('value').val() == data.child('value').val() - 1)"
            },
            "$Event": {
              //some variable and permissions
            }
          }
        }

i use the tmp variable to recognize the event key that i want verify if is in creation or in delete and consequently i update the value variable . in the example i add only the update rules without other rules to not get in confusion

DThink
  • 429
  • 2
  • 18