考虑以下代码以在 FireBase 中的多个位置进行原子写入:
var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
var newPostRef = ref.child("posts").push();
var newPostKey = newPostRef.key();
var updatedUserData = {};
updatedUserData["users/"+authData.uid+"/posts/" + newPostKey] = true;
updatedUserData["posts/" + newPostKey] = {
title: "New Post",
content: "Here is my new post!"
};
ref.update(updatedUserData, function(error) {
if (error) {
console.log("Error updating data:", error);
}
});
这种方法可用于在不同位置更新帖子,但如何在服务器端强制执行原子更新? (通过规则)。
如何确保用户在不填充users/UID/posts/
的情况下无法更新位置/posts/
(通过其直接引用),反之亦然?
最佳答案
这样的“业务规则”有很多可能,所以我将选择一个并实现它。假设用户引用的任何帖子都必须存在。因此,如果 /posts/mypostid
存在,您只能写入 /users/myuid/posts/mypostid
。我还将对帖子本身进行基本验证。
{
"posts": {
"$postid": {
".validate": "hasChildren(['title', 'content'])",
"title": {
".validate": "newData.isString()"
},
"content": {
".validate": "newData.isString()"
},
"$other": {
".validate": false
}
}
},
"users": {
"$uid": {
"posts": {
"$postid": {
".validate": "newData.parent().parent().parent().parent().child('posts').child($postid).exists()
}
}
}
}
}
这里最大的技巧是 newData.parent().parent()...
位,它确保我们获取新数据中的帖子。
您习惯于问诸如“我如何确保使用方法 ABC 来更新数据?”之类的问题,这很少是正确的思考问题的方式。在上面的规则中,我专注于验证数据的结构,并不关心哪些 API 调用可能会导致该数据。
关于Firebase,多位置更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35720749/