Firebase & NodeJS Tutorial: Creating Your First Project and Setting Up Firestore

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's dive into the process of setting up a Firebase project and creating a database using fir store in this tutorial we will also learn how to connect to our fir store database using a node.js application to start we need to go to firebase.com and click on get started from there we can create a project with any name of our choice after accepting the terms and conditions we can proceed to enable analytics which is free of cost we can configure the location of our analytics but for the time being we can go with the default options once the project is created we can click on continue and we will have successfully created our Fire based project currently we are on the spark plan which allows us to use a variety of applications for free including hosting and Fir store which offers one GB of storage to to enable fir store we can click on all products and then Cloud fir store from there we can create a database and start in test mode without any restrictions on who can access the data we can select the region that is closest to us and our database will be created successfully to access this database from our code we need some credentials we can go to the project overview and select web to create a no do JS application to access the database we can give it any name and select Firebase hosting which we can use to host our front-end application for free in the future for now we can enable it and register the app next we need to use npm to install Firebase and use the provided code to connect to the Firebase Firebase offers a CLI that gives us a lot of control over accessing Firebase using the CLI we can inst install it globally and access it using Firebase finally we need to take these steps to host our application in future video series we will deploy our front-end application and explore these options further for now we don't need to do anything else here so let's continue to the console where we can see the web application we have created now let's let's proceed to open our node.js application and create a new file called Firebase under the lib folder but before we start writing anything it is essential to set up the environment variables required to connect to Firebase if you need assistance in setting up the environment file kindly refer to the link provided in the description to get all the necessary information go to the web application and click on the settings icon scroll down to find all the relevant details moving on in the Firebase file we need to import the initialize app method from Firebase SDK to do this we need to First install Firebase in our project additionally we also need to import the error Handler function to log any errors that may occur for more information on how to create an error Handler kindly refer to the link provided in the description next we need to import all our secret variables from the environment file and create the Firebase config required to connect to the Firebase app all the necessary details can be found on the website we will then create a variable called app to hold the Firebase instance that we will create we will create a function called initialize Firebase app to initialize the Firebase SDK by calling the initialize app from Firebase method and passing all the the Firebase configs that we created earlier additionally we will create another function called get Firebase app which simply Returns the Firebase app instance these two methods will be exported now let's see how we can connect to the Firebase fire store to do this we need to import a method from the Firebase SDK called get fire store we will then create another variable called fir store DB to hold the database connection once we initialize the app inside the initialized Firebase app function we will get the fire store and assign it to the fir store DB variable before we proceed with any crud operations it's important to understand how files are stored in the fire store database on the root level of fire store we have a collection which can be thought of as a folder we'll create a new collection in our database and name it anything we like such as users or transactions for now let's call it testing within a collection there are multiple documents which are like files inside that folder we'll create one document for our collection and the document ID should be unique for each document we can autop populate this ID every document is a simple key value pair where we can define a key and its data type and value a collection can have multiple documents and a document can be a simple key value pair like the one we just created or it can be a whole new collection itself we can have nested documents and collections now let's create a new collection called receipt and see how we can update and retrieve data from this collection using our nodejs server first let's take a look at how we add new data in our Firebase file we'll import two methods from Firebase fir store doc to create a new document and set doc to push the created document into the database we'll create a method called upload processed data and create some temporary data to upload to our database we can have any key value pair with any data type we'll create the document we want to create using the doc method which takes three arguments the first is firestore DB our connection to the fir store database the second is the collection name and the third is a unique ID for that document we'll push this document into our database using the set doc method passing the document we want to upload and the data for that document we'll export this method and test it out in our index file we'll import the upload processed data method and test it On A New Path called test upload even after hitting the API we don't see any data so we'll check our logs the first argument is undefined the issue is that we forgot to initialize our Firebase app we'll initialize the Firebase app in the server starting file and test it again awesome we can see that the data was uploaded successfully and Fir store automatic Ally assigned appropriate data types to the values now let's see how to retrieve data from fir store to do so we need to import some methods from Firebase fir store these methods include collection which defines the collection we want to retrieve data from get do which retrieves the collection we defined earlier from the database and query which filters the exact data we want to retrieve from that collection we will create a method called get the data which will start by cre creating a collection reference this reference is a reference to the collection we want to access data from it takes two arguments fir store DB which is the fir store database connection and the collection name which is the collection from which we want to retrieve data we will then push all the data we retrieve inside the final data array next we will create a query in the query we can specify exactly what data we are looking for for now we will retrieve all the data from our collection reference reference we will get the data using the get docs method which Returns the document snapshot we can Loop through all the snapshots and get the data out of it finally we will return all the data we retrieved in our index file we will create a new path to test this function we will return all the data we receive from the function when we hit this URL we can see an array of data the first empty object is the empty document we created earlier let me create some test documents I created three more documents with different dates inside key3 we will then learn how to query the data with some conditions such as getting data between a date range for that we need to import a new method called where which is useful to add conditions in inside our query we will add a condition telling key3 should be greater than or equal to this date and less than or equal to this date these conditions should be inside the query not inside the get dos method we can see that we get only three out of four documents since one of the documents Falls outside our time range we can query with any key value condition if you want to learn more about the wear method there is good documentation available which I will link in the description we have successfully connected to the database and in the next tutorial we will see how we can put all the things we learned so far and build our final receipt tracker bot in telegram thank you for tuning in and I'm excited to meet you in the next tutorial
Info
Channel: Lets build together
Views: 10,204
Rating: undefined out of 5
Keywords: firebase, firestore, NoSQL, Node.js, database, web development, CRUD operations, tutorial, beginner's guide, Firebase project, data management, programming, web apps, real-time data, Firebase tutorial, Firebase Firestore tutorial, NoSQL database, data storage, web development tutorial, Node.js tutorial, Firebase CRUD, learn Firebase, Firebase Firestore CRUD
Id: sh7xiO7jnV0
Channel Id: undefined
Length: 11min 52sec (712 seconds)
Published: Sun Oct 15 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.