Building data-driven web apps with Flask and SQLAlchemy Transcripts
Chapter: MongoDB edition
Lecture: Connecting to MongoDB

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, let's get started writing code
0:01 for our mongodb edition of our app.
0:04 So, over here we've got, in our final
0:07 just as we've had before
0:08 and this time on Chapter 16, final code.
0:12 So, we've got a couple of interesting things to do here.
0:14 The first thing we want to do
0:15 is we want to start out by having a new requirement.
0:18 Now, eventually we'll drop the requirement for SQLAlchemy
0:22 but at the moment, we want to be able to copy
0:24 from SQLAlchemy's database, the one it's managing
0:27 over into MongoDB.
0:29 So, we're going to keep them both around.
0:30 Now, just like before
0:32 we didn't say, let's just start writing raw SQL queries
0:36 against SQLite, or whatever database we want to use.
0:40 We said we're going to use SQLAlchemy model of classes
0:42 and have a higher level programming model
0:44 to talk to our database.
0:46 We'll do the same thing in MongoDB
0:48 and we're going to use this thing called MongoEngine, like so.
0:52 So, we can install that
0:54 I guess maybe the most formal one would be down here
0:57 but, we'll just pip install -r requirements.txt like so
1:02 And we're installing MongoEngine which depends
1:04 on the core library PyMongo to talk to it.
1:08 Okay great, these are all good
1:09 and can tell not misspelled if you want.
1:13 Super. Now, just like before we have this data folder
1:18 and we have a DB session file that we've created.
1:22 If I was starting entirely from scratch I would
1:24 probably just create a folder called data
1:26 and start working there but, because I want to have both
1:28 the SQLAlchemy version and the MongoEngine version
1:31 around for a little while I'm going to
1:32 create a different folder.
1:37 It's called nosql or something like that.
1:39 And in here we're going to create a file called mongo_setup.py
1:43 Something like that.
1:44 Now, getting started I'm going to write the simplest version
1:47 that would actually entirely work for this demo.
1:49 Then I'm going to replace it with what you'll actually need
1:52 in production which is a little more complicated.
1:54 We're just going to paste it in we don't need to talk about it.
1:56 From the function called global_init just like we
1:59 did for SQLAlchemy, we're going to pass in the db_name
2:04 and we're going to default this to pypi.
2:08 Now down here what we have to do is we have to import
2:10 MongoEngine and we just call one simple function.
2:13 So we say MongoEngine and we import that up there
2:16 and we say register_connection.
2:18 Now there's a few things to get passed
2:20 the important one that we're going to work
2:21 with is something called the alias.
2:23 In our database entities we can describe which database
2:27 connection it should use and we have a core one
2:30 an analytics one, a reporting one
2:33 I don' know whatever, backup, you name it.
2:36 So we can put a name here and then this
2:38 is going to be something we can reference in our models.
2:41 The other part that's interesting is we're going to go
2:44 down here and set the name to be equal to the db_name.
2:49 Like so. That's it, this is all we have to do
2:52 for our getting started version.
2:56 Do a little clean up and it should work.
2:58 Now in practice what you need to do is specify
3:01 your server, maybe a port, a username, password
3:05 authentication mechanism, all sorts of messy stuff.
3:08 So I'm going to replace that with a more complicated version
3:11 that has a bunch of defaults that default to the localhost
3:14 version, has the same database name.
3:18 But if we specify username and password it's going to go
3:20 create a dictionary with all these settings down here
3:24 otherwise it's just going to do that little bit
3:26 that you just saw me write.
3:28 So this is all good looks like everything
3:31 is going to be working great here.
3:33 Now, where do we call this.
3:35 Over here in our app we'll recall we
3:37 had this configure DB stuff, like this.
3:40 So this is actually going to get a lot easier.
3:42 All we have to do here is say mongo_setup
3:46 hit import that and say global_init.
3:48 That's it, just take the defaults.
3:50 So all this stuff about getting the file and what not
3:52 we don't need it here.
3:53 In practice maybe we get the username, password, etc. from
3:56 the environment or from a configuration file or something
3:59 like that, right now we don't need to do this.
4:02 Okay so it looks like the connections probably going to work.
4:06 Everything's set up. However when I run it, it's not going to work so super well.
4:10 Let's go over here and run the app.
4:11 Looked all right didn't it, registered the developer
4:14 connection, that's good.
4:15 But we talked to it None type is not callable.
4:18 Well why is that, that's because down here
4:20 in get latest releases we're actually
4:23 trying to work with SQLAlchemy still.
4:26 However what we did was we used some really cool
4:28 design patterns here to separate our
4:30 database code from all the other stuff.
4:32 Notice it didn't crash in the views or in the template
4:36 or anything like that, it crashed just right there
4:38 in these couple of files that are
4:40 responsible for database access.
4:43 What you'll see is that we'll pretty much
4:44 be able to interact with just those files
4:48 and swap out the data access there
4:50 in a super simple and convenient way.
4:53 And then our apps just going to start
4:54 working again it'll be great.