0

I'm trying to find the best method for storing and retrieving a list of posts with comments in Firebase using the RTDB (not Firestore as this project has been ongoing for a while).

I have a set of users: enter image description here And I have a set of posts enter image description here

Some people have suggested storing the user's name directly in the post and post comments, but I am nervous that at some point they will change their information. Plus, I'd like additional info (reputation, member since, etc.) to show up.

My thought is querying for each user and storing the object in an associative array. So the first time UID 1234 comes up, the code will check the array, and if it doesn't exist, it will query it from the database.

users = {};    
getUser( uid ){
    return new Promise( (resolve, reject ) =>{
      if( this.users && this.users[ uid ] ){
        resolve( this.users[ uid ] );
      } else {
        this.angular.object('users/'+uid ).snapshotChanges().take(1).subscribe( data =>{
          const user = data.payload.val();
          if( user ){
            this.users[ uid ] = user;
            resolve( user );
          } else {
            reject();
          }
        });
      }
    });
  }

If 100 users and comments are on a page, I am wondering if that will be inefficient? And is that too many queries for something like Firebase to handle?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Everett Glovier
  • 328
  • 3
  • 15
  • "I am nervous that at some point they will change their information". For strategies for dealing with this, see https://stackoverflow.com/questions/30693785/how-to-write-denormalized-data-in-firebase – Frank van Puffelen Aug 16 '18 at 20:18
  • 1
    Loading the users on demand is quite common too. It's not nearly as slow/resource intensive as you may think, since Firebase can pipeline the requests over its open connection. See http://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786 – Frank van Puffelen Aug 16 '18 at 20:19
  • @FrankvanPuffelen you're the guy i was hoping to hear from! Thanks so much! – Everett Glovier Aug 20 '18 at 21:46

0 Answers0