RESTful and HTTP APIs in Pyramid Transcripts
Chapter: Adding a database backend with SQLAlchemy
Lecture: Unit of work and the session factory

Login or purchase this course to watch this video and the rest of the course contents.
0:01 So now that we have our connection string figured out let's actually print this out really quick, print connection string,
0:08 and of course, we're going to need to call this function we want to make sure to not forget that, we want to do it once right, at start up
0:14 so you can imagine down here into the under init we go and let's add something here that says config or init db, something like that.
0:24 Now, we could pass this config file here, we're not actually passing anything, but if we did, so over here if we passed in
0:32 say like the base file name something like that, then we could store that file name here, we could just add it
0:39 it's super easy, just like this, like db file, just go ahead and do it, huh so let's go here, db file name, so we can pass that in there
0:50 and we'll just put it let's make the file name the same, db file name
0:55 and we just put this here, now it doesn't go in quotes, we'll just put it like this, okay
0:59 so we can go grab and we make sure this is in production as well, or things are going to go bad later, I'm sure,
1:05 so I am going to put this here, so we can go grab this value in our dunder init,
1:10 so we're given a config, you can say settings= config.get_settings, like this in the settings it's a dictionary, so I can say
1:22 db_file = setting.get (' db_filename '), like this, standard dictionary stuff, and then we'll just call, let me copy the name here,
1:33 DbSessionFactory.global_init, so import this and call global_init and pass the db file, over here it's going to print out what the connection string is
1:47 based on the file, so if I run this, everything should be looking good, connection string is really long, oh my goodness look at that,
1:55 but it's restful auto service db sqlite. And that's looking really good, notice there's no file over here yet
2:02 but when we interact with sqlite, if there's no file it's going to create it, so that's not a problem, so we comment that out
2:08 but that's going to be very, very helpful. Now the next thing we need to do is basically work with connections to the database.
2:14 It doesn't matter what kind of database we're working with we configure it basically the same, right
2:19 so it's going to create a pool of connections and when things get returned to the pool is going to close them, roll back transactions, things like that
2:25 so what we're going to do is we're going to create this thing called an engine so we continue to have access to sqlalchemy up here like so
2:33 and then we're going to create an engine, say sqlalchemy.create engine now there's a couple of things we can pass here,
2:41 first thing we got to pass is the connection string, again, this makes me crazy * args ** kwargs, thanks for the help folks,
2:49 so anyway, we pass the connection string, the other interesting thing is we can say echo,
2:54 here we can say echo=true and now you want this off in production for sure, you probably want it off normally, but if you're new to sqlalchemy
3:01 and you want to see what's going on, this will basically make sqlalchemy log all of its behaviors, all those sql queries that sends off to the database
3:08 things like that will spit to the console or the terminal; so this is kind of helpful, we'll go ahead and keep that there.
3:14 Now finally, you would think what we need to do is store this engine somewhere so we can use it for stuff, but not really,
3:21 the idea is we're going to work with these things called sessions and the session needs the engine to do its work, so what we're going to really store
3:28 is we're just going to store this thing called a session factory and once we have the session factory and we associate the engine to it
3:35 it will just hold a reference to it, so we'll be golden. So let's go over here and tell this thing that it has a session factory,
3:43 that is nothing for the starter here, and we'll go ahead and allocate this we'll need it in just a little bit when we want to do anything interesting
3:50 so we're going to go over here and we're going to need another piece of sqlalchemy, we're going to need the ORM layer,
3:56 because this session unit of work business is only an ORM thing, so I'll come over here and we'll have a thing called a session maker
4:03 now the session maker is going to need things like to know how to talk to the database as part of its session in life cycle
4:11 so we're going to give it this engine here, right, so this we're going to hold and let's make this a little simpler,
4:17 make this a class method like so, we'll come down here and say cls.session_factory is this, okay so this we're going to need later
4:25 and because we're holding it here it's implicitly holding on to the engine which had a connection, so everything will be all wired together
4:32 and all we have to really keep track of is this session factory, let's go and just do the one thing that we're going to have to do while we're here
4:38 let's define add another class method, we will say something like create session, and we'll just go to the class, to our session factory,
4:49 and we'll call it and return that value. Okay, so the way sqlalchemy works is we want one of these for basically one of those per connection string
4:58 but every time we create a session, we're going to have to allocate a new one because that more or less represents a transactional set
5:05 of processing, a transaction in the database.


Talk Python's Mastodon Michael Kennedy's Mastodon