Skip to content

Instantly share code, notes, and snippets.

@katowulf
Created December 8, 2020 19:50
Show Gist options
  • Save katowulf/32ee81e459fba670cc938f5982f215f7 to your computer and use it in GitHub Desktop.
Save katowulf/32ee81e459fba670cc938f5982f215f7 to your computer and use it in GitHub Desktop.
In Firestore node.js, set the max value on a field similar to https://cloud.google.com/firestore/docs/reference/rest/v1/Write#FieldTransform
import firebase from "firebase/app";
import "firebase/firestore";
const randomNumber = Math.floor(Math.random() * 1000);
const docRef = firebase.firestore.doc("path/to/doc");
db.runTransaction(async function(t) {
const doc = await t.get(docRef);
if (doc.exists && doc.data().number < randomNumber) {
console.log("setting new max");
t.update(docRef, { number: randomNumber });
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment