I'm using Firestore at beta version with Cloud Functions. In my app I need to trigger a function that listens for an onCreate
event at /company/{id}/point/{id}
and performs an insert (collection('event').add({...}))
My problem is: Cloud Functions with Firestore require an idempotent function. I don't know how to ensure that if my function triggers two times in a row with the same event, I won't add two documents with the same data.
I've found that context.eventId
could handle that problem, but I don't recognize a way to use it.
exports.creatingEvents = functions.firestore
.document('/companies/{companyId}/points/{pointId}')
.onCreate((snap, context) => {
//some logic...
return db.doc(`eventlog/${context.params.companyId}`).collection('events').add(data)
})
set
approach I outlined if Cloud Functions guarantees no concurrent execution of the same event. Theset
approach saves a network round-trip though, which can matter under load. – Photoneutron