What’s up guys, Eze is here. I already did couple of posts about Firebase and how to use the Real-Time database and the storage with AngularJs in case you missed it you can check here. Today I want to go further and show you how to structure your data but more important how to de-normalize your data, yes I’m not crazy we are going to make exactly the oposite we learned with relational database.
I guess this is the case of everyone how has some knowledge about Relational database. One of the first things we learn is how to normalize our database which is reducing data duplication. In this post I’m not saying we need to forget it because if you are working with relational database it’s the way to go but with NoSQL databases things are quite different.
Denormalization: duplicating data
Yes, as simple as that denormalization means that we are going to duplicate data in order to simplify or minimize our queries. If we take as example the model we use in this post.
In this model we need to query our database twice as I showed in that post to get the order data + the user data. And this is fine as long as this query runs over an small data set or is a query that you know is not going to run very often but if this is not the case you will probably want to remove the inner query for good.
So taking this into account one posible solution might be to store the user data we want to use like first name and last name under the order object instead of the reference id we are storing right now.
A rule for denormalization
Basically a good rule to know when is useful denormalize our data structure is to design our database after our views. Doing that we warranty that using one single path we will get the right data to show in our views, also that gives us the opportunity to do further queries in case we need it.
By this time I’m sure that you guys have the following question in your minds: What about the consistency? And you are right we are duplicating data everywhere so how we can warranty the consistency across our database?. Well let me tell you that Firebase has covered that as well with a really good feature called multi-path update that I’m going to cover soon.
For a complete reference of Firebase use this link
For a complete list of querying functions you ca use this link.
If you found this post useful please don’t forget to press the like button and share it. If you are in doubt don’t hesitate to ask a question and as always thank you for reading.