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