Shrinidhi Rao
Shrinidhi Rao

Reputation: 275

How to write to Firebase table only if its greater than current value in table

I need to update the table level value in Firebase score table only if the table value is less than the value currently being posted to update. So the KeyOfLevel value never decreases.

Since the client side involved too much calls to fetch and check. I need to do this in rules tab of Firebase. And I don't exactly know how to write those rules.

Eg:

score: 
{
    "1234567890": 
    {
        CoinsKey: 1000,
        KeyOfLevel: 5
    }
}

where 123456789 is the user ID.

Upvotes: 3

Views: 1270

Answers (1)

Ravi Prakash Verma
Ravi Prakash Verma

Reputation: 1484

You might want to write security rules like this.

{
   "rules": {
      "score": {
        "$userId": {
           "KeyOfLevel": {
              ".validate": "!data.exists() || data.val() < newData.val()"
           }
         }
      }
   }
} 

In place of .validate you can also use .write

Upvotes: 8

Related Questions