Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structuring user data by email address or by user 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:

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

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 Such as:

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

Groups Instead of:

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

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.

Pseudo-code rule: Users[UsersKeyVal[auth.uid]].groups.moderator == true

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

like image 909
Douglas Gaskell Avatar asked Feb 23 '26 20:02

Douglas Gaskell


2 Answers

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

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.

Why?

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.

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!

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

Firebase data structure and url

like image 91
Jay Avatar answered Feb 26 '26 09:02

Jay


In my opinion there is no problem with your data structure.

According to the Doc

This is a necessary redundancy for two-way relationships. It allows you to quickly and efficiently fetch your members memberships

Also using the generated UId from firebase or your custom Id (here your e-mail) doesn't change the way firebase works. You just have to make sure your e-mail are unique.

like image 29
Crema Avatar answered Feb 26 '26 10:02

Crema



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!