Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: MongoDB edition
Lecture: Rewriting our queries

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now in this section, we get to see the power of our proper factoring of the various aspects of our application. It's totally possible that people go
0:10 and start writing actual queries right in here, right. Right in the view method. We made a point not to do that. We're using the view models
0:18 to make sure the data exchange is clean and inside those view models, like this one here we're going to our services
0:26 our package service for example on calling Get package by ID where we're passing a name along. So, when we go and change the queries
0:33 we need to rewrite them from using SQLAlchemy to using MongoDB you'll see that really, for the most part we could just change only those three files
0:42 little services, that are dedicated to database queries and nothing else at all. It turns out the base view model, it's cookie management
0:52 is exchanging an integer and we're later going to need to change that to a BSON ObjectID but other than that, we won't have to touch anything.
0:59 So let's go and get started. I guess probably the most important one is to go to this package service one. This is the one that was crashing.
1:06 So let's go down here and look at all packages. Remember, the way it works is SQLAlchemy creates a unit of work, a session
1:12 we do some stuff and then we have to close out that session. It turns out it's way simpler in MongoEngine. So let's get rid of all that stuff.
1:21 And we come over here and we still want to execute this as a list but instead of doing a session query we just go to the package thing
1:29 and we say, dot objects, as long as we import the right one at the top so we're going to change data to nosql
1:37 down here now we can say objects(), like this and it's just going to pull it in and go. This is it! That's the entire query, we also have
1:45 a limit in MongoEngine, just like we do on SQLAlchemy. Up here, again, we want to get a package by ID. This looks kind of complicated
1:53 we've got our validation, that's all fine. This stuff, we don't need any of that this stuff, we don't need any of that
2:01 and remember, the session query just goes to package.objects(), so we don't have joins here so that's good. And then when we do this filter
2:13 we could either do it here, we could actually just put it inside of objects. We don't use the type name in double equals
2:18 we just use the field name and single we just assign the value. So literally, that's it. get_package_by_id just got simpler didn't it.
2:28 Let's keep going. Well it's not going to take long at all to finish this up so we do that business, over here
2:33 convert this release to release.objects, like so call count, done, send them away, just keep going.
2:41 Package count, package.objects().count(), like that. All right, get latest releases what was that, in a second? Yeah this is the last one
2:50 so let's go ahead and write this one we can drop that clean this all up change a release here, one more time
3:00 just say dot objects, like so, don't have joins. We do have order_by, do have limit and we do have all however, again remember, we don't say
3:10 when we're doing a order_by, just like with a quality we don't say the type name. This one I don't like as much I prefer SQLAlchemy's
3:17 but what we're going to do is we're just going to go down here and put as a string, either, like this for ascending
3:26 or a minus, here, for descending for the order by. And that's it! We've completely changed all the package queries
3:35 theoretically, I haven't tested this yet but theoretically work under MongoDB. Let's go quick and do user_service One as well.
3:44 That can go, this one becomes a nosql can one come over here, just change this. It's kind of a little bit annoying
3:55 but I want to go through the entire process of changing everything because it's actually just going to take like five minutes
4:02 to rewrite all the queries. So let's just spend those five minutes so you can see what the whole experience is like.
4:08 And objects in the filter, we just change this to email the field equals email the value done. Look at, not only is this easy
4:16 most of the time it's cleaner an simpler love it. Okay, same thing here, we create the user we set their values but in order
4:24 I hope I don't mess them up. But instead of doing this, all of this stuff here we just go and say, user.save()
4:34 remember active record, and we return it. It's going to return with it's primary key set and all that. Okay, all this is looking pretty good here.
4:49 And go login_user now actually down here we have a, find_user_by_email and that's what we're trying to check
4:55 so let's just not do this, let's call this function here and it's going to be smarter. Whoops, we want the email in, that's good.
5:04 And then, last one we need to rewrite this.
5:21 Here we Have it. So I think we've rewritten this one as well. Let's run our site and see how it's working if we're lucky we'll see the home page
5:29 if not, we'll see it crash. TADA! Look at that! And is it working? Well there's no releases, that cause
5:37 we haven't imported any data, there's no projects but check it out, there are two users those are the two users we inserted, so yeah
5:43 it look's like it's actually using our data our queries to go to the database and pull this back. How cool is that?
5:50 Yeah it was a little bit tedious to do this but we completely re-wrote the data access layer from my relational to a non relational document data base
5:59 in a hand full of minutes, it's amazing.


Talk Python's Mastodon Michael Kennedy's Mastodon