Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore + cloud function: How to tell if it's a create, update, or delete event

I want to write a Firebase cloud function that is triggered by a Firestore write event. Inside of the function, I have to know if the write is actually a create, update, or delete function. The logic is slightly different, but I don't want to copy and paste three different event handlers. How can I tell which event type it is?

The cloud documentation reads very confusing to me.

The documentation for Event says that in the case of FireStore, the event is actually a DeltaDocumentSnapshot.

https://firebase.google.com/docs/reference/functions/functions.Event

The documentation for DeltaDocumentSnapshot suggests that the class has no information about the event itself (e.g. indication of create, update, or delete), but only the target document:

https://firebase.google.com/docs/reference/functions/functions.firestore.DeltaDocumentSnapshot

I found samples, where the author seemed to infer create/update/delete by checking if the current or previous document exists. I've tried that but I get an error.

https://github.com/firebase/functions-samples/blob/master/child-count/functions/index.js#L30

Here is what I've tried:

exports.updateLikeCount = functions.firestore
.document('likes/{likeId}').onWrite((event) => {
    if (event.data.data().exists()) {
      // Looks like the document still exists. It must be an update or create.
    }

    if (event.data.previous.data().exists()) {
      // Looks like the document existed before. It must be an update or delete.
    }

That code fails with this error:

TypeError: event.data.exists is not a function
like image 804
Thomas Fischer Avatar asked Nov 26 '17 17:11

Thomas Fischer


2 Answers

With the new release of firebase-functions version 1.0, a function to check this could look like this (in typescript):

import { Change } from 'firebase-functions';

export function checkEventType(change: functions.Change<FirebaseFirestore.DocumentSnapshot>): string {
    const before: boolean = change.before.exists;
    const after: boolean = change.after.exists;

    if (before === false && after === true) {
        return 'create';
    } else if (before === true && after === true) {
        return 'update';
    } else if (before === true && after === false) {
        return 'delete';
    } else {
        throw new Error(`Unkown firestore event! before: '${before}', after: '${after}'`);
    }
}

I have tested this function, and works great in combination with the .onWrite() event.

like image 161
DauleDK Avatar answered Sep 21 '22 07:09

DauleDK


I've tried a few different things. The version below seems to work, yet I don't know if it is correct.

var exists = event.data.exists;`
var hasExisted = (event.data.previous != null) && event.data.previous.exists;
var isCreate = (exists && !hasExisted);
var isDelete = (!exists && hasExisted);
like image 20
Thomas Fischer Avatar answered Sep 19 '22 07:09

Thomas Fischer