Building data-driven web apps with Pyramid and SQLAlchemy Transcripts
Chapter: MongoDB edition
Lecture: MongoDB entities

Login or purchase this course to watch this video and the rest of the course contents.
0:00 With our connection all set up and in place
0:02 it's time to start creating our models.
0:04 Remember, over here, we had like a Package and the User.
0:08 The User was pretty simple, we can go with that.
0:10 So, we created a class
0:11 it derived from a certain base class
0:13 we gave it a little meta information
0:15 and we declared the columns, their types
0:17 and their behaviors, whether they're primary keys and so on.
0:20 We'll do exactly the same thing for MongoEngine.
0:23 That looks like it's going a little crazy
0:25 let me just restart PyCharm so it gets its cache
0:28 for auto-complete corrected.
0:32 Ah, better, looks like it now knows about everything.
0:35 Every now and then PyCharm goes a little wonky
0:38 and loses some stuff there. So it's good.
0:42 Now let's go create the equivalent
0:44 of this User class over an our nosql.
0:49 Call it users, we're going to keep exactly the same file names
0:51 that'll help us a little bit in conversions
0:54 and then we're going to import mongoengine
0:56 just like we imported SQLAlchemy.
0:59 We're going to create a class, called User
1:01 and it's going to drive from mongoengine.Document.
1:04 We're don't have tables, we have a records
1:05 we have documents in MongoDB.
1:07 It has an implicit id, which is going to be fine
1:12 for the primary key and it'll auto-generate that for us.
1:15 We don't have to say that like we do over here.
1:18 Put these other things, let's put them across.
1:21 So we want to name
1:22 and SQLAlchemy obviously is not what we want.
1:26 We're going to use mongoengine
1:27 and this time we're going to have a StringField
1:30 and we could say required equals False
1:34 to match this nullable is True
1:37 but unless we say required is True
1:39 it's going to be effectively nullable by default.
1:41 So the top one and these two lines, four and five, same
1:46 and we'll do the same for email.
1:50 Nullable, now notice this index down here
1:53 we don't put the index here, we put it somewhere else
1:54 so I'm going to just put a little comment here.
1:56 Index, nullable, let's just keep going.
1:58 This is real similar, it's a string. Just like that.
2:05 This is a datetime, we know what that looks like.
2:09 We have a DateTimeField, and guess what
2:11 we have default values, just the same.
2:16 So hopefully this feels super comfortable to you
2:18 because you know it's really very, very little difference.
2:22 Again, a StringField, actually we never use this.
2:24 Let's just drop that one and same for this.
2:26 Let's just go with this.
2:28 So final thing to tie these together.
2:32 Over here we controlled stuff
2:34 by saying we want this to be in the users table like this.
2:37 On this version we do it a little bit different.
2:38 We have a meta dictionary in here
2:41 and it's not really obvious
2:42 but we're going to set some keys.
2:43 We're going to say db_alias
2:44 that's the alias we set before
2:46 and it was core, remember, this is the one?
2:48 You can have multiple connections to different databases
2:50 and say these entities go here or there.
2:53 This is how you do it SQLAlchemy
2:54 you would have different base classes
2:56 whereas on this one
2:58 you have different connection aliases.
3:01 Then the table name is now Collection
3:03 'cause we don't have tables we have collections.
3:04 And this is going to be users.
3:06 And then remember the index is up here.
3:08 We had email and hashed_password.
3:13 Want to set that to an array of just the names
3:16 email, hashed_password.
3:18 Maybe we also want one for created date, just to be safe.
3:21 We want to find the newest users or something like that.
3:24 There we go then we can get rid of these bits here.
3:26 Done, that's it!
3:27 Let's see really quickly how we just use this.
3:30 I'm just going to throw this into the most wrong place
3:33 there possibly is, right in the init_db.
3:36 Well let's suppose we wanted to create a new user
3:38 and first of all
3:39 I'm going to connect to this thing called Robo 3T.
3:41 I already have MongoDB running
3:42 and Google how to do that for your OS on how to set that up
3:46 or go check out the other courses.
3:52 So you notice we have this PyPI Demo full thing
3:54 that I already made.
3:55 This one actually has more data.
3:57 We can play with it later
3:58 but notice around here there's no pypi_nosql.
4:02 Database doesn't exist yet, right?
4:04 So, when we begin to talk to it the first time
4:08 MongoEngine will create the database
4:10 and configure the tables with the indexes
4:12 and names, and things like that.
4:13 So let's say that's just really quick.
4:25 So here we've created a new user.
4:27 Now rememeber in SQLAlchemy
4:28 we have what's caled the unit of work pattern.
4:32 MongoEngine is more like Django style
4:34 in that it follows what's called active record.
4:36 In that pattern, we work in individual documents more.
4:39 So we'll juts come over here and go
4:42 and that'll insert it into the database.
4:43 So by starting the website
4:45 it's going to run this code which is going to talk to MongoDB
4:48 create the database, create the tables
4:49 and insert it all in one shot.
4:51 Let's go.
4:55 Great, we registered our development connection
4:58 and it didn't crash so that probably worked.
4:59 Let's see.
5:00 Right, go in refresh.
5:02 There's our nosql.
5:03 Here's our one collection.
5:04 Here's our indexes.
5:05 And let's go find our user.
5:08 There, not super interesting
5:10 nothing embedded or anything like that
5:12 it's just a stardard record but there it is in the database.
5:15 You can see the ID is automatically created.
5:18 The defaulte value is set on the created date
5:19 and then these are the two I entered.
5:21 Super easy to work with, right?
5:23 There's not a whole lot of value
5:24 for you seeing me to do that for each one of these
5:27 so let me just drop in all of these entities
5:31 because they're all the same.
5:32 We'll talk through anything that's interesting, okay?
5:37 Okay, so now you can see we've got them all.
5:39 We've got our mongo_setup we added
5:40 that didn't change.
5:42 Our users, pretty much the same thing.
5:45 Here's our releases, it's all good.
5:48 So you can see these are all pretty standard.
5:50 The one that gets kinda intereseting
5:52 and let's go look at packages real quick.
5:54 So over here this stuff is all sort of columner
5:57 you know, standard real column type stuff
6:00 but this one, check this out.
6:01 This is a list field.
6:02 So this is that embedded relationship of maintainers.
6:06 In SQLAlchemy we have packages, we have users
6:09 and we have another table called maintainers
6:11 that is mapping the many-to-many relationship between them.
6:14 This one line right there avoids that other table
6:18 avoids those joins
6:20 and does that many-to-many mapping perfectly.
6:23 And of course we'll want to have some kind of index on this.
6:27 So maybe maintainers just like that.
6:29 Alright, so let's run this again.
6:32 So it looks like it's working, right?
6:33 Man, now let's see how much we've got going on over here.
6:36 Did this fill up all the tables?
6:37 Now we haven't interacted with the others
6:39 so nothing's happening yet.
6:40 If we click it, not so much.
6:43 None type is not callable.
6:45 You know what that is?
6:47 That is us trying to work
6:48 with our session factory in SQLAlchemy.
6:52 So our next step is going to be going
6:54 let's show you where this is.
6:56 This is in our package service.
6:58 Our next step is going to be to take these data
7:01 these new entities that we've created
7:02 and rewrite our data access layers.
7:04 Maybe that sounds not very fun.
7:06 It turns out it's almost no work.
7:08 It's amazing. So we're going to do that
7:09 and then we'll pretty much have our app running.
7:12 It'll have no data, but it'll be up and running.