Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 726
Shrinidhi Rao Avatar asked Nov 26 '25 10:11

Shrinidhi Rao


1 Answers

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

like image 68
Ravi Prakash Verma Avatar answered Nov 28 '25 02:11

Ravi Prakash Verma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!