20

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: "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"
    }
}

Let me know if I should include anything else!

Kendall
  • 1,992
  • 7
  • 28
  • 46
  • 3
    I recommend reading this [answer to someone trying to do something similar](http://stackoverflow.com/questions/16421179/whats-the-best-way-of-structuring-data-on-firebase/16423051#16423051), this [answer to a simple NoSQL data modeling question](http://stackoverflow.com/questions/16638660/firebase-data-structure-and-url/16651115#16651115) and this [article explaining common NoSQL data modeling techniques](https://highlyscalable.wordpress.com/2012/03/01/nosql-data-modeling-techniques/). – Frank van Puffelen Apr 13 '16 at 01:14
  • 3
    For an interesting sample of a Firebase twitter clone, look at https://firefeed.io/. Since it was made by the Firebase engineers, it is bound to include many of their ideas of good practices when it comes to NoSQL data modeling. – Frank van Puffelen Apr 13 '16 at 01:24
  • So looking at firefeed.io, is it correct to say that some queries can be avoided by using rules? For example, rather than trying to query who should be updated by a post, rely on rules to only update followers? – Kendall Apr 13 '16 at 01:43
  • 2
    The rules can be used to ensure that your application code can only update followers. It doesn't do the actual update (only code can do that, and the security rules are not code), but it can be used to ensure that only valid updates are allowed. Note that all of this will not start to make any sense until you try actually modeling your data in Firebase, write a few security rules, write some code and then ensure that everything works the way you expect it. – Frank van Puffelen Apr 13 '16 at 01:56

1 Answers1

8

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.

metame
  • 2,480
  • 1
  • 17
  • 22
  • 1
    Thanks, I actually didn't think to look there. I did however visit the link and think that it makes a bit more sense. I've posted an update with some sample lists in the question, am I on the right track? I am worried that the Firebase web API might not play too well with attempting to query this, as their page mentions de-centralization and looks rather different. Although maybe that is just me? – Kendall Apr 12 '16 at 23:14