Reputation: 8604
I'm making a friends list chrome extension for an online browser game I play. One feature is that friends can be able to chat with one another. The database I'm using is called firebase which stores its data in a JSON tree format.
My database has this structure:
Users
|
|_USER1
| |
| |__FRIENDS
|
|_USER2
|
|__FRIENDS
I'm trying to figure out what would be the best way to store chats as part of this database. The option I'm leaning towards right now would just keep a copy of the two users chats in both their section of the Users directory, looking like this:
Users
|
|_USER1
| |
| |__FRIENDS
| |
| |__CHATS
| |
| |__chat w/USER2
|
|_USER2
|
|__FRIENDS
|
|__CHATS
|
|__chat w/USER1
This would make it so on each message send I'd have to update two objects, one in each users section. Note since the tree is formatted as 'key/value' pairs, in the CHAT section of each user the keys would be the other user's name, while the value would be the list of messages sent.
Is this a decent way of organizing such a database? The game is pretty small so I'm not expecting huge traffic.
Upvotes: 2
Views: 1290
Reputation: 599621
When it comes to the Firebase Database (and most NoSQL data stores), it's often best to flatten your data.
Users
|
|_USER1
| |
| |__FRIENDS
|
|_USER2
|
|__FRIENDS
UserChats
|
|_USER1
| |
| |__chat w/USER2
|
|_USER2
|
|__chat w/USER1
This way you can look up the user's friend list without having to load their list of chats.
Also look at this answer about a convenient scheme for constructing 1:1 chat room identifiers: Best way to manage Chat channels in Firebase
Upvotes: 2