Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Firebase as a relational database with generated keys (JS)?

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.

The app has three tables, users, tweets, and followers. 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?

Here is the ERD that I am using to give myself a starting point:

ERD for app

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 .push() functions to add to the lists in the database (as seen from the Firebase dashboard):

Firebase attempt

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 did 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.

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.

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

Update

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

users: {
    Hj83Kd83: {
        username: "test",
        password: "2K44Djl3",
        email: "[email protected]"
    },
    J3kk0dK4: {
        username: "another",
        password: "33kj4l5K",
        email: "[email protected]"
    }
}

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

followers: {
    Lk3jdke9: {
        userkey: "Hj83Kd83",
        followerkey: "J3kk0dK4"
    }
}

Let me know if I should include anything else!

like image 435
Kendall Avatar asked Apr 12 '16 22:04

Kendall


People also ask

Is Firebase database a relational database?

The key differences between Firebase and MySQL: Architecture: Firebase is a NoSQL database that stores and syncs data in real-time (a real-time document store); MySQL is an open-source relational database management system based on the domain-specific language SQL.

Is Firebase a key-value database?

Firebase uses a very popular style of NoSQL database: Key-value stores. The concept is not so complicated once you know the basic JSON syntax.

Is Firebase a key-value store?

Firebase is categorized as a NoSQL database program, which stores data in JSON-like documents. In Firebase, a document is a set of key-value pairs defined by a schema.


1 Answers

Representing relationships in non-relational or noSQL databases, in general, is solved either through embedding documents (noSQL verbiage for rows) or through document references as you have done in your example solution.

The MongoDB site has some decent articles that are mostly applicable to all non-relational databases including Model One-to-Many Relationships with Document References, which I think is most relevant to your issue.

As far as the reference key, it is typically best practice to use the generated IDs as you have assurance that they are unique.

like image 120
metame Avatar answered Sep 30 '22 17:09

metame