Firebase数据库触发器:onCreate,onUpdate,onDelete [英] Firebase database triggers: onCreate, onUpdate, onDelete

查看:64
本文介绍了Firebase数据库触发器:onCreate,onUpdate,onDelete的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最新的Firebase函数版本上,FirebaseDatabase触发器已更新,将其功能拆分为onCreateonUpdateonDelete,而不是始终使用onWrite,并检查每次调用中是否已删除数据.

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.

有人可以提供更多有关是否值得将当前FirebaseDatabase触发器迁移到新的拆分功能以及如何在应用程序中对其进行更新的信息.

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.

推荐答案

当然值得!拆分您的功能将使您的功能更短,更清晰,更快捷.同样,您将避免无限调用DatabaseTriggers最终应用return.最后,您需要为应用程序正在使用的触发器数量付费,因此您应尽量避免无用的通话以节省资金!

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!

首先要在云功能中实现它,您需要在功能folder中的package.json上更新您的firebase-functions版本,至少将其升级到0.5.9.

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.

关于如何使用每个触发器,让我们看一下可以拆分的onWrite示例.

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

function检查是否在特定的reference上写入了新的comment,并基于是否已添加,添加了deletedupdated,将其加1减1或不执行任何操作:

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;
        });
});

您可以查看更多示例,并在下一篇文章中阅读有关此新功能的信息: https://firebase.googleblog.com/2017/07/cloud-functions-realtime-database.html

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

这篇关于Firebase数据库触发器:onCreate,onUpdate,onDelete的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆