Home > Software design >  React Firebase - Handle denormalized data
React Firebase - Handle denormalized data

Time:09-29

I am reading docs from my database which has denormalized data inside of them. I use this data to render a GUI component "UserListItem" which is clickable.

When the user presses this component, it will navigate to a screen "Profile".

So, if for example, after fetching a denormalized doc I get the following:

{
    userData: { // Denormalized data
        id,
        avatar,
        name,
        username,
        isCelebrity
    },
    ... other stuff
}

and the "Profile" screen, to which the user navigates when clicking, requires the following user fields:

userData = {
     /* Included in denormalization */
     id,
     avatar,
     name,
     username,
     isCelebrity,

     /* Not included in denormalization */
     totalFollowers,
     totalFollowing,
     status,
     premium,
}

How can I handle this situation? I mean, whats the typical strategy?

I have thought about doing this in the Profile Screen:

useEffect(() => {
   if(!isUserDataComplete(userData)) { // Check that the userData object contains all the required fields
      const newUserData = getUserData(userData.id); // DB Fetch
      users.updateData(newUserData); // Updating the user data in our context
   }
}, []);

But not sure if this is a good and "clean" approach.

And also, does this situation have a name? To read more about it.

CodePudding user response:

But not sure if this is a good and "clean" approach.

This is highly dependent on your functional requirements, on the access rights, on the frequency of update and on the volume of the data that is not "included in denormalization".

If:

  1. All the authorized readers of UserList can read the corresponding Profiles;
  2. Between the moment a user opens the UserList and the moment he/she opens one of the Profile (click on a line of UserList if I correctly understand) data that is not "included in denormalization" is not going to change;
  3. This data that is not "included in denormalization" is not really heavy (I imagine that totalFollowers and totalFollowing are numbers, status may be a code and premium a boolean);

Then you should probably include "in the denormalization" the data that is not included: you will save some reads, since the data to be displayed when opening a Profile will have already been fetched.

On the other hand, if one of the above condition is not true, then you should probably stay as it is and fetch the data that is not "included in denormalization" when the user opens a Profile

  • Related