PWA Tutorial for Beginners #23 - Firestore Database

Net Ninja · Beginner ·🔧 Backend Engineering ·7y ago

Key Takeaways

Integrates a Firestore database into a Progressive Web App

Full Transcript

okay the gang so far as we've been making this up we've been cashing different assets so that we can view our app offline which is awesome but as of yet we've not worked with any kind of data yeah we have this dummy stuff right here but it's not real data and when we start to work with real data and a database that is a completely different ballgame - cashing assets when it comes to using data offline we need to take a slightly different approach than using our service worker to cache data responses because the data could be constantly updating and changing in the database if we start caching the responses from that database then we might always be showing old data to the user for a long time so the way we deal with data in a PWA is a bit different and it's going to vary depending on which database solution that you're going to use for the app now for some databases you're going to find yourself using the indexed DB API right here directly to keep things in sync and allow offline data use that is an option but I'd like to use a database service which makes accessing and using data offline even easier and it will use this under the hood but we're not going to have to directly get involved with it so the database solution that we're going to be using is going to help with all this and it's called firestore it's created by Google fiber's now Google firebase is a bunch of different services which act as a back-end to our application and we can see all of those different things right here we have cloud functions that enables us to run code on a server we have authentication hosting cloud storage and we have this cloud firestore database right here and this comes built with offline support which is perfect for our PWA meaning that it will allow users to view data when they're offline as well as online and firestore takes care of all of that offline data for us we don't really have to do much at all which is freaking awesome it does this by interacting with that index DB the browser's built-in database right here and it stores data there for us so that when we go offline we can still access it so the first thing to do is sign up for a free firebase account and once you've done that just go to your console over here and this is going to list your different projects I already have a couple but I'm gonna make a new one and I'm gonna call this food - ninja - PWA okay so I'm gonna accept the terms at the bottom and create this project this will just take a few seconds and then when it's done just press on continue and this is going to take you to the control panel for this project now this area right here is essentially going to act as the backend now to our application and we could use any one of these different services authentication the database storage etc we're not going to focus on all the different things if you want to learn more I do have a whole series on authentication and on fire store and on hosting so feel free to check out those playlists on my youtube channel for now we're going to focus on the database and again I'm not going to go into a lot of depth about fire store I'm going to teach you all the basics here and how to hook it up with our PWA but if you want to learn more about fire store I'll leave a link to the complete fire store playlist down below anyway once you've clicked on database create a database make sure it's a cloud fire store one and not the one down here the real time database so create this database and we want to start in test mode which means we can easily develop and we can access all the data from our application over here but later on make sure if you publish your app you address the different fire store rules so that only the appropriate people can access the data okay so let's enable this for now and this here now is our fire store database so in fire store databases we have collections of data and each collection will store documents of a particular type of data for example we could make a collection called users and that could store user documents so each document would represent a single user right what we're going to do is make a collection called recipes and that's going to create the collection for us when we first create a collection it's going to ask us to also create our first docking for that collection now this document ID right here this is going to be a unique ID for each individual document so that if we want to get a reference to a particular document later on we can use that unique ID and everyone is going to be different now we don't need to fill this in ourselves it's going to auto-generate if we leave it blank so what we want to do is make up a few different fields now fields and values are basically key value pairs a bit like a JavaScript object so we're going to have a property name and a property value essentially and each one has a type as well so we're going to say we want a title of the recipe and that title could be something like ninja soup and we also want to add a new field and that field is going to be the ingredients and this is going to be mushrooms onions and garlic okay so now we're going to save this document and it's going to save it to this collection so we see now we have this recipes collection right here and in a second we're going to see this document right here and when we click on this document it will open up on the right and we can see the two different properties ingredients and title and the values of those properties as well and this is the unique ID the google firebase created for us as well okay so let's add one more document by pressing on add document over here again leave the ID blank it's going to also generate this time we'll do a title property of veg burger and the ingredients are going to be soy beans onion garlic again why not and mushrooms again why not okay so save that and now we have two different documents and the ID is unique for each one right so we have this one ninja soup and also the veggie burger cool so now we have a bit of data stored inside our fire store database and what we want to do is hook up our application on the front end with this database on the back end so what we're going to do now is go to the project overview and then we're going to add an app so to do that click on this web and then we're going to register an app so what this ninja food and register the app and then this is gonna give us a bit of code that we have to copy and put into our project so let's just copy that right here and over in our index file let's open this up and come to the bottom and I'm going to paste it just above these other files right here so let me paste that in and just scoot this in okay so now what we're doing is putting in a link to the firebase app right here we also need a link to the firebase of fire store so I'm going to duplicate this and add in firebase - fire store and we need that because we're using the fire store so let's get rid of this comment right here and then this is the configuration for our application basically so that we know which back-end to connect to and then down here what we're doing is initializing our firebase up on the front-end we also need to initialize our database instance from the front-end so that we can communicate with the database from here as well so I'm going to store that in a constant called DB and set it equal to firebase dots fire store like so and now in the future if we ever want to communicate with the database from our own application we're going to use this DB constant to do things like get data or save data etc okay so the last thing I want to do is actually create a JavaScript file over here so I've got a new file and I'm going to call this DB j s and this is going to be all the JavaScript that we write to interact with the database so I'm going to hook that up from the index file as well so let's do another script tag and we'll say the source is going to be equal to /j s /d b GS so now we'll hooked up to that as well and so now we're in a good position to start communicating with our database and we're going to start that in the next video

Original Description

Hey gang, in this PWA tutorial we'll bring a real-time database called Firestore (by Firebase) into the mix. ---------------------------------------- 🐱‍💻 🐱‍💻 Course Links: + Course files - https://github.com/iamshaunjp/pwa-tutorial + Modern JavaScript Tutorial - https://www.thenetninja.co.uk/udemy/modern-javascript 🐱‍💻 🐱‍💻 Other Related Courses: + Firebase Firestore Playlist - https://www.youtube.com/watch?v=4d-gIPGzmK4&list=PL4cUxeGkcC9itfjle0ji1xOZ2cjRGY_WB Firebase Authentication Tutorial - https://www.youtube.com/watch?v=aN1LnNq4z54&list=PL4cUxeGkcC9jUPIes_B8vRjn1_GaplOPQ
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related Reads

Up next
Indian Express Editorial Analysis by Chandan Sharma - 1 JULY 2026 | UPSC Current Affairs 2026
StudyIQ IAS
Watch →