Is there syntax for watching multiple collections together in a single `onUpdate`?

Viewed 15
exports.eventOnUpdate = functions.firestore //
    .document('collection') //
    .onUpdate(async (snap, context) => {
        // code here
    })

I have multiple collections and subcollections, I need to watch for updates to them in Cloud Functions, to make writes elsewhere. I've scaffolded similar to above.

Am I relegated to defining each onUpdate separately and having them call an external JS function?

Or is there a syntax to watching a group of collections under a single onUpdate?

1 Answers

Cloud Functions triggered by Firestore always listen to collections as a specific path. So you can listen for the subcollection of every root document with:

exports.eventOnUpdate = functions.firestore
    .document('rootcollection/{rootdocid}/subcollection/{subdocid}')
    .onWrite(async (snap, context) => {
        // code here
    })

There's no way to listen across an entire branch though: so you can't listen for all writes to all root documents and all subcollections under it. Whenever I need something like that, I put a field (e.g. lastUpdatedRecursively) in the parent document, and then listen for updates to that document/collection.

Related