MongoDB for Developers with Python Transcripts
Chapter: Mapping classes to MongoDB with the ODM MongoEngine
Lecture: Concept: Registering connections

Login or purchase this course to watch this video and the rest of the course contents.
0:00 One of the very first things if not the first thing that we need to do is register our connections.
0:06 So it's really straightforward, we just import MongoEngine and then we call register connection,
0:12 and you want to give this connection an alias or set it as the default, and then we're going to set the name equal to the name of the database.
0:19 Here we're calling this one core, I like to be very explicit and say everywhere that you are working with a connection or a database really
0:27 you name that explicitly in your code as we'll see later when we get to the classes. So we register connection, and we set the alias to core,
0:36 and the db we're going to say name = dealership. Now, this worked well if you're just connecting on local host
0:42 no authentication, default port all that, right we just let everything else fall through the cracks. When we get to the production deployment,
0:50 well that's not really going to fly anymore, we're going to need ssl, we're going to need to enable authentication
0:55 and pass credentials and all that kind of stuff, so we can use a more complicated variation here,
1:01 where we do basically the same thing, but we create this dictionary that has the additional elements, now it doesn't have to be a separate dictionary
1:08 you could just set them explicitly, but it turns out that sometimes if you want to like put this into your log or things like this, it's kind of handy,
1:15 so we're going to basically set a username, password, host, port, authentication source is almost always admin, not always,
1:22 it's either the database you're working with if it's a local user or if it's a server wide user you're using to be on admin
1:28 authentication mechanism is scram-shah-1, or you can change it that's the default and ssl is true,
1:34 and in this case, we might be using a self signed certificate which is totally good for the encryption, but it's going to fail
1:41 and say we don't trust the certificate, if we trust the server you can go with ssl cert requires none,
1:48 or if you want to make sure you have one, trust its certificate, omit the last line. And then we just use **data basically to pass those
1:54 as keyword arguments to register connection and notice, each step I'm saying get the user from config or environment
2:01 so this could be in a web app where these values are stored in the config file, you don't want to put them in source specifically
2:08 you don't want them checked in anywhere ever, you could say get them from, you can put them in environment variables
2:13 on your server and then grab them at runtime out of the environment and set them here.


Talk Python's Mastodon Michael Kennedy's Mastodon