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