
Check Firestore Function Type
2 min read
Sometimes you want to check what kind of onWrite() function you are dealing with in Firestore:
functions.firestore
.document('posts/{docId}')
.onWrite(async (change: any, context: any) => {
//... code
}
So, I created some simple functions to find out. First install the package:
npm i adv-firestore-functions
Then import the functions you want:
import { createDoc, updateDoc, deleteDoc } from 'adv-firestore-functions';
or for more complex checks like:
writeDoc = createDoc || updateDoc
shiftDoc = createDoc || deleteDoc
popDoc = updateDoc || deleteDoc
import { writeDoc, shiftDoc, popDoc } from 'adv-firestore-functions';
And use it like so:
if (createDoc(change)) {
// a document is being created, so do something...
}
Simple.
J
firebase
cloud-functions
cloud-firestore