2
votes

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?

1
TypeScript should infer the types of the parameters without you doing anything in the code. If your code editor is smart about TS, it should show you the inferred types.Doug Stevenson
Thanks, Doug. Did not know that. That's because I was declaring my functions in separate files and out of the function.firestore.document().onUpdate() triggers. I was only adding this part in index.ts where I was importing and re-exporting them inside the triggers. But will use it like this from now on.cbdeveloper

1 Answers

6
votes

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) => {
...
}