DAVID EAST: Firebase
Database for-- oh, OK. All right. Let me just redo this one. Hey, everyone. Welcome to the second episode
in the Firebase Database for SQL Developer Series. In this lesson, I'm going
to take a relational model that you'd see in
a SQL database, and then convert
that to a NoSQL model that works well for
the Firebase database. So let's dive on
down to the laptop. So this is a
relational model that backs an event
website that allows users to sign up for events. To model this
relationally, we will create a users table, and events
table, and an attendees table. Attendees is a table that
relates users to events. What creates this relation
is called a foreign key. A foreign key is a key in
one table that references a primary key in another table. In this case, the
UID key in attendees is a foreign key
for the users table. And the same goes
for event ID, which is the foreign key
for the events table. If you want to get a list of
attendees for a given event, you would write a SQL
query that looks like this. Here, we're joining
all three tables to get the relevant data
needed for this query. We're joining
using foreign keys. Attendees is joined to events
using the event ID key, and users is joined
using the UID key. So how would we model
this in a NoSQL fashion? The Firebase Database is
a NoSQL JSON database, which means that your data
is structured by using JSON. Moving from a
relational model, you might be tempted to do
something like this at first. First, we'd create a users
key, where each child key is a primary key for each user. So one is the primary
key for David, and nine is the
primary key for Alice. Then, we have an
events key, where each child key is a primary
key for each single event. So FM is the event for
the Firebase meet up. Now, for a first try,
this is pretty good. But there is one problem. The attendees key is
embedded within each event. And this is a problem due to the
way the Firebase database loads data. When you retrieve data,
you specify a path. To retrieve a single
event, we would create the path events/FM. Now, the Firebase database will
bring back every single piece of data underneath that
path, which may not be what you want
because you don't want to download
the attendees just to display a list of events. Rather than embedding
the attendees, we can break them out to their
own root key in the database. Now I've created
a new collection called event attendees at
the root of the database. This collection uses the
event ID key and the UID key to represent each user. This makes my data
structure flat, which is a good thing
because it means I don't have to download my
attendees every single time I download an event. But if I do need
my attendees, I can do that because I'm using of
the same key, which is event ID. And this is how you
would create a Firebase query using the Firebase SDK. First, you will
create two references, one to the events location,
and one to the event attendees location. You'll notice, too, that
we're using the same key in each collection. FM is a primary
key for the event, so when used with
event attendees, it's like a foreign key. Now, to retrieve the
data in real time, I'm going to create a
listener on each reference. And I'm breaking
the listeners out because we can actually render
to the view independently. And this is much easier than
creating an inner listener that acts as a join. A value listener works
best for the single event because value events work
really well when you're synchronizing objects. And a child listener really
works well for the attendees because attendees is a
list, and child events work well for lists. And just like that, you've
gone from SQL to NoSQL with a Firebase Database. And that's all for today. Tune into the next
lesson, where we're going to take a look
at common SQL queries, and then convert
them to Firebase queries using Firebase SDK. So that's all. I was see you all
in the next lesson. Thanks for watching our video. Yeah, we really appreciate it. You should watch
this video, too. And this one. Yeah. And make sure that
you subscribe. It's really cool.