使用 React Native Firebase 创建具有自定义 uid 作为键的用户

Create users with custom uid as key with React Native Firebase

我正在使用 react-native-firebase,我通常想在 table 的 JSON 对象上使用自定义键。我已经搜索过,我知道我可以使用 set 方法而不是 push 来完成它。我的问题是在创建新用户时。

React-native-firebase 使用函数 signInWithEmailAndPassword(email,password) 自动创建一个具有随机 ID 的新用户,例如 9dBqfh5yfbd2dMGIieIe3tPs2ba2。现在因为我习惯了 mySQL 数据库结构,本能地我想使用数字键,甚至用户名作为 ID。但是我不太确定在数据库上存储 uid 的正确方法是在对象的键上,还是在对象内部的 uid 属性.

除了使用管理 SDK 之外,还有其他方法吗,这当然需要服务器,并且不能 运行 在我的应用程序上本地实现此目的?

您可以在仅使用 Admin SDK 创建用户时设置自定义 UID。您可以将随机 ID 和整数与其他用户信息一起存储在数据库中 if 必要。例如,如果您使用的是 Firestore,那么文档可以是这样的:

{
  uid: 'firebase_auth_id',
  num: 1,
  ...otherUserInfo
}

Now since I am used to mySQL database structure, instinctively I would like to use either a numeric key, or even the username as an ID

在 NoSQL 数据库中使用此类随机 ID 而不是整数(如 SQL 中的 UUID 作为键)是很常见的。存储这样的顺序 ID 可能会导致 hotspots and is not advised as mentioned in Firestore's documentation。 如果出于 querying/sorting 目的需要将 ID 转换为整数,则可以存储 createdAt 时间戳字段。