Firebase Firestore批处理提交:是否按指定顺序执行了批处理的指令? [英] Firebase Firestore batch commits: are the batch's instructions executed in the specified order?

查看:24
本文介绍了Firebase Firestore批处理提交:是否按指定顺序执行了批处理的指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于Firestore和Cloud Functions开发类似/不同的帖子系统.目前,我正在设置一个Cloud Function,当应用程序用户查看帖子列表时将执行该功能.该Cloud Function的目的是计算每个帖子的喜欢次数,总结同一帖子所有者的每个帖子的喜欢次数,并将每个帖子所有者的所有帖子的喜欢次数保存到数据库中文档.

I am working on a like/unlike system of posts based on Firestore and Cloud Functions. Currently, I'm setting up a Cloud Function that will be executed when the app user takes a look to the list of posts. This Cloud Function's aim is to count the number of likes of each post, sum up the number of likes of each post of the same post owner, and saves in database the number of likes of all the posts of each post owner into the post owner document.

所以:

  1. 我遍历所有用户文档,并将所有帖子的点赞次数设置为0.这是一个初始化.我使用批量更新.

  1. I iterate over all my users documents, and set the number of likes of all their posts to 0. It's an initialization. I use batch updates.

然后,我遍历所有喜欢的对象.在RAM中,对于每个帖子所有者,我都会计算出他创建的每个帖子的点赞次数之和.我使用JS数组操作.

Then, I iterate over all the likes. In RAM, for each post owner, I compute the sum of the number of likes of each of the posts he has created. I use JS arrays operations.

然后,我遍历JS数组,该数组包含与所有帖子的赞次数相关联的帖子所有者的ID,并将数据库计数器设置为所有帖子的赞次数为价值.我使用批量更新.

Then, I iterate over the JS array that contains the post owners' ID associated to the number of the likes of all their posts, and I set the database counter of the number of the likes of all their posts to the value. I use batch updates.

如果不清楚,下面提供了代码.

If it's not clear, the code is available below.

我的问题是:由于我首先将所有用户文档的计数器初始化为0,然后影响其中一些用户文档的计数器的良好值,因此我实际上需要确保在进行初始化之前将0初始化完成.休息批更新.是这样吗?

My question is: since I first initialize to 0 the counter of all my users documents, and then I affect the good value for the counter of some of them, I actually need to be sure that the 0 init is done before the rest batch updates. Is it the case?

exports.sortUsersByUsersPostsLikes = functions.https.onCall((data, context) => {
    if(!context.auth) {
        throw new functions.https.HttpsError('failed-precondition', 'The function must be called while authenticated.');
    }

    const batch = admin.firestore().batch();
    const users = admin_firestore.collection('users');
    const likes = admin_firestore.collection('likes_of_users_posts');
    users.get().then(function(users_docs) {
        users_docs.forEach(user_doc => {
            batch.update(user_doc.ref, "number_of_likes_of_users_posts", 0);
        });

        return likes.get();

    }).then(function(likes_docs) {
        const map_users_id_with_number_of_likes = [];
        likes_docs.forEach(like_doc => {
            if(!(like_doc.data().post_owner in map_users_id_with_number_of_likes)) {
                map_users_id_with_number_of_likes[like_doc.data().post_owner] = 0;
            }
            map_users_id_with_number_of_likes[like_doc.data().post_owner] += 1;
        });
        Object.keys(map_users_id_with_number_of_likes).forEach((k) => {
            const user = admin_firestore.collection('users').doc(k);
            batch.update(user, "number_of_likes_of_users_posts", map_users_id_with_number_of_likes[k]);
        }); 
        return batch.commit();

    }).catch(function(error) {
        console.log("UNABLE TO SORT THE USERS");
        console.log(error);
        throw new functions.https.HttpsError('unknown', 'An error occurred when trying to sort the users.');
    });

});

推荐答案

批处理和事务一次完成,要么原子完成,要么根本不完成.没有命令.如果您两次设置字段的值,则只有在构建批处理时指定的最后一个值才会生效.

Batches and transactions complete all at once, atomically, or not at all. There is no order. If you set the value of a field twice, only the last value you specify as you build the batch will take effect.

如果您要询问可能会因文档更新而发生的Cloud Functions触发器,则永远不会为这些触发器提供订购保证.

If you're asking about Cloud Functions triggers that might happen in response to a document update, there is never an ordering guarantee for those trigger.

这篇关于Firebase Firestore批处理提交:是否按指定顺序执行了批处理的指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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