1
votes

Firestore Cloud Functions priority

Now I deploy two cloud functions in the Firestore database.

They are triggered by the same document changes.

Is it possible to specify the execution order of the functions or the trigger sequence? For example, I want to let updateCommentNum function trigger fist, then trigger writeUserLog function. How could I achieve this goal?

exports.updateCommentNum = functions.firestore
.document('post/{postId}/comments/{commentsID}')
.onWrite((change, context) => 
{
    //update the comment numbers in the post/{postId}/
}


exports.writeUserLog = functions.firestore
.document('post/{postId}/comments/{commentsID}')
.onWrite((change, context) => 
{
    //write the comment name,text,ID,timestamp etc. in the collection "commentlog"

}
1

1 Answers

3
votes

There is no way to indicate relative priority between functions.

If you have a defined order you want them invoked in, use a single Cloud Function and just call two regular functions from there:

exports.onCommentWritten = functions.firestore
.document('post/{postId}/comments/{commentsID}')
.onWrite((change, context) => { 
  return Promise.all([
    updateCommentNum,
    writeUserLog
  ])
})

function updateCommentNum(change, context) {
    //update the comment numbers in the post/{postId}/
}

function writeUserLog(change, context) {
    //write the comment name,text,ID,timestamp etc. in the collection "commentlog"
}

That will also reduce the number of invocations, and thus reduce the cost of operating them.