Let's say I have a collection of Employees and a collection of Departments. And I have cloud functions set to update the employees count in a department whenever an employee is created or deleted.
import * as functions from "firebase-functions";
import admin = require("firebase-admin");
export const handleEmployeeDeletion = functions.firestore
.document('Employee/{employeeId}')
.onDelete((snapshot, context) => {
const departmentRef = admin.firestore()
.collection("Department").doc(snapshot.data.departmentId);
return departmentRef.update({
employeesCount: admin.firestore.FieldValue.increment(-1)
})
})
What happens if two employee documents are deleted exactly at the same time? Am I safe from double-spending as long as I'm using the firestore.FieldValue.increment rather than reading the current value to the RAM of the Cloud Function and then writing the updated (And possibly outdated) value from there?
How about if the change that needs to be done to the field involves more calculations than simply incrementing? How can I make the changes while making sure there's no chance multiple sessions will be modifying the same field at the same time after reading the same old value of the field?