Q:

Cloud function to deduct values from a user balance

I'm using cloud functions quite heavily in an app I'm building and wanted some advise how best to tackle this problem.
My function looks like this
exports.deductFromBalance = functions.database.ref('listings{listing}').onCreate((snap, context) => {
  const author = snap.val().uid;
  return admin.database().ref(`balances/${snap.val().uid}`).update({
    balance: //current balance -= 1
  })
})

I need a way to reduce the balance by one each time a listing is successfully created. Is there a way for me to reduce this value without having to read the current value?

A:

There are no atomic increment/decrement operations in the Firebase Realtime Database. You'll need to use a transaction to set the new value based on the current value atomically.

