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