Home > OS >  Changing username in firebase
Changing username in firebase

Time:05-27

i'm making a chat app using firebase. For each document that contains any name, i'm creating it as auth.currentUser.displayName. So for example, each message in the chat is a document created as:

sender: currentUser?.displayName!,
      avatar: currentUser?.photoURL!,
      content: message,
      time,

About changing the username, i know how to do it, but if i change my username, i would have to access all the documents of every user that contains my name and change it too. Is there a better way to do it? Like, instead of auth.currentUser.displayName, i use something like user XYZ.displayName, and if user XYZ changes its name, it will also change in every other document.

Thanks in advance

CodePudding user response:

But if i change my username, i would have to access all the documents of every user that contains my name and change it too.

This is indeed a consequence of the data modelling in a NoSQL database, where you duplicate data instead of normalizing it as you would do in a SQL database.

Is there a better way to do it?

Apart from mimicking an SQL database (having the user name in a unique user document and fetching this doc each time you want to display this user name - i.e. you mimic an SQL join) there is no other way than updating each doc containing the value of the user name.

Mimicking an SQL database means that each time you want to display a message in the chat you need to fetch the document containing the user name value (not good for cost and performance). Keeping the denormalized approach means that you need to update all the corresponding message documents only when a user name change. So, depending on the frequency of the user names updates, you can decide which one is the best.

Like, instead of auth.currentUser.displayName, i use something like user XYZ.displayName, and if user XYZ changes its name, it will also change in every other document.

Basically this will not make any difference since you'll have to update all the docs containing the (previous) user name.

  • Related