firebase数据库runtransaction在节点上带有大量数据(例如root节点)
简短的问题: firebase实时数据库中的不良习惯在根节点上运行事务操作吗? (或任何带有大量数据的父节点。)
长版本:让我们根据firebase文档中建议的数据库结构进行示例:
{
"users": {
// users indexed by their unique id
"alovelace": {
"name": "Ada Lovelace",
"groups": {
"techpioneers": true,
"womentechmakers": true
}
},
... // other users
},
"groups": {
// groups indexed by their unique id
"techpioneers": {
"name": "Historical Tech Pioneers",
"members": {
"alovelace": true,
"ghopper": true,
"eclarke": true
}
},
... // other groups
}
}
让我想说我想将用户添加到带有runtransaction()的组中。唯一常见的共享节点是根,所以我需要做类似的事情:
runTransaction(ref(database, '/'), (value) => {
// do something with the data
})
要做到这一点,我需要读取对根节点的访问,这有点不错。就像在客户端一样,它可能不再对客户端可行,但是您可以依靠云功能或自定义BE。
但是,似乎这是远非推荐数据)。
那么,我在这里想念什么? 看来我误解了RunTransaction()的工作原理以及为什么需要读取访问,或者它是现实生活中的一种非常不可行的方法(也考虑了Firebase的最佳实践,以及如何构建NOSQL数据库)。
有人可以帮我理解吗?谢谢!
Short question: Is it a bad practice in Firebase Realtime Database to run a transaction operation on a root node? (or any parent node with a lot of data.)
Long-ish version: Let's do an example based on a database structure recommended in the Firebase doc:
{
"users": {
// users indexed by their unique id
"alovelace": {
"name": "Ada Lovelace",
"groups": {
"techpioneers": true,
"womentechmakers": true
}
},
... // other users
},
"groups": {
// groups indexed by their unique id
"techpioneers": {
"name": "Historical Tech Pioneers",
"members": {
"alovelace": true,
"ghopper": true,
"eclarke": true
}
},
... // other groups
}
}
Let's say I want to add a user to a group with runTransaction(). The only common shared node is the root, so I'll need to do something like:
runTransaction(ref(database, '/'), (value) => {
// do something with the data
})
To do this I need to have read access to the root node, which is kinda fine. As in, it's probably not doable from the client anymore, but you can rely on a Cloud Function or custom BE.
However, it seems like this is far from recommendable, as this operation requires downloading the whole database (or even a large portion of it in case you are not running the transaction on the root node, but still on some parent node with a lot of data).
So, what am I missing here?
It seems that either I'm misunderstanding how runTransaction() works and why it requires read access, or it's a very unpractical method in real-life scenarios (also considering the best practices -presented by Firebase as well- on how to structure NoSQL databases).
Can somebody help me understand? thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的理解是正确的:Firebase读取交易运行的整个节点,因此您需要在JSON树中低运行。
在您共享的示例中,添加组并不需要交易,但是可以通过单个多路写操作来完成:
Your understanding is correct: Firebase reads the entire node where a transactions runs, so you'll want to run it low in your JSON tree.
In the example you shared, adding a group doesn't require a transaction though, but can be accomplished with a single multi-path write operation: