为Firebase中的私有数据访问对数据结构进行非规范化? [英] Denormalizing data structures for private data access in Firebase?

查看:102
本文介绍了为Firebase中的私有数据访问对数据结构进行非规范化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建可扩展的数据(以跟踪用户的私人数据).

I would like to create data that scales (to track private data of a user).

Firebase文档建议将子对象嵌套在父对象下,如下所示:

The Firebase documentation recommends to nest the child objects under the parent like this:

{
  "users": {
    "google:1234567890": {
      "displayName" : "Username A",
      "provider" : "google",
      "provider_id" : "1234567890",
      "todos": {
          "rec_id1": "Walk the dog",
          "rec_id2": "Buy milk",
          "rec_id3": "Win a gold medal in the Olympics",
          ...
      }
    }, ...
  }
}

(其中rec_id是由Firebase.push()生成的唯一密钥.)

(Where rec_id is a unique key generated by Firebase.push().)

但是,正如对数据进行规范化是正常的我认为最好采用这种结构:

But as mentioned in Denormalizing Your Data is Normal I think it would be better to structure it this way:

{
  "users" : {
    "google:1234567890" : {
      "displayName" : "Username A",
      "provider" : "google",
      "provider_id" : "1234567890"
    }, ...
  },
  "todos" : {
    "google:1234567890" : {
      "rec_id1" : {
        "todo" : "Walk the dog"
      },
      "rec_id2" : {
        "todo" : "Buy milk"
      },
      "rec_id3" : {
        "todo" : "Win a gold medal in the Olympics"
      }, ...
    }, ...
  }
}

然后仅允许用户写入/读取自己的数据,请应用以下安全规则:

And then to only allow the user to write/read it's own data apply the following security rules:

{
  "rules": {
    "users": {
      "$uid": {
        // grants write and read access to the owner of this user account whose uid must exactly match the key ($uid)
        ".write": "auth !== null && auth.uid === $uid",
        ".read": "auth !== null && auth.uid === $uid"
      }
    },
    "todos": {
      "$uid": {
        // grants write and read access to the owner of this user account whose uid must exactly match the key ($uid)
        ".write": "auth !== null && auth.uid === $uid",
        ".read": "auth !== null && auth.uid === $uid"
      }
    }
  }
}

由于我是这种数据库的新手,所以我想知道我在构造数据库方面是否存在任何缺点.

As I'm new to this kind of databases I wonder if there are any downsides the way I'd like to structure it.

按照第一个示例的建议,将所有待办事项直接嵌套在用户下会更好吗?

Would it be better to nest all todos directly under the user as recommended in the first example?

推荐答案

首先,如果您还没有遇到这些资源,请提供一些资源:

First, a couple resources if you haven't run into them yet:

  • "Structuring Your Data" portion of the Getting Started Guide in the Firebase Docs?
  • "Creating Data That Scales" section of that guide is the important part, as you've said.
  • Also, check out this answer to "Clarification on Firebase denormalization blog post"

由于您的问题中有链接,您显然已经使用了这些资源,但我建议您再读一遍《结构化数据》指南.

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆