0 votes
in Google Cloud by
How you would design a Firestore schema for a chat application where users can have multiple private conversations?

1 Answer

0 votes
by

Firestore schema for a chat application would consist of three main collections: ‘Users’, ‘Conversations’, and ‘Messages’.

‘Users’ collection stores user data with each document representing a unique user, identified by their userID. This includes information like username, profile picture URL, etc.

‘Conversations’ collection represents individual chats between users. Each document in this collection is identified by a conversationID and contains an array field named ‘participants’, storing the userIDs of both participants. It also has a timestamp field to track the last message sent time.

‘Messages’ sub-collection resides within each ‘Conversations’ document. Each document in ‘Messages’ represents a single message, containing fields such as ‘sender’ (userID of sender), ‘messageText’, and ‘timestamp’.

To retrieve messages from a specific conversation, query the ‘Messages’ sub-collection of that conversation document ordered by ‘timestamp’. To get all conversations of a user, query the ‘Conversations’ collection where ‘participants’ array contains the user’s ID.

...