Created
December 8, 2020 19:50
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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