title: relational data structures for firebase (best for performance?)
body: <p>I'm building out a messaging application with the following hierarchy: </p>
  
  <p><code>Organization -&gt; Chat Rooms -&gt; Threads -&gt; Messages</code></p>
  
  <p>I'm a little confused as to the best approach to structure the relational entities in <strong>firebase</strong>. 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()).</p>
  
  <pre><code>rooms -&gt;
    _room_id -&gt;
      org_id
      label
      created
      type
  
  threads -&gt;
    _thread_id -&gt;
      room_id
      created
  
  messages -&gt;
    _message_id -&gt;
      thread_id
      user_id
      name
      created
      status
      body
  </code></pre>
  
  <p>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. </p>
  
  <pre><code>rooms -&gt;
    org_id -&gt;
      _room_id -&gt;
        label
        created
        type
  
  
  threads -&gt;
    org_id -&gt;
      room_id -&gt;
        _thread_id -&gt;
          created
  
  messages -&gt;
    org_id -&gt;
      room_id -&gt;
        thread_id -&gt;
          _message_id -&gt;
            user_id
            created
            status
            body
  </code></pre>
  
  <p>With the newly proposed structure, the uri structure could look something like this:</p>
  
  <pre><code>/rooms/&lt;org_id&gt;                          -&gt; rooms
  /threads/&lt;org_id&gt;/&lt;room_id&gt;              -&gt; threads
  /messages/&lt;org_id&gt;/&lt;room_id&gt;/&lt;thread_id&gt; -&gt; messages
  </code></pre>
  
  <p>Appreciate any insights on the best approach. Thanks</p>