So here are the available triggers for Firestore cloud functions:
https://firebase.google.com/docs/firestore/extend-with-functions
onCreate
functions.firestore
.document('my-collection/{doc-id}')
.onCreate((snap, context) => { /* ... */ });
onDelete
functions.firestore
.document('my-collection/{doc-id}')
.onDelete((snap, context) => { /* ... */ });
onUpdate
functions.firestore
.document('my-collection/{doc-id}')
.onUpdate((change, context) => { /* ... */ });
onWrite
functions.firestore
.document('my-collection/{doc-id}')
.onWrite((change, context) => { /* ... */ });
I'm converting my project to Typescript. What types should I use for the params change context and snap?
Here are the types:
onCreate:
snapshot: FirebaseFirestore.QueryDocumentSnapshot
context: functions.EventContext
onDelete:
snapshot: FirebaseFirestore.QueryDocumentSnapshot
context: functions.EventContext
onUpdate:
change: functions.Change<FirebaseFirestore.QueryDocumentSnapshot>
context: functions.EventContext
onWrite:
change: functions.Change<FirebaseFirestore.DocumentSnapshot>
context: functions.EventContext
More details here and here in the doc.
If you are using Typescript, you would import/use like this:
import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
...
export const fnSomeCollectionTriggerOnUpdate =
functions.firestore.document('somecollection/{docId}')
.onUpdate(async (change: functions.Change<admin.firestore.QueryDocumentSnapshot>,
context: functions.EventContext) => {
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With