Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase database triggers: onCreate, onUpdate, onDelete

On last Firebase functions version, FirebaseDatabase triggers have been updated spliting his functionality with onCreate, onUpdate and onDelete instead of always use onWrite and check if the data have been removed or not in every call.

Can someone give a bit more of information about if it's worth migrate current FirebaseDatabase triggers to new splited functionality and how to update it in an application.

like image 285
Francisco Durdin Garcia Avatar asked Jul 14 '17 10:07

Francisco Durdin Garcia


People also ask

What are triggers in Firebase?

The Functions which we write can respond to events that are generated by Firebase and Google Cloud features called triggers.

Does Firebase use Webhooks?

Firebase Webhooks automates the workflow for companies and Developers by triggering the defined events via URL. Firebase Webhooks Integration is the simplest and most efficient way to communicate between app and Firebase.

What are the Realtime Database limits in Firebase?

256 MB from the REST API; 16 MB from the SDKs. The total data in each write operation should be less than 256 MB. Multi-path updates are subject to the same size limitation.

How do you change rules in Firebase?

Edit and update your rulesOpen the Firebase console and select your project. Then, select Realtime Database, Cloud Firestore or Storage from the product navigation, then click Rules to navigate to the Rules editor. Edit your rules directly in the editor.


1 Answers

Of course is worth it! Split your functionality will make your functions shorted, clear and faster. Also you will avoid infinite calls to DatabaseTriggers to finally apply a return. In the end you will pay for the number of triggers that you app is using, so you should try to avoid useless call to save money!

To implement it in your cloud functions first you will need yo update your firebase-functions version on your package.json inside your function folder and upgrade it to 0.5.9 at least.

About how to use each triggers, lets look closer to an example of onWrite which can be splited.

This function check when a new comment is writed on an specific reference and based on if it have been added, deleted, or updated it plus 1, minus 1 or do nothing :

exports.countComments = functions.database.ref('/workoutPosts/{workoutId}/info/comments/{commentId}').onWrite(event => {
    const workoutId = event.params.workoutId;

    //Comment created
    if (event.data.exists() && !event.data.previous.exists()) {
        return database.ref(`/workoutPosts/${workoutId}/meta/commentsCount`).transaction(addPrivateWorkout => {
            return (addPrivateWorkout || 0) + 1;
        });
        //Comment deleted
    } else if (!event.data.exists() && event.data.previous.exists()) {
        return database.ref(`/workoutPosts/${workoutId}/meta/commentsCount`).transaction(deletePrivateWorkout => {
            return (deletePrivateWorkout || 0) - 1;                
        });
        //Comment updated
    } else if (event.data.exists() && event.data.previous.exists()) {
        return
    }
};

Each update call will be an useless call, and a waste of resources. How can we make this easier? Using the new splitted cloud functions:

exports.countCommentsOnCreate = functions.database.ref('/workoutPosts/{workoutId}/info/comments/{commentId}').onCreate(event => {
    const workoutId = event.params.workoutId;
        return database.ref(`/workoutPosts/${workoutId}/meta/commentsCount`).transaction(addPrivateWorkout => {
            return (addPrivateWorkout || 0) + 1;
        });       
});

exports.countCommentsonDelete = functions.database.ref('/workoutPosts/{workoutId}/info/comments/{commentId}').onDelete(event => {
    const workoutId = event.params.workoutId;

        return database.ref(`/workoutPosts/${workoutId}/meta/commentsCount`).transaction(deletePrivateWorkout => {
            return (deletePrivateWorkout || 0) - 1;
        });
});

You can check more examples and read about this new features on the next post : https://firebase.googleblog.com/2017/07/cloud-functions-realtime-database.html

like image 79
Francisco Durdin Garcia Avatar answered Sep 27 '22 21:09

Francisco Durdin Garcia