I'm building out a messaging application with the following hierarchy:
Organization -> Chat Rooms -> Threads -> Messages
I'm a little confused as to the best approach to structure the relational entities in firebase. The docs say flatten your data, but if the data is flattened then each record must be searched in order to find the identifiers, which seems more costly. What is the most cost-efficient or "proper" way of doing this? Below is my original proposal. The arrows denote a parent->child relationship. The underscores below id means a generated identifier (using firebase .push()).
rooms ->
_room_id ->
org_id
label
created
type
threads ->
_thread_id ->
room_id
created
messages ->
_message_id ->
thread_id
user_id
name
created
status
body
And here's the newly proposed structure. The primary difference between these two is how the relational entities sit at the child level rather than the parent.
rooms ->
org_id ->
_room_id ->
label
created
type
threads ->
org_id ->
room_id ->
_thread_id ->
created
messages ->
org_id ->
room_id ->
thread_id ->
_message_id ->
user_id
created
status
body
With the newly proposed structure, the uri structure could look something like this:
/rooms/<org_id> -> rooms
/threads/<org_id>/<room_id> -> threads
/messages/<org_id>/<room_id>/<thread_id> -> messages
Appreciate any insights on the best approach. Thanks