0
votes

I am developing a chat application with firebase firestore. I have two collections {users} and {chats}

This is the chat structure

{
 createdAt :1595285204356,
 receipient:"9zjJ8cH8PLSRjcW5rfTxI0xipcC3",
 uid:"GZI1AlbYObWJltzCVrlouRZLIU92",
 messages:{
           {content:"Hello Pal, Reply to this",
           createdAt :1595285245784,
           is_read:1,
           receipient:"GZI1AlbYObWJltzCVrlouRZLIU92",  //receiver
           uid":9zjJ8cH8PLSRjcW5rfTxI0xipcC3",         //sender
          },
          {content:"Hi Pal, this is reply!",
           createdAt :1595285245958,
           is_read:0,
           receipient:"9zjJ8cH8PLSRjcW5rfTxI0xipcC3", //receiver
           uid":GZI1AlbYObWJltzCVrlouRZLIU92",        //sender
          }
     }
 } 
       
             

and this is the user structure

{
displayName:"theusername***",
email:"theuser***[email protected]",
photoURL:"https://lh4.googleusercontent.com/-sXb0FiS_w7Q/xxdfd/xfQg5o8IRixaox1Ute1Q/photo.jpg",
uid:"9zjJ8cH8PLSRjcW5rfTxI0xipcC3",
 }
{
displayName:"theusername2***",
email:"theuser2***[email protected]",
photoURL:"https://lh4.googleusercontent.com/-sXb0FiS_w7Q/yydfd/xfQg5o8IRixaox1Ute1Q/photo.jpg",
uid:"GZI1AlbYObWJltzCVrlouRZLIU92",
 }

for every message there's a 'uid' representing the sender and 'receipient' representing the receiver.

i want to join the chat with the users matching uid in users with either,'uid' or 'receipient' in chats collection. where the results will be something like this

{
receipient:"9zjJ8cH8PLSRjcW5rfTxI0xipcC3",
uid:"GZI1AlbYObWJltzCVrlouRZLIU92",
receipient_data:{
               displayName:"theusername***",
               email:"theuser***[email protected]",
               photoURL:"https://lh4.googleusercontent.com/ox1Ute1Q/photo.jpg",
               uid:"9zjJ8cH8PLSRjcW5rfTxI0xipcC3",
   },
   sender_data:{
               displayName:"sendername***",
               email:"sender***[email protected]",
               photoURL:"https://lh4.googleusercontent.com/IRixaox1Ute1Q/photo.jpg",
               uid:"GZI1AlbYObWJltzCVrlouRZLIU92",
   },
 createdAt :1595285204356,
 receipient:"9zjJ8cH8PLSRjcW5rfTxI0xipcC3",
 uid:"GZI1AlbYObWJltzCVrlouRZLIU92",
 messages:{{content:"Hello Pal, Reply to this",
           createdAt :1595285245784,
           is_read:1,
           receipient:"GZI1AlbYObWJltzCVrlouRZLIU92",  //receiver
           uid":9zjJ8cH8PLSRjcW5rfTxI0xipcC3",         //sender
          },
          {content:"Hi Pal, this is reply!",
           createdAt :1595285245958,
           is_read:0,
           receipient:"9zjJ8cH8PLSRjcW5rfTxI0xipcC3", //receiver
           uid":GZI1AlbYObWJltzCVrlouRZLIU92",        //sender
          }
     }
 } 

I am new to firestore and i have tried lots of functions but none seems to work to my satisfaction best one i have tried yet is this

    return this.afs.collection('chats', ref => ref.where(type ,'==',user_id))
  .snapshotChanges()
  .pipe(
    map(actions => {
      return actions.map(a => {
      
        const data: Object = a.payload.doc.data();
        const id = a.payload.doc.id;
        return { id, ...data };
      });
    })
    

);

but i'm stuck as to how to implement the join. any help is greatly appreciated

1
I found something intestesting about using combineLatest: medium.com/@joaqcid/… Not sure if useful for youOliver Aragon

1 Answers

1
votes

Ok. There are no joins in firestore. i admit limited knowledge of firebase firestore made me ask this question. I saw quite a number of answers most of the unconvetional with perforamce issues.

I suggest that you set the sender_data, receipient_data for each chat field and update all of them later if it becomes neccessary