Learn CRUD with Node.js and Firebase in 13 minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome friends to today's nodejs firebase crud tutorial where today we're going to be looking at how we can implement crud create read update and delete with a node.js backend and firebase as our database it's super easy it's super powerful it's very cheap and cost effective so it's a great thing to know how to implement so without further ado let's get into the code so for this tutorial fortunately i have taken the time to prepare a little back-end rest api that we'll be working with and essentially it just starts off by defining some friends that we have we can use a couple of these methods where we can get a friend we can get a particular friend this one's the whole friends list we can add a friend change a friend and delete a friend if you want to test this out yourself the link to the code is in the description down below so be sure to check that out and if you like the video don't forget to like and subscribe super appreciated chairs but anyway to test this out we're going to be using the test.rest files and if you would like to use this you can do it by coming into extensions and just looking up rest client but anyway from here what it allows us to do is run an http request and so in this particular case i can run that and get my friends list back i could even get a particular friend back or i could add a friend using this post request adding tomato adding them as an enemy so here we can see tomato has been added as an enemy and let's say tomatoes were good for us because they're full of particular vitamins so we can change them to a friend with a new status just here so that updates them to a new status and finally if we wanted to delete banana uh we can delete banana and so that's just the functionality of all these routes however the issue is as soon as our server restarts if i run it again tomato is there now but if i get our friends again we've lost tomato so what are we going to do about it that's just not acceptable well to solve our problem we're going to be using firebase and so here we can just create a new project and this one is going to be crud node firebase we can click enter and continue we don't need any of the analytics all of this is just at the firebase console once you've logged in and it's totally free we can create our project and now that our new product is ready we can click continue and that will take us to our home page and now that we're here for the second we're just going to come back into our application we can close that and down here in our terminal we're going to have to run a few small install commands once again the link to the documentation will be in the description down below but we're just going to say npm install firebase admin dash dash save and now that that is saved we're going to come into our directory make a new file called firebase dot javascript and in here we're going to say const initialize app and cert is equal to require firebase admin slash app and we're going to have a second line that just says const get firestore that is going to be equal to require firebase admin firestore just like that so next what we're going to do is we're going to say const service account is equal to and just have that as an empty string for the second and then finally we're going to call the initialize app function open up and here we're going to set the credentials equal to the certificate and we're going to pass it the service account just like that finally we can say module dot exports is equal to and we can just send out our database like that after we've initialized it just here by saying const db is equal to get firestore absolutely magic the only thing you might notice that we're missing is this initialize app and these credentials and we can get them by coming back into our application and just saying once again web we can give it a register app crud node firebase click register app we can go into settings click project settings we can come under service accounts and in here what we can do is generate a new private key just like that click generate new private key generate the key and this will prompt us to make a download and so here we can just come into our node file and save that just here let's just call it crit creds that will be saved into our project directory just there and so what you can do is now instead of having this we can just require that file so we can just require as uh creds dot json and that's all we have to do to initialize a node firebase in our node.js app so now that we have that initialized we can come into our firestore database and we can create a database and just click start in test mode and enable now once we're done with that we're just going to come into our rules and for this particular instance we're just going to remove this if statement just like this that's all gone and we're going to publish these rules just because the previous rule would limit our publishing ability and whereas this is just totally open in public so that anyone can do it so equally you could come and find these credentials however this will be gone and now we can come back to the data and we can once again start a collection and we'll call this people click next and in here we'll give a document that's just called associates and we won't have any particular key and so that will just initialize these entries a common example would be you could have a collection called users each document could be a particular user id and then all of the fields in here could be the different you know settings or configurations for that particular user anyway so that's cool now that we've initialized that we can come back into our server file and import that file so we can say const db is equal to require and we can just and require firebase dot js dot slash firebase.js just like that and so now if i run our project npm run dev that fails because our firebase needs to look for it just like that so creds.json so now when i restart that that's all working perfectly so that's all good so now that that's all working how do we go about adding friends well let's look at this add friend method first and just start off by saying const people ref is equal to database dot collection uh and then we pass the particular collection which is people.doc and that is associates and now we can use a method here so we can say constant response is equal to and we await uh people ref dot set and in here we provide our set functions and so in this particular case it's just going to be an object where we have the name and in here we have the status just like that except it's already inside an object so we don't have to do that and we can just leave it like this and we are going to pretty much be done just like that so now we can remove this logic or just come to out for the second and if i go ahead and run that it doesn't work because res has already been declared so we'll just call that res2 and it also files fails because this is in our weight syntax so we have to add an asynchronous prefix to our callback function and so now if i come into our test.rest file and let's say we want to add a friend called tomato and we click send request that runs our request it runs as it used to however now if we come into our firebase we can see that that ad entry actually worked and that data is persistent so that is super excellent what on earth do we do next well the next thing that we're going to do is work on the rest of these methods so let's say that we wanted to change the status well it's actually super easy we literally just do exactly the same thing we receive the name and the status in the body of the request and so that comes through in here and so in this particular case currently if we look tomato is an enemy we want to change them to a friend and so we are going to [Music] swap out new status for the old status and add the async keyword just in there like that and now we can go ahead and call this function and if we look at our database we can see that tomorrow is now a friend so that's super cool and what about delete well it's literally just as easy we can come in here get that people ref except now what we do is we still copy that previous response that we used to have except now we use the field value dot delete method just like that and so that has automatically imported up at the top just there so that's all good we can close that and so in this particular case we're going to be wanting to delete tomato whereas it's going to want to delete but we also notice that we have to add the async keyword here so let's just do that real quick now that restarts so in this particular case we have tomato uh this is going to delete banana so let's first add banana so we want to add banana as a friend so we can run this method and this is just emulating a front end so now in here we have banana there however notice that it actually overwrote our previous entry and so we can fix that really quick just by coming up here and when we add our friend we can just add a second object and just say merge is equal to true so now if we post banana that will work and now if we have tomato and enemy we should get both of them and we do so now that's going to merge we also probably just want to add this merge syntax to each of them so just object merge as true it will merge our documents instead of essentially recreating them we do not need it on the delete method however and now we decide that we don't like banana so we run that delete request and that totally didn't work because it can only be used in an update syntax and so in here what we have to do is write update instead of set so if i resave that and rerun that command we do in fact delete tomato so that is absolutely beautiful all of our crud logic is working except for these first two routes so what do we do if we just want to read data well the answer is actually very simple once again we're just going to need to copy this syntax just here and so we can come up into our friends list paste that in there just like we used to we will also add an asynchronous call because it will be an asynchronous call and here we can just say const doc is equal to await peopleref dot get and here we can say if doc dot exists we're calling an exist method we can just res dot uh send status 400 so that's a failed status and if we just return that that will be good and otherwise what we can do is we can just res.send and now we can just say doc dot data because data calls the data method on the document and gets it and so now we should see that we have an enemy who has tomato here let's just add one more we'll add banana back let's say banana as a friend i can't spell we add that we have banana as a friend and now what we want to do is see if our get method works at the very top so we call friends and we get an error and that's because this should actually just be like that not a method and so now we call it and we get back our two responses from firebase and so just like that we've covered all of the crud basis we've done create read update and delete and realistically the sky's the limit in terms of what you want to do with that they have a super high free tier that you can use for just about all projects and even then it's super inexpensive once you cross that barrier but anyway if you liked the video and found it useful please like and subscribe super appreciate it uh and i'll catch you guys in the next one peace you
Info
Channel: Smoljames
Views: 26,247
Rating: undefined out of 5
Keywords:
Id: LefcqnZHYeg
Channel Id: undefined
Length: 13min 43sec (823 seconds)
Published: Fri Jun 03 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.