MongoDB with Async Python Transcripts
Chapter: PyPI Beanie
Lecture: Connecting to MongoDB

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The next thing to do is to actually connect MongoDB as well as those models we created with Beanie. So we did that before in our semi-quickstart here,
0:12 over in P2 quickstart. I'm going to do it again in this section, but let's make it a little bit different.
0:19 We're going to create a folder here called infrastructure. We'll have a couple of things we put in there, and one of them is going to be,
0:30 I'll just call this Mongo setup, like so. And it's gonna have a function that looks like this.
0:37 We need to, as before, import Beanie as well as the motor. Actually, we need to import that. Like this. Now, this doesn't really allow us to connect
0:50 to production databases that would need like a username, a password, server, and so on. But for now, let's just say we're only talking to localhost.
1:02 We may need to upgrade this later when we get to deployment and things like that.
1:08 Notice this thing is going to be just passing over the database, whatever we call it. We're going to call it pypi, which is excellent.
1:16 We also need to pass over those models, the three top-level ones, not the embedded ones, not the releases, not the location,
1:23 but the user, the release analytics, and the package. So let's go and do a little bit of work here. I could just import all three, but let's go and add
1:34 a dunder in it and make this a little sub package thing. And here we can have all models. And this we can just have package.
1:47 This is probably the first place where we're going to need to start thinking about how PyCharm sees this project.
1:55 So right now, if you want me to work with this code, if I'm going to work with it, I need to type import code dot chapter three, chapter eight, etc. And so on.
2:06 We don't want to do that. So we just want to say, basically treat that as its own project. Right? We want to say, you know, we're at this stage. And so in our mind, this is the entire application.
2:19 And the way we do that in PyCharm is we go over here and say mark directory as sources root and that turns it blue, but more importantly that means
2:27 PyCharm sees this as a whole thing. I might also need, if I'm going to make that...
2:31 I need to add just an empty dendrite to make this a top-level module, to make this a lowercase one, and I say from models import 41 package,
2:47 user and release analytics. And then down here we can say all the models are package.package, user.user, release analytics.releaseanalytics.
3:00 You can do your imports however you want, but we need to get these three classes put together in a list somewhere.
3:08 Then once we have that in our Mongo setup, we can now say Import models and then down here instead of doing them all separately, this will be models,
3:24 all models. All right, this way if we have to use this in multiple locations or we want to categorize
3:30 them separately and sort of manage that, it's just all focused on this model section.
3:34 Not absolutely required, but I find it to be maybe adding a little bit of clarity here. Let me clean that up so it doesn't have squigglies.
3:43 So we're going to call it with our database. We're going to specify PyPI when we call this. And it's going to pass off the motors database object
3:51 directly. And then we're going to use the models that we put into this models sub module, the sub package here. Notice you can tell the difference
4:00 by there's a little dot here versus one not here. I'll go ahead and put one over in this infrastructure just as well, just to make it more consistent
4:11 from a package versus sub-module perspective. It looks like it's good, but how do we know? We gotta run it.
4:17 We gotta use our API, we gotta use our classes. So the final thing that we're gonna do here in this chapter is we're gonna turn this
4:26 into a little CLI style way to talk to our PyPI data. We don't yet have a web front end, We don't have a JSON HTTP API that we could call.
4:39 We're going to get to that. But for now, I'm just going to write a CLI, just terminal type of application
4:45 that lets us put all the query concepts in place and basically use what we've created in this chapter.


Talk Python's Mastodon Michael Kennedy's Mastodon