Data has relationships, and Firebase allows you to define those relationships in your AngularJS applications.
Hello, this is Lukas Ruebbelke and in this tutorial I'm going to show you how to structure your Firebase data so that you can have relationships between data objects.
Now this is a slightly advanced topic and something that I had to actually work a little bit to wrap my mind around about how to actually structure your data to allow you to say, "Get me all of the items based on this criteria."
In Firebase, all of the data is actually completely denormalized. It's just one large data object, and you do not have the ability to say, "Give me all of the items based on this query."
You have to structure your data slightly different in order for this to work. In the example, what I have is a drop-down of users. Then I'm listing the items for that user. Then I have the ability to actually add an item for that user.
This is what we're going to be building out over the course of this tutorial and you'll see how to actually keep your items and your users separated, but then establish the relationship between the two in the code.
We have a user service that we're going to be filling out here that is going to involve adding items, getting items, removing items. Then I have an item service that we're going to be adding items to.
Then also I have one more piece that I'll get into in just a moment is I have an item directive that deals specifically with a single item. We'll see why that's important in just a moment.
The first thing we need to do is when an item is added we're sending in an item object. Let's jump down and let's do this method first.
We're going to call add on the items array. We're going to pass an item.
Now when this is completed we have a promise that gets returned that has a primmer that is the reference object that was just created. This is really useful when you actually need this information to create another item.
What we're going to do is we're going to take this reference item and we are going to call this add item for current user and pass this in.
What we're doing is we're saying, "Create an item in the items collection and then when you create that item, pass in the reference or the key and we are going to pass that over to the users." Then from here we're simply going to say...
We're going to get a child reference for the current user, but we're actually going to be building out the endpoint for this manually. We're saying, "The current user items." Then we actually want the item ref name. This is essentially the key.
Then from there, we are just going to set this to true. We could set it to anything, but we need to actually set it to something so that it goes into the remote collection.
We're going to hop over into the demo and we are going to select myself as a user and we're going to add a new item.
Let's look at the actual Firebase so you can see that we've created a new item here, name new item, and you can see the key here.
Then if you look down into my user object, we've created an items property that we have pushed the key for this item into it and we've set it to true. Now we have a reference to this item's object.
Essentially what we're doing is creating an array of keys that we can then point to and iterate over in just a moment. The very next thing that we need to do is to actually jump back into the code and let's get the items for the user.
From here we simply can say return users child. We're going to set the current user plus item. Now we're simply saying get us all the object with all of the items and the item keys for the current user.
Then from here we select my name. Now you can see that there is this item here.
Firebase instances are very lightweight and so I've had hundreds of these on a page and there's been no problem. It's just easier to actually than take the key and then create an individual reference to that via a directive, so it provides some nice encapsulation.
From here I'm saying get the item ID and then create a new Firebase reference for that. Then I'm getting the URI items and then the item ID, which matches up to this data structure here that says, "Items." Then I'm passing this in so that I'm getting this particular item.
Then from here is that, once it's changed then I'm saving it internally. But if I need to actually remove it, then I go ahead and I delegate that up to the item service since it's more of a global concern.
For remove item, then we simply go items, remove, item, ID. Then using the promise again.
Now this is slightly different because we actually know the item ID that we want to remove now. We don't actually have to even wait for the reference or use that.
We'll call remove item for current user and we'll just pass this item ID through. Then up here we are going to simply go...like so.
Let's hop into the application and let's go ahead and we'll delete this new item. We're going to click remove. It's been deleted, but let's look over. You can see now we no longer have any items in our collection and it's been removed from it.
Let's just see this real quick one more time in action. Let's create an item for Joel. OK, so we can see we've created this item.
Let's create an item for John. Then we'll create an item for myself.
What we should have now is three items in our collection. We have the new new item, John's item, and Joel's item. But then if we look here into the users, we have an items collection with the IDs for the items that we were created that now we can reference and then iterate over.
When you actually select a user it's going and saying, "Get me all of the item IDs for this user." Then we in our HTML is we are iterating over this.
We're saying, "ID item and items." Then we're instantiating this item directive, passing the item ID, and then handling it as if it was an individual Firebase object.
The big thing to take away from this video is that when you are handling relations in Firebase, you need to essentially manually normalize your data.
Using the promise that is returned from the creation of an item, you can then use that reference to push that key into basically, essentially another data structure and then pull it out in your HTML.
This concludes this lesson on establishing relations in Firebase and how to structure your data. Stick around for more videos on this topic. I look forward to seeing you next time.