How To: Create API Using MongoDB and Google Cloud Functions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what's going on everybody welcome back to another episode of trade codes my name is trey hope as always and today i'll be discussing how to set up a back-end api using both mongodb and google cloud functions now mongodb is a no database that's pretty popular on the web which is similar to firebase and google cloud functions is a way that we can upload our own node.js functions to the web and use them right out of the box so in conjunction with the two we'll be able to set up our own backend for apps in the future so it's going to be a lengthy project but i'll be sure to cover everything in detail that way you can start making some back-ends for your own applications in the future so let's get started [Music] [Applause] [Music] all right so the first thing that we need to do is head over to account.mongodb.com it's reading cloud.mongodb.com here but essentially this is a mongodb's website where you can actually monitor your database monitor access and a bunch of other things so i already have a database here so i won't um recreate one essentially because it costs money to do so but i'll just kind of go over the process on how to do so so once you sign in which is completely free you want to go over here to create and this is where you would create a database or a cluster and so you would create a dedicated cluster um there's two other options i'm not too sure what server that's what shared does but mines was dedicated and then you would pick your provider i pick aws just because they're pretty reliable and then you need to pick a region wherever you are located so it doesn't have to be exactly where you're located um i usually pick north virginia um even though i was in ohio but you don't really want to pick something like paris or frankfort obviously if you're in america then you'll scroll down and you'll see here it has options for cluster name this is where you would name your cluster um it could really just be anything uh for my one of my apps critic i just named it the the credit cluster it went from there so after you create it um well you'll hit create cluster and then it takes about seven to ten minutes for it to actually create so you just need to wait a little bit but then once it's ready you'll see it back here and we have cluster zero i guess i named the cluster zero i didn't name it critic but um oh yeah that's right i named my database critic not the cluster see it was just cluster zero so then we would select the cluster um but actually with cluster zero here we need to hit connect all right so now we have the options to connect with mongodb shell connect your application or connect using mongodb express we want to hit connect your application and so here you actually get the connection string so um i'm not going to show the password because if i did then you'd have access to my entire database and i don't want that but as you can see here this is the connection string that we're going to need so we'll copy this and save this for later so just keep in mind that you really need this is very important all right and that is all we do for right now in the next step i'll show you how to actually use mongodb compass which is a gui for mongodb [Music] now we need to open up our gui which is called mongodb compass and i'll drop a link in the description on where you can actually download that but it's a free application that allows us to actually mess with our database a little bit more easily than i'm trying to do it on the website so open up mongodb compass take a little second to initialize we'll full screen this too all right so as you can see here uh we need to enter in that connection string that we copied a few minutes ago so we'll go ahead and copy and paste the wrong one let me just copy that again there we go all right copy and paste and as you can see here this is requiring the password i'm going to go ahead and enter this password now but i'm not going to show it so give me one second all right so i entered in the password for the connection string we are now in a mongodb compass so what we want to do now is create our database so we'll go up here to create database and we will give it a name um for this example i'm just going to create a database of uh we'll just call it my database for now i hate that name whenever i'm doing examples but it's the best best name i can go to right now and then we're going to have a collection which is essentially a collection of documents so we're going to be adding people to a database so we'll call this collection people and then we will hit create database and then as you see here we now have our database of people there's no document in it just yet but we're going to go ahead and get to that in just a little bit in the next step i'll show you how to create your project in firebase [Music] now we head over to firebase because this is where we will be setting up our google cloud functions um essentially that's tied in with some of the things that firebase offers so you don't have to go to firebase to set up a google cloud function but this is where i do it just because it's a little bit easier and it just looks better when i'm working with my actual application so we're going to create a firebase project so we'll go to console as you see here here's some other apps that i already have but we'll be making a new one today for the example so we'll hit add project um we'll call this one mongodb demo and we don't need google analytics for this project since we're just um using the google cloud functions so then we'll hit create project this takes a couple seconds to set up all right and now it says your new project is ready so we'll hit continue and here we are our mongodb demo is not ready and what we have to do is we go over to functions over here on the side and in order to use google cloud functions you have to upgrade your project um the payment plan so by default the project is on the spark plan which is free zero dollars a month but in order to use functions we need to upgrade it to the blaze plan which is pay as you go so this is pretty much going to be charging for uh ability to extend your project with google cloud so we'll hit select plan purchase all right we're now upgraded now we can get started with google cloud functions and as you can see here is giving us directions on our next steps as far as how to uh install google cloud functions for our app so um i'll uh explain more of that in detail the next steps but just know that we need to run three of these commands and then we're good to go but in the meantime i just want to show you here's the dashboard for functions and right now it says ready waiting for your first deploy so in the next step i'll show you how to actually create the project in vs code all right now we need to create our project in vs code so what we want to do is i'm going to head over to my development folder where i keep all of my applications create a new folder called mongodb demo and then i'm going to open up visual studio code and i'm going to open up that folder i just created so as you can see here it's an empty folder so what we need to do is run a few commands to get this going so the first command that you saw previously was um oops not that far uh npm install dash g firebase dash tools uh whoops i forgot i have to run pseudo commands on my mac that's embarrassing but still haven't figured out how to give access to certain file directories on my mac yet so we'll let that run give us everything that we need to run this application all right the next command we need to run is firebase init all right so once we get here it's going to ask us as you see here it says firebase cool little visual it says you're about to initialize the firebase project in this directory which firebase features do you want to set up for this directory so it gives us a bunch of options but all we need for today is functions so we'll hit the space bar to select functions that hit enter then it says uh first let's associate this project directory with the firebase project you please select option so we're going to click use an existing project and we're going to select the one that we just created in firebase mongodb demo we'll hit enter all right then it says what language would you like to use to write cloud functions i usually just pick javascript you can use typescript if you're well-versed in that so we'll hit javascript and then do you want to use eslint to catch it to catch probable bugs and in for style usually i would hit yes on this but i'm going to select no just for ease that way it doesn't give me any errors if there's some syntax some simple syntax problems so hit no and then um do you want to install dependencies with npm now we'll hit yes and we'll just let this run right now all right so as you can see now in this mongodb demo directory we have our functions folder as well as uh some other files uh below it so now what we need to do is we need to go into the functions directory from the terminal and we need to download the mongodb package for our node function so we need to run npm install mongodb and this will let us actually use uh mongodb for our node functions so as you can see here this is an empty this is an example function that you could use um just a simple one that says hello world but this is essentially where we will be coding up our cloud functions so in the next video i'll show you how to code the crud functions which is uh well crud means create read update and delete but i'll show you that in a second [Music] okay so now we need to go ahead and create our functions so we need to come up here to the top first let me close this and we need to create some imports first so first we need to create a reference to the mongodb client and we do that by calling with require mongodb dot mongodb client um we also need to create an instance of this thing called object id which is the way that db uh references its documents in the collection so you can add a normal id but by default you get an object id on every document and we need that when we're using our read and delete functions so we'll go require um mongodb dot object id uh there we go um and we already have functions in here we need that uh then we need to create the client and we do that by calling client uh we'll pass in the connection string um which was this and before i deploy the functions i will change this password to my password but for now i don't want you seeing that so we'll just use this um and then we need to add some brackets and then we need to have use new url parser true and use unified topology true i spell that right i think i did okay all right now we need to add our database name you don't really have to do this but i like it just just to create a variable for my database name and the database was my database and then our collection name was people all right so now that we have that we can delete this function we don't need that um we're ready to create the first part of our crud operation and as i mentioned before crud c-r-u-d stands for create read update and delete so every database should have these four basic functions any database without one of those is not a real database and therefore is lacking in that area so first function is called create person so we'll call exports oops exports dot create person now the name of the function next to exports is what the name of the function will be in the google cloud so make sure it's something that something that you can identify easily so we'll call functions https on request and it's an async function so we'll add the async modifier and then pass in um rec and res so uh rec is how we get the actual properties that hit the endpoint and then rez is how we send the response back so the response so requirements and response i believe that's what rec stands for don't don't quote me all right so we have our base of our function now we need to create variables for this function so when we create a person we're going to pass the first name last name and age to the endpoint so we'll call rec body first name same thing for last name wreck body last name and then an age as well all right now we need to create a try catch to make sure that if the endpoint doesn't work we uh catch the error so let's go ahead and write that catch out now so there's an error we return the response and we send the error back but if everything is good we go ahead and connect our client so we call client.connect um yep client.connect um and first we see uh if there's an error with the connection as well so if if error we throw error otherwise we go ahead and create a json object of our data to pass to uh the actual function and that data is just going to be the first name um last name and age now first name and last name are both strings but the age is going to be a number so we need to call parseint on this to make sure that we convert it to a number that way it doesn't show up as a string in our database all right so we have the data now we need to call clientdb pass in our database name then we need to call the collection of that database and we'll pass in the collection name and then we need to call this function called insert one and what insert one is it inserts one document to our database so we're passing that data that we just created right here and then we need to also check for errors when we submit that to the database so we uh pass in the data and then in our return function we have an error uh or the uh we could have a variable right here like user or id or something but as long as we don't have an error we just assume that uh the insertion was successful um so if air just like above throw air otherwise otherwise return uh true to let us know that hey it was successful so this right here is our function for create person all right now we need to go ahead um oops i'll move this out the way so we have more room now we need to cr uh code our function for reading a person all right and i'm gonna copy and paste this code and uh just modify it a little bit just to save some time all right so when we read a person we don't need the first name last name age anymore all we need now is the id of that person or that document so to speak all right so we take out the data all right and now this time instead of insert one let me add a semicolon that instead of insert one what we want to call is find one and what find one does is obviously it finds one document matching um matching the criteria of the filter that we pass in so the filter that we're going to be using is we want to find the id and be careful the id that we're looking for is underscore id so that's the id of the document itself if you pass in just id it's going to be looking for an id property that you created not the id that it gave to the document but i'll show that in a second so we'll say id new object id and then pass in that id that we got so i know this is a little confusing but the way it works is it looks for object id now even though it says it's deprecated it's still useful um i mean it's still used so don't worry about that you can ignore that but you're looking for the object id of the id that you sent all right so then uh our return function brings an error or user right and just like before um if there's an error we throw an error otherwise what we want to return is the user itself so so instead of true like above we want to return the user into information all right now we need to write our function for updating a user or a person i should say so this time when we update we want to update based on first name last name and age as well but we also need the id to know which document that we're updating all right so instead of find one what we want to do is update one all right we're going to update one document based on uh the id similar to up here so we can actually just copy and paste this all right so we're updating that id and what we need to call is uh this function called set and what set does is it sets those properties on that document to the ones that we that we specify right here so we're gonna say first name is going to be first name last name last name and then age parse and age all right let me move these down a little bit so it's easier to view all right so we have that in there um now we need to do our return function as well so if there's an error obviously we'll throw the error but otherwise um we can just return true similar to the create function so we'll go up top copy and paste that all right so that's our update function and finally we need to write our delete function okay so say delete person you know what i wasn't changing the names of any of these let me go ahead and do that now delete person update person okay so for our delete person all we need is the id so really we can kind of copy and paste this read one that probably a little bit easier all right so we have the id so instead of calling find one all we need to do is call delete one and this means that we'll be deleting one doc one document from the collection based on that id all right um as always if there's an error or error otherwise if it's deleted successfully we return true all right so this is all of our crud operations now what we want to do is deploy this to firebase okay so we need to open up our terminal again all right and now we want to call firebase deploy so this is going to deploy all these functions to the google cloud so you see here we're going to let that run give it a few seconds um required oh typo only required try that again all right so after a couple minutes you can see here that it says we had a uh successful deploy all of our functions so if we go over to functions in firebase hit refresh we can now see we have all four of our functions to create delete read and update so that's all it is to actually get the functions uploaded now in the next video we'll go ahead and test each endpoint [Music] all right so now that we have our google cloud functions uploaded we now need to test these endpoints and i use a extension in vs code called thunderclient it's free to download and it integrates very well with vs code so i'll be using this instead of postman today so to test our first function we need to go to google cloud functions in firebase and copy that endpoint for create person so this is the first request that we'll be testing so we'll paste that endpoint in we're going to scoot this over a little bit so we can see the full url and then we'll close this it needs to be a post um because all of our methods are post method and for the headers we have to add content types and then application url here we go so this is just how uh this is how the endpoint reads what type of data you're sending um you could pass in json as well if you were using json for the body but for me i'll be using form and code so we'll use that one all right so first thing we need to add is the first name which is trey uh last name hope and then the age 29 so it's in we get back true and if we come over to our database hit refresh we now see that we have our first document with the object id first name last name and age so as i mentioned earlier that object id is added by default so you don't even have to worry about adding ids for your documents which makes it very easy uh the next uh part of our endpoint we need to test is the read function so we'll copy that endpoint paste it in here and we need to pass in an id this time and the id is going to be that object id on our document so we'll add that to the id and we'll turn off age last name and first name because since we're using the read all we need is the id and as you can see here we get back that document that we just saw the next one we need to test is the update function so we'll come in here paste that and we'll turn these back on because we want to update the first name to be trayvon the last name to be johnson and then we'll go two years in the future and i'll be 31 and we'll use the id to specify which document we're updating so hit send get back true and if we refresh our database we now see that the first name is now trayvon johnson age 31. the last one we need to test is the delete function so we'll come in here paste that turn these off again because just like the read function all we need is the id for the delete function so we'll hit send get back true refresh and now there's no data so all of our crud functions work properly and this is essentially how you set up an api back-end using mongodb and google cloud functions in firebase all right so hopefully you got a better understanding of how to use both together to make a back end i only went over four of the functions today there's a lot more that mongodb offers such as listing documents sorting documents filtering documents and things like that but you get the gist of how to actually start working with mongodb at the back end um via google cloud functions so as always if this video was helpful like comment subscribe and always hit me up if you have any other ideas for what you would like me to discuss in my next video until then have a great day and i'll see you next time [Music]
Info
Channel: Trey Codes
Views: 197
Rating: undefined out of 5
Keywords: appstore, iphone, ios, app, flutter, upload, dart, googleplay, playstore, android, apple, apps, game, mobileapp, googleplaystore, mobile, games, mobilegames, google, tech, applewatch, download, gamer, mobileapps, technology, mobilegame, newapp, itunes, googlecloud, googlecloudfunctions, functions, mongodb, compass, nosql, nodejs, endpoint, endpoints, backend, database, thunderclient, vscode, ide, code, coding, development
Id: YbfIwC6vOsk
Channel Id: undefined
Length: 29min 11sec (1751 seconds)
Published: Tue Sep 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.