Firebase Cloud功能更新数据库中的所有条目 [英] Firebase Cloud function update all entries in database

查看:87
本文介绍了Firebase Cloud功能更新数据库中的所有条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Firebase数据库中有很多注释,我想通过Cloud Function对注释进行一些更新(这是简化的示例,我将做一些需要Cloud Function的逻辑).

i have bunch of comments in Firebase database and i want to do some updates to the comments via Cloud Function ( this is simplified example, i will be doing some logic which does require Cloud Function ).

我需要做的是遍历数据库中的所有注释,调整其评级节点,然后使用调整后的注释更新数据库.

我花了很多时间研究这个问题,但是我对Cloud Functions完全陌生,所以我很难解决这个问题. 我假设我想将对所有注释的所有更改(可能有成千上万个)存储在数组或对象中,然后一次进行更新,而不是分别为每个注释进行更新?

I spent a lot of time researching this, but i am completely new to Cloud Functions, so i have realy hard time figuring this out. I am assuming i want to store all the changes to all the comments (there can be thousands of them) in the array or object and then do the update at one time instead of for each comment separately ?

此代码无法正常工作,我假设数组和return完全错误.

Btw this code is not working, i am assuming the array and return is completely wrong.

exports.increaseRating = functions.database.ref('/comments/')
    .onUpdate((snapshot) => {   
     
        var updates = [];

        snapshot.before.forEach((element) => {
            var comment = element.val();
            comment.rating += 1000;
            updates.push(comment);
        });

        return updates;
    })

我用来更新一个条目的代码.我需要一次对所有评论执行相同的操作.

Code i am using to update one entry. I need to do the same thing for all the comments at one time.

exports.increaseRating = functions.database.ref('/comments/{commentId}')
    .onUpdate((snapshot, context) => {

        const comment = snapshot.before.val();
        const newRating = comment.rating += 1000;       
       
        const now = new Date().getTime();
        if (comment.lastUpdate) {
            if (comment.lastUpdate > now - (30 * 1000)) {
                return null;
            }
        }
        
        return admin.database().ref(`/comments/${context.params.commentId}`).update({
            "rating": newRating,
            "lastUpdate": now
        })
    })

推荐答案

如果要更新所有子节点,可以执行以下操作:

If you want to update all child nodes, you can do something like this:

var ref = firebase.database().ref("comments"); // or admin.database().ref("comments")
ref.once("value").then((snapshot) => {
  var updates = {};
  snapshot.forEach((commentSnapshot => {
    var comment = commentSnapshot.val();
    var newRating = comment.rating + 1000;
    updates[commentSnapshot.key+"/rating"] = newRating;
  });
  ref.update(updates);
})

这将对所有注释执行单个多位置更新.请注意,由于执行

This performs a single multi-location update for all comments. Note that the performance benefit over performing separate updates is quite small, since Firebase pipelines the multiple requests over a single connection.

还请注意,您应该将其放在/comments上的Cloud Functions触发器中,因为这将导致无休止的循环:每次编写注释时,您的函数都会触发并更新注释,再次触发该功能.

Also note that you should not put this in a Cloud Functions trigger on /comments, since that will lead to an endless loop: every time the comments get written, your function triggers, which updates the comments, which triggers the function again.

如果您在Cloud Functions中需要此功能,则需要使用HTTP触发的功能,该功能由HTTP调用而不是数据库写操作触发.

If you need this in Cloud Functions, you'll want to use a HTTP-triggered function, which is triggered by HTTP calls instead of database writes.

exports.updateCommentRatings = functions.https.onRequest((req, res) => {
  var ref = admin.database().ref("comments")
  ref.once("value").then((snapshot) => {
    var updates = {};
    snapshot.forEach((commentSnapshot => {
      var comment = commentSnapshot.val();
      var newRating = comment.rating + 1000;
      updates[commentSnapshot.key+"/rating"] = newRating;
    });
    ref.update(updates).then(() => {
      res.status(200).send("Comment ratings updated");
    });
  })
})

然后,您可以使用cron-job.org之类的服务定期调用此URL/函数.有关更多信息,请参见 Firebase的云功能是否按时触发?.

You can then periodically call this URL/function with a service like cron-job.org. For more on this see Cloud Functions for Firebase trigger on time?.

这篇关于Firebase Cloud功能更新数据库中的所有条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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