I am trying to add +1 to a specific field in the realtime database. My function:
exports.dbWriteOnNewPost = functions.database.ref('/posts/{postid}').onWrite((change, context) => {
const postUUID = context.params.postid;
const postData = change.after.val();
const communityUUID = postData.community;
const authorUUID = postData.author;
const postDate = postData.date;
const promisePostByCommunity = admin.database().ref('/posts_by_community/' + communityUUID + '/' + postUUID).set(postDate);
const promisePostByUser = admin.database().ref('/posts_by_user/' + authorUUID + '/' + postUUID).set(postDate);
const promiseCommunityPostsCount = admin.database().ref('/communities/' + communityUUID + '/posts_count').transaction(
(posts_value) => {
return posts_value + 1;
}
);
return Promise.all([promisePostByCommunity, promisePostByUser, promiseCommunityPostsCount]);
});
I am simply asking if this transaction will prevent assigning wrong value, if for example 10 users are creating posts in the exact same time, which is going to happen if I use typical .once value => .set ?
EDIT: Finally managed to test it without breaking anything and the code above works perfectly fine.