通过电子邮件地址或用户ID构造用户数据

通过电子邮件地址或用户ID构造用户数据

本文介绍了通过电子邮件地址或用户ID构造用户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让数据库中的用户结构化,从而使人们更易于阅读和管理.使用用户的电子邮件地址作为属性名称,而不是用户ID:

I want to have the users in the database structured in a way that makes it easier for a human to read and manage. Using the users email address as the property name instead of the User ID:

用户:

  "Users" : {
    "[email protected]":{
      "id": "DK66qu2dfUHt4ASfy36sdfYHS9fh",
      "name": "A Display Name",
      "groups": {
        "moderators": true,
        "users": true
      }
    },
    {...}
  }

因此,如果我在一个组中有一个用户列表,则可以将它们作为电子邮件列表而不是用户ID列表阅读.

So that if I have a list of users in a group, they can be read as a list of emails and not a list of user IDs.

组,例如:

  "Groups": {
    "moderators":{
      "name": "moderator",
      "members": {
        "[email protected]": true,
        "[email protected]": true
      }
    }
  }

组而不是:

  "Groups": {
    "moderators":{
      "name": "moderator",
      "members": {
        "DK66qu2dfUHt4ASfy36sdfYHS9fh": true,
        "K2fkHYQDFOge3Hw7SjRaGP3N2sdo": true
      }
    }
  }

但是,使用规则来验证用户(例如他们的组)的属性,将要求我维护两个用户列表,一个类似于上面的列表,另一个本质上是一个ID和电子邮件地址,这样我就可以从他们的uid获取用户的电子邮件地址.

However, using rules to verify a property of the user (such as their group), would require me to maintain two list of users, one like the list above, and another essentially a table of key-value pairs of ID's and email addresses so I can get the users email address from their uid.

伪代码规则: Users[UsersKeyVal[auth.uid]].groups.moderator == true

在使用Firebase时,什么是最可接受的做法?两者的优缺点是什么?

With firebase, what would be considered the most acceptable practice? What are the pros and cons of both?

推荐答案

请不要将用户数据存储在其电子邮件地址下!稍后将成为大麻烦.

Please do not store user data under their email address! This will be BIG TROUBLE later.

您的用户节点应遵循标准" Firebase设计模式

Your users node should follow the 'standard' Firebase design pattern

users
  uid_0
   name:
   gender:
   etc
  uid_1
   name:
   gender:
   etc

总的来说,最好是将存储在节点中的动态数据与节点的键解除关联.

The bottom line is that in general, it's best to disassociate the dynamic data stored in the node from the key of the node.

为什么?

假设您使用各种链接和对[email protected]的引用构建了一个复杂的结构,然后@ mynotsocoolcompany.com收购了@ mycoolcompany.com.好了,您将必须进入并在整个数据库中重建对Franks电子邮件的所有引用.嗯.

Suppose you build a complex structure with all kinds of links and references to [email protected] and then @mycoolcompany.com gets acquired by @mynotsocoolcompany.com. Well, you will then have to go in and rebuild every reference to franks's email in the entire database. ugh.

然后,如果有100或1000个用户@ mycoolcompany.com,该怎么办!哎呀.

Then what if there are 100 or 1000 users @mycoolcompany.com! Ouch.

如果像我上面建议的结构那样取消数据关联,您只需更改节点内的电子邮件地址,其他所有内容都可以!

If you disassociate the data, like my per above suggested structure, you just change the email address within the node and everything else... just works!

请阅读有关堆栈溢出的答案-由Firebaser撰写并解决您的问题

PLEASE, read this answer on stack overflow - written by a Firebaser and addresses your question

Firebase数据结构和URL

这篇关于通过电子邮件地址或用户ID构造用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 08:11