How to use Firebase as a relational database with generated keys (JS)?
<p>I'm working on a simple JavaScript Twitter clone utilizing Firebase as the backend storage mechanism (JSON). I am familiar with relational databases (SQL) but not with non-relational databases. I am currently trying to work out how to design the structure of the dataset in Firebase, as there is no foreign key relationships or table joining.</p>

<p>The app has three tables, <code>users</code>, <code>tweets</code>, and <code>followers</code>. Users can post tweets, as well as follow other users and see a feed of their tweets. The problem comes when attempting to create the data structure, as I have no idea how I will join the necessary tables. For instance, how will I be able to implement the user-follower functionality?</p>

<p>Here is the ERD that I am using to give myself a starting point:</p>

<p><a href="https://i.stack.imgur.com/aROkl.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/aROkl.jpg" alt="ERD for app"></a></p>

<p>As I've been trying to wrap my head around this whole JSON thing, this is the closest that I could relate it to a relational database, while still using the Firebase <code>.push()</code> functions to add to the lists in the database (as seen from the Firebase dashboard):</p>

<p><a href="https://i.stack.imgur.com/R2HMF.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/R2HMF.jpg" alt="Firebase attempt"></a></p>

<p>I've seen some people attempting to solve this by "de-normalizing" that data, citing that Firebase doesn't have any query mechanisms. However, these articles are all primarily before 2014, which is when Firebase <strong>did</strong> add queries. That being said, I don't understand how using the queries could help me, and I think I'm getting stuck with the generated keys.</p>

<p>How should I best structure my data to work with the Firebase JSON lists? I've read some of their documentation but haven't been able to locate anything that uses what I'm looking for and the generated keys.</p>

<p>Should I be attempting to use the <code>.set()</code> method somehow, and using the email addresses as the "primary keys" for the users instead of the generated key? <strong>[As mentioned below, this is something I do plan to avoid. Thanks @metame ]</strong></p>

<h2>Update</h2>

<p>Is this more what I should be looking at for the data structure? And then querying by the keys?</p>

<pre class="lang-json prettyprint-override"><code>users: {
    Hj83Kd83: {
        username: "test",
        password: "2K44Djl3",
        email: "a@b.c"
    },
    J3kk0dK4: {
        username: "another",
        password: "33kj4l5K",
        email: "d@e.f"
    }
}

tweets: {
    Jkk3ld92: {
        userkey: "Hj83Kd83",
        message: "Test message here!"
    },
    K3Ljdi94: {
        userkey: "J3kk0dK4",
        message: "Another message here!"
    }
}

followers: {
    Lk3jdke9: {
        userkey: "Hj83Kd83",
        followerkey: "J3kk0dK4"
    }
}
</code></pre>

<p>Let me know if I should include anything else!</p>