I am designing a chat app using cloud firestore and the schema looks something like this :
Each 1:1 chat is created inside a chats collection. Each chats collection will have a set of fields and a sub collection. Each sub collection will have all the messages. It looks something like this -
/chats
* id (auto gen)
* channel ( string type - sort(uid1 , uid2) )
* uid1
* uid2
* createdTime
* lastMessageSentTime
* messages (sub collection)
* fromUid
* toUid
* content
* sendTime
To show the initial chat screen, with all users the user has been chatting with, I query the /chats collection and sort the results by lastMessageSentTime.
One problem I see with this design is that I need to update the lastMessageSentTime for every message sent or received. Is there a way I can avoid this double writing ?
Can I use the messages sub collection's sendTime to somehow show the first chat screen or is there another way out ?
Thanks in advance !