Should cloud functions be consolidated per trigger, or is it fine to write multiple functions for the same trigger? In particular, if I'm reading documents inside... Are there significant performance or billing implications?
// Example: multiple onWrite functions triggered by the same Firestore doc path
functions.firestore.document('myCollection/{itemId}').onWrite((change, context) => {
// do one complex thing, potentially reading/writing data
}
functions.firestore.document('myCollection/{itemId}').onWrite((change, context) => {
// do another complex thing, potentially reading/writing the same or different data
}
or...
// Example: one trigger and a monolithic function handling everything...
functions.firestore.document('myCollection/{itemId}').onWrite(async (change, context) => {
const otherDataSnapshot = await admin.firestore().ref('myPath').once('value').then();
this.doOneComplexThing(change, context, otherDataSnapshot);
this.doAnotherComplexThing(change, context, otherDataSnapshot);
}
const doOneComplexThing = (change, context, otherDataSnapshot) => {
// do one complex thing
}
const doAnotherComplexThing = (change, context, otherDataSnapshot) => {
// do that other complex thing
}