Firebase with React, v2 Firebase with React, v2

The Structure of Cloud Firestore

Check out a free preview of the full Firebase with React, v2 course:
The "The Structure of Cloud Firestore" Lesson is part of the full, Firebase with React, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Steve explains how Cloud Firestore differs in structure from Realtime Database.

Get Unlimited Access Now

Transcript from the "The Structure of Cloud Firestore" Lesson

[00:00:00]
>> Steve Kinney: We are gonna write a lot of code today. So I wanna cover some of the kind of theoretical and philosophical parts first. Cloud Firestore is based on this idea of collections. And this is very different. The previous version of this course focuses entirely on the real time database, and a lot of that is still relevant.

[00:00:18] And that's why we kind of focus in on Cloud Firestore today. That content still works. With Cloud Firestore, you have this idea of a top level collection. In this case, there's users and users have many workouts. And those workouts have histories in this diagram from Fire Base. In the real time database, it was one giant JSON tree.

[00:00:39] And you had to be really careful, because let's say you wanted to make a chat room, and you're like, cool, I'll have rooms, and those rooms will have messages, and those messages will have users, and so on and so forth. If you got any part of that tree, you would get every sub-node, right?

[00:00:55] So you had to be really careful about keeping the tree incredibly shallow, so you can have just chat rooms that had maybe the IDs of the messages, and keep the messages somewhere else. So you could get as little of the tree as you needed. With Cloud Firestore actually, search is shallow, so if you get the users, you're just gonna get this top level.

[00:01:13] And for a given user, you can go get the next collection down, right? So a lot of the really careful data management that we had to do with a real time database Is not as much of a thing anymore in Cloud Firestore. There are kinda some new challenges there which is going across all of the sub collections is a little more difficult.

[00:01:34] So if you're getting to really complicated NoSQL queries, you are still gonna have to do some data normalization as well. But that is now for specific kinda searching cases. Generally speaking, you can structure your data based on your UI. And as you need to pull stuff, right, you can then kind of rearrange stuff.

[00:01:51] And it's really interesting to think about stuff. When we think about SQL databases, we have previously been kind of at the mercy of the database schema. Normalize all the data, don't have it duplicated anywhere, so on and so forth. And this a lot of times when we're working a NoSQL database is a big change.

[00:02:07] Right, for the most part, is thinking about our user interface, and thinking about what we wanna show to the user and structuring our data based on that. And it's one of those things that becomes really kinda difficult to wrap your mind around. Sometimes is, it is okay to store the same data multiple places.

[00:02:23] You might be like, how do I keep that all in sync? When we get to cloud functions there's a way to say okay, if I change it and there's one canonical source. Let me show you all the places to go ahead and update it there as well. And there's ways to go about that.

[00:02:35] But basically, thinking about your user interface first and modeling your data based on that, rather than the, like, bowing to the Lord that is your schema and trying to then sit there and write complicated join statements and course and then back into something that works for your user interface.

[00:02:52] That's really great and a fun way to think about things that I enjoy. So like I said before, in the real-time database you had to be very careful cuz you would get all the sub-nodes of a tree. That is not true in Cloud Firestore, you'll get a shallow query.

[00:03:06] So if you just search for all of the users, you're gonna get those users before you get those sub collections, and you kind of dive down for each one as you need it. So you still need to be mindful, especially if you're doing some complicated searches. But you can be less paranoid that you're gonna like send way too much data to your user.

[00:03:22] And either like cuz theoretically we are developing devices that could run on mobile or lower power devices, like many of us who are writing code have like fancy $3000 laptops, but generally speaking not all of our users do. So we can get to like, we don't have to worry about as much with Cloud Firestore, which I think is a really nice thing.