Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Types for Firestore triggered cloud function params?

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?

like image 326
cbdeveloper Avatar asked Jun 17 '26 13:06

cbdeveloper


1 Answers

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

like image 193
Renaud Tarnec Avatar answered Jun 20 '26 03:06

Renaud Tarnec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!