I am using react-native-firebase, and I generally want to use custom keys on the table's JSON objects. I have searched and I know that I can use the set
method instead of push
, to accomplish that. My problem is when creating new users.
React-native-firebase uses the function signInWithEmailAndPassword(email,password)
which automatically creates a new user with a random ID like 9dBqfh5yfbd2dMGIieIe3tPs2ba2
. 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. However I am not really sure if the corect way to store the uid on the database is on the object's key, or inside the object on a uid property.
Is there any way except from using the admin SDK which requires of course a server and cannot run natively on my application to achieve this?
You can set custom UIDs while creating a user using the Admin SDK only. You can store both the random ID and the integer along with other user info in the database if necessary. For example if you are using Firestore then a document can be like:
{
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
It's quite common to use such random IDs in NoSQL databases instead of integers (like UUIDs as key in SQL). Storing such sequential IDs might lead to hotspots and is not advised as mentioned in Firestore's documentation.
If you need the ID to an integer for querying/sorting purposes then you can store a createdAt
Timestamp field instead.