我正在尝试确定我的云功能中的事务模式是否正确。据我了解,您应该限制对子节点的更新。所以我试图遵循这个一般规则。
/functions/index.js
//not rate limited
//writes:
// - board.posts+1
// - thread.replies+1
// - thread.pages+1 if page count should change Math.ceil(thread.replies+1/POSTS_PER_PAGE)=pages
exports.newPost2 = require('./lib/new-post-2')(functions,firebase,rootRef,POSTS_PER_PAGE);
/functions/lib/new-post-2.js
module.exports = function(functions,firebase,rootRef,POSTS_PER_PAGE){
return functions.database.ref('/posts/{forum}/{board}/{thread}/{post}').onWrite(function(event){
// Only edit data when it is first created.
if (event.data.previous.exists()) {
return;
}
// Exit when the data is deleted.
if (!event.data.exists()) {
return;
}
var params = event.params;
var forum = params.forum;
var board = params.board;
var thread = params.thread;
var post = params.post;
//first increment the replies node of the thread.
rootRef.child('threads').child(forum).child(board).child(thread).child('replies').transaction(function(data) {
return data + 1;//increment reply count for thread.
}).then(function(snapshot){
//now, our snapshot should contain the updated value of replies.
var replies = snapshot.val();
//update the page count of the thread
rootRef.child('threads').child(forum).child(board).child(thread).child('pages').transaction(function(data) {
return Math.ceil(replies+1/POSTS_PER_PAGE);//replies is always +1 because OP of thread doesnt count as a reply.
});
//update the posts count for the board. this field is also updated by another firebase cloud function, when a thread is created
rootRef.child('forums').child(forum).child('boards').child(board).child('posts').transaction(function(data){
return data + 1;//increment post count for the board.
});
});
});
};
我担心在线程上增加初始回复字段后,更新线程的页数和板的发布计数时可能会竞争。我想知道是否有更安全的方式我可以重组这个以更新第一笔交易中的所有3个字段,如果可能的话。