firebase数据库runtransaction在节点上带有大量数据(例如root节点)

发布于 2025-02-13 01:14:35 字数 1118 浏览 0 评论 0原文

简短的问题: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱你不解释 2025-02-20 01:14:35

您的理解是正确的:Firebase读取交易运行的整个节点,因此您需要在JSON树中低运行。

在您共享的示例中,添加组并不需要交易,但是可以通过单个多路写操作来完成:

firebase.database().ref().update({
  "users/alovelace/groups/newGroup": true,
  "groups/newGroup/members/alovelace": true
})

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:

firebase.database().ref().update({
  "users/alovelace/groups/newGroup": true,
  "groups/newGroup/members/alovelace": true
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文