Subscribe for tips on developing apps with Angular and Ionic

Jumping from SQL to Firebase NoSQL Database

Menu

This is a guest post by Jorge Vergara who has written many tutorials about using Firebase in Ionic apps. In this post he writes about how to structure and update your data in a NoSQL database like Firebase.

As a new Firebase user, you're probably still trying to wrap your head about all the querying possibilities you used to have when working with MySQL or PostgreSQL databases.

SQL databases use tables to store data, those tables have columns and rows where the data is stored.

A column is basically a property of the table.

For example, a table could have 3 columns: ID, First Name, Last Name.

And the rows are the records inserted, so if the columns were id, first name, and last name, then the first row in my case would be "ebrattJorge, Jorge, Vergara".

Where:

ebrattJorge is my ID.

Jorge is my first name.

Vergara is my last name.

If I want to add my email I can't just send it with the rest of the data ebrattJorge, Jorge, Vergara, j@javebratt.com that won't work, since the email column doesn't exist on our table.

I'd have to modify the table to accept the new value, this has its pros too, for example, it's making sure data integrity remains throughout the entire database.

Firebase (and mostly all NoSQL databases) on the other hand don't have this kind of restrictions.

The Firebase database is just a big JSON object where you can store whatever you want inside. Unlike SQL there's no schema for the database, no tables, no columns, it's just a combination of key/value pairs.

Basically, you can just add whatever information you want to add to the user's node. This is really great for flexibility but doesn't really help us ensure data integrity. So you take the good with the bad.

In this post, I'm going to do my best to guide you in how to structure your data in Firebase, so allow ultra fast reads, minimal querying, and how you'll accomplish a common SQL query just restructuring your data.

We'll break it into 3 parts:

Best practices for the data structure.

A practical example of who to structure the data.

A way to ensure data integrity when updating.

Firebase Best Practices

One of the best ways to ensure your app is going to stay performant is to keep your database connection "clean", meaning that there isn't too much clutter, too many database calls for things that you don't really need to.

With that in mind, let's jump right into best practices:

READ > WRITE

You should be thinking by default on how you're going to be reading the data you're about to store in the database.

Statistically speaking, the average user is going to spend more time reading data from the database than writing it, so you'll want to make sure reading the data is as painless and uses as little resources as possible.

For example, if you're building an event management app (...which I once had to do for my wife :P ) you might be thinking about how to store your users, the events you're creating, and the relationship between them.

This is a great way to start de-normalizing data, and for most use-cases, you can just leave it like this, but if you think your app will be "big" then there's a catch here.

Firebase uses references to pull your data, for example, if you want to pull the "National Pizza Day" event you'll do something like this:

let eventRef = firebase.database().ref("events/natlPizzaDay");

That will bring the entire event information, so if all you need is the event name + date then this is not a really great idea, you're going to be pulling a ton more data than you need to if those inside lists start growing.

But don't worry, there's a way to go around this.

Keeping your data flat

Firebase supports deep nesting (up to 32 levels I think) for your data, but it's a really bad idea to go that deep, imagine only needing the event name and pulling instead the name + a list of 5,000 people that attended to the event (and National Pizza Day? More like 5,000 million people)

If you're coming from SQL, imagine this as a "Many to Many" relationship, where you create an extra table to store the relationship.

For example, let's go back to our event/user relationship, what if instead of storing the guestList inside the event node we create a separate table for it?

And that's just a big fat NO, you can't allow that to happen, for that you can manually update every other instance where the user's name is (and by manually I mean you can create a function for it), so it would be something like:

That's OK I guess, the problem is that you have no idea what the user's Internet connection is like, what if you have to update the name in several places and when the app has updated 2 nodes there's a power outage?

Then you will have a few updated and a few outdated nodes, which means you'll be jumping manually (and this time I DO mean manually) to your database and one by one updating those names.

You know better, your time is too valuable for that.

For those cases Firebase has a cool feature, it's called multi-part updates, basically it's one function where you chain every single update you need to happen and Firebase treats it like ONE update operation.

If all of the updates clear, then the function will return a success, if somewhere through the updates it fails, it will rollback and won't change anything, that way you can try it again when power/the Internet is back.

This is when we try to get a bit of that data integrity that SQL databases provide :)

Let me walk you through a step by step of how this update would happen in our little example:

That's an atomic operation (kinda like Git commits) so it will either update all the paths or none of them.

Next Steps

By now you should have a better understanding of how data is structured on Firebase, your next steps should be to actually put this into practice while building your next Ionic and Firebase app.

I've created a little care package to help you get started building those apps, in includes:

A free 7-lesson course that takes you through building your first app with Ionic and Firebase (setup, authentication, objects, lists, Firebase Storage, and more) using the JavaScript SDK.

A free 5-lesson course to help you get started with AngularFire2, a really cool library built by angular and Firebase team members, which lets you use real-time bindings and observables in your Ionic + Firebase app without any heavy lifting.

Access to a code library where I have the source code of over 10 apps that I've built while creating tutorials for my blog (form validations, email authentication, ionic native, and more).