Unlocking the Power of NoSQL: FastAPI with MongoDB

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey friends and welcome in this video we are going to be creating a fast API application and connecting that fast API application to a nosql mongodb let's dive into the code and let's create our fast API mongodb application so before we get started with learning how to you know set up fast API with a nosql database what we're going to need to set up is our account on Atlas now Atlas is a cloud platform for nosql mongodbs so it'll require you not to have to install anything on your Mac Windows Linux whatever your operating system is you do not have to install anything and what's even better is it'll be all hosted on the cloud and it's free so it's free hosted on the cloud so that's what we're gonna do so first let's go to cloud.mongod dot com and here you need to create an account so you'll need to create an account it's free to create an account and then it's free to create a cluster after you do this let's head over to database under deployment and click build a database all right once you click build a database it's going to give you three different options I'm going to click the m0 which is the free mongodb Cloud environment here we can select our provider which is AWS gcp which is Google Cloud or Azure I'm going to go with AWS here you're going to want to click the region that's closest to you for me it's Northern Virginia and then we can name it something so I'm just going to leave it as cluster 0 which is the default but you can name it whatever you want after this let's go ahead and click create after you click create it's going to tell you that hey we're going to start provisioning your cluster but you're going to need to pass in a username and password I'm just going to say admin as the username and for the password I'm going to say test1234 exclamation mark and then I'm going to say create user and here we can see that it just created the user of admin password and then we can say finish and close so congratulations on setting up the access rules let's go to the database and here it's going to start creating our cluster which is our cluster zero so it's going to be our client connection to our mongodb and this is important because when we create our fast API application we need to connect to our client which is going to be this cluster zero and then from here you're going to need a database inside the cluster which we're going to name like to do application and then we need a collection which is similar to a SQL table so it's going to be a table which is known as a collection our DB and our client and creating a cluster can take a little while so I'll come back to the video once this is complete okay so now we have our cluster zero which is our cluster for our nosql database here we can do a couple different things we can go to view and monitoring and browse collections but as you know as of right now we don't have anything inside our cluster right and we want our fast API application to create these clusters and these databases all automatically so from here let's just click connect and you're going to get a few different options we're going to get our drivers which is going to be the way that we pursue it's the native drivers implementation but we can also use Compass we can use shell mongodb for vs code so if you're a huge vs coder that might be a good option and then Atlas SQL but let's go ahead and just click drivers for right now and here we need to select the type of driver I'm going to be using Python and here we need to select our stable version our version of python we're going to be using so right now 3.12 is the most stable version I'm going to go down to python 3.11 just because that's the version of python I'm using and it's going to make it a little bit easier for my setup but if you're using python 12 feel free to change that to 3.12 here it's going to tell us the driver that we need to install and our connection string which is how our fast API application is going to be able to identify the cluster and before we jump ahead let's just go ahead and now open up our fast 8 API application that we want to connect to a nosql database and I already created a package but it's a empty package so we're going to walk through everything step by step so don't worry there but as you can see we have our fast API directory that's the directory I called it and the very first thing I'm going to do is create a virtual environment I'm a huge believer in Virtual environments and keeping each of our applications and their dependencies siled so let's go ahead and just say python3 Dash m v e and V and then I'm going to name the virtual environment just EnV when you click this you can see that right here in our fast API directory we are going to get our EnV file implemented and now we need to activate it so we can say source EnV slash bin slash activate and we can see that our environment has started because we have this little EnV parentheses in front of our directory all right now the very first thing we need to install is fast API so we can say pip install fast API we then want to say pip install uvicorn and then lastly we can go back to our browser and just copy this which is are going to be our python-m it's going to say pip install some type of Pi version We want to make sure that we install the dependency of Pi because that's the dependency to be able to connect to a mongodb cluster so from here let's go back to our visual studio and just paste that in foreign so right here we now have everything installed that we need the very first thing we want to do is just go ahead and create our main.pi file which is our main file for our application which we're going to use to connect to fast API but we're also going to be using it to quickly ping our mongodb application just to make sure that everything's set up and ready to go so let's right click and say new file of main.pi all right perfect now inside here we want to just say from Fast API import fast API this is how we start up any kind of fast API application next we want to say app equals fast API with parentheses at the end all right and now that we have our app equals fast API let's go back to our Atlas and right here we can see our connection string but let's go ahead and just say view full code sample here we can see the entire full code sample that we need to quickly ping our database just to make sure everything's connected and ready to go so I'm going to grab all of this and now let's go back to our Visual Studio code and just kind of paste that in there but right here where we say username change that to the username you created and password change that to the password so I used admin test1234 exclamation mark and then it's going to connect to our Atlas mongodb and now it's going to try and connect okay and now if we go back to a terminal we need to install one more dependency and this is going to be a pip install and then we need to say single quotes Pi and then in Brackets we want to say SRV now I already have that installed but make sure you install that if not you might get a DNS python error so now we can go ahead and say unicorn main colon app dash dash reload and here we can see that we pinged your deployment and we successfully connected to a mongodb database so this just means everything is working exactly how we expected and we're able to connect to our mongodb database so from here what we can do is just right click and we're going to say new folder just to make it a little bit more clean and let's go ahead and call this config and Insider config we want to create a new file of database dot Pi okay and now the very first thing we want to say is client equals and let's go back to our main.pi and let's just grab this entire URL right here and here we just want to say client and we just want to pass this inside I spelled client wrong client and we want to above this go ahead and just say from PI import client okay and now underneath here let's go ahead and say DB equals client dot to do DB and now under this we want to say collection name equals DB to do collection and now we need to do is create a model that we're going to save inside our mongo.db so we can say right click new folder and just say models and now inside our models let's go ahead and say new file and we're going to call this to Do's dot pi and the very first thing we want to say is from pedantic import base model and pedantic is going to be our validator and our database table which is going to be our mongodb collection so we can say from pedantic import base model now we want to create a new to do model so we can say class to do and here we want to pass in base model where we want our 2D to have a name of type string a description of type string and we're going to say complete of type Boolean so now that we have that created let's now jump into a new folder that we're going to call schema now nosql databases send it over to Json but it's going to be hard for our python to be able to use this as an object so we need to be able to deserialize or serialize this nosql information into something that we can use within our application so let's go ahead and just open up our schema.package and inside here let's create a new table of schemas foreign and we want to make sure that this is a python file and the very first one we want is to go to create a individual serializer so we're going to connect our to do object to a dictionary so we can see the IDS and the keys of each item so let's go ahead and just say Dev individual serializer where we're going to be passing in a to do but we're going to be returning a dictionary and now inside here we just want to return the key and value pair for everything so we want to say ID which is going to be a ID that mongodb creates for us and we want to make sure that we wrap this into a string and then we want to pull out the ID and this underscore information that is a mongodb specific way of finding a column a key which is in the mongodb to return the value so after this we want to go ahead and say name because we made name a thing where we can just return the name then we want the description and then lastly we want is it complete or not which was the Boolean value that we created in the models all right and now the next thing we want to do is be able to retrieve all of them so this is just deserializing one item so we need to be able to make sure that when we want to return all of the to Do's we're able to do that as well so let's go ahead and say diff list serial where we're going to pass in to do's however we're going to return a list and here we just want to return a list of our individual serialization where we can pass in our to do for to do in our to-do's we're going to be passing in our two do's and we're going to be running this function which needs it to do for each to do in our to-do's and we're going to deserialize it and return a dictionary with our ID and our value so our key value pair all right right on now the last thing we need to do here is let's create a new folder called routes and inside our routes let's go ahead and just create a new file of route.pi and inside our route.pi that's where we're going to do all the magic to be able to fetch all of our to-do's create to-do's update the to-do's and delete the to-do's so the very first thing we need to say is from Fast API import API router which is going to be our routing interface we want to say from models dot to Do's import our to do we want to import our database we need to say from config .database import our collection name and our collection name if you remember if we go into config.database is this to do collection so we already have this DB connected to our client and then we have to create a collection so this is where we're going to be looking inside our mongodb client we then want to say from our schema dot schemas we want to import our list serial and then lastly we want to say from bson import object ID and that is what mongodb uses to be able to identify the ID that it creates itself alright so as we know we need to say router equals our API router that we implemented and now let's go ahead and create our first get request method so I'm going to say our git request method now inside here we can now create our fetch call from our mongodb and we can do this by saying at router.get and we just want to say we're going to leave it at the API endpoint of just a slash for our application we then want to say async def get to Do's where we don't need to pass in anything and we want to say our to Do's is going to equal our list serial and now we need to do some mongodb magic where we just pass in our collection name and we can say dot find and then return our to-do's so what's going on here is we are using our collection name which again in our database is our collection name of to do collection and then mongodb from PI has this find functionality which just means we are going to find everything in this collection and return it what we know it's going to be to Do's so we're going to pass it through our list serialization which is going to serialize all the data into a dictionary with key and value pairs so if we just open up our terminal right now and we run this application with a yubicorn main colon app dash dash reload oh it says it's already running so I guess it's already been running this whole time so let's just turn off and jump right back in just in case anything was wrong so our application is running you know what we need to connect our application first because we got that ping so what we can do here is just remove all of this information from main.pi file and we can just connect our router to it so we can say app dot include router into here we can just pass in a router and we'll import router from the top so we can say from routes dot route import router now let's restart our application and here we can see if we go to slash docs we're going to get our Swagger with our get DB where if we try it out and execute we're going to get an empty list and that's because we don't have anything saved in our database yet so let's go ahead and create a post request so if we go back to our route right under here we can create our post request so we can say post request method and here I'm going to say out router dot post and I'm also going to leave this just as a empty slash and async Def um post to do where we need to pass in a to-do of type to do and inside here let's just say collection name Dot insert1 and inside here we say dictionary of our to do let's go back to our application right now um refresh the page and inside our post to do I'm going to say try it out and for the name I'm going to say walk the dog let's say because he has energy and this is not complete if I go ahead and say execute we can see that we get a null response Audi but we get a status code of 200. well now if we come back up to our get to do's and we say execute we can see that we now have one to do item in our uh nosql database with an ID walk the dog because he has energy and false and if we go back to our DB Atlas and we say browse collections we can see that in our collections we have now a to do database with a to do collection that has our item inside so this is showing you that we are in fact connecting our fast API application to our mongodb Cloud environment well this is awesome stuff let's um continue going um now that we have a post request let's go ahead and just update this so for the next one we can say um put request method and inside here I want to say router Dot put and here we need to be a little bit creative we need to say slash brackets and pass Center ID this is going to be the ID that we get back from our nosql database and here we can now say async def put to do where we need to pass in a ID that is a string and our to do of type to do and now inside here we can say um collection name dot find one and update where we now want to pass in our underscore ID which is our ID and here we want to say by our object ID where we pass in our ID and this object ID is coming from this bson that we imported earlier we then want to make sure that this is a set so we can say comma another pair of brackets and inside here we want to say money sign set colon dictionary and then pass in our to do so what this is saying is override what's currently in our database for that specific object IDs to do and we're going to find it and update it so if we go ahead and save this application and we go back to our Swagger UI we can say execute to get this which is going to be the ID we can see that complete is false and now if we scroll down to our put after I refresh the screen and we say try it out we can pass that in so we're going to pass this in and now say name is walk the dog description because he has energy and now we're going to make it true if we click execute we can see that we're going to get a 200 and now if we come back up here and we say execute this git we can see that it's now true but we're going to keep the exact same ID as before so we are going to update that to do that is already in our collection on our mongodb foreign now the last thing that we're going to show in this video is our delete functionality so we can come over here and we can say we are now going to create a delete request method and we want to say at router dot delete where we are going to be passing in our ID again and we're going to say async def delete to do where we're going to pass in our ID of type string and here we're just going to call our collection name dot find one and delete where again we are going to pass in our underscore ID and our object ID where we wrap it around in our ID that we set so now if we go ahead and refresh and we say hey I want to get our to Do's go ahead and execute we have one to do I'm going to create just a dummy data in here real quick so now if we come back up here we can see that we have two to do's and let's grab this last one and scroll to the bottom go to our delete functionality Try It Out Pass that in Click execute we can see we get a 200 and if we go back up to our get to Do's we can see that it was deleted and if we go ahead and just grab that as well and we come back to our delete because we've been awesome and we've done all the to-do's on our to-do list we can see that we get a 200 and now we get another empty list because we've deleted everything successfully from our mongodb collection and if we go back to here and we check out our collections again we can see that we still have our to do DB and our to do collection it is just completely empty and we can continue adding items and deleting items updating items all the fun stuff that equals crud which is create read update and delete um so this is essentially how you connect a fast API application to a nosql database specifically mongodb I hope you enjoyed the video If you enjoyed the video please like And subscribe leave a comment on what you want to learn next that helps me be able to create material that will suit you and I'll see you in the next video
Info
Channel: Eric Roby
Views: 24,091
Rating: undefined out of 5
Keywords: fastapi, python, mongodb, mongodb python, fastapi tutorial, fastapi python tutorial, fastapi python, rest api, rest api python, rest api python tutorial, fastapi with mongodb, fastapi with nosql, nosql, nosql database, fastapi nosql tutorial, fastapi mongodb, pymongo, pymongo fastapi
Id: QkGqjPFIGCA
Channel Id: undefined
Length: 23min 56sec (1436 seconds)
Published: Sun May 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.