Full Web Apps with FastAPI Transcripts
Chapter: async databases with SQLAlchemy
Lecture: Updating the db_session for async

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Here we are in chapter eight
0:01 now. You can see I've copied the code,
0:03 the final code from chapter seven over as per usual,
0:06 now it's the starter code for chapter eight.
0:08 In order for us to work with the new version of SQLAlchemy,
0:12 the first thing we gotta do is actually we've gotta go over here and adjust
0:15 this to say, this is a certain version and funny the way this is coming
0:20 together, we're creating a fake PyPI,
0:22 we're going to need to go to the real PyPI and find SQLAlchemy,
0:25 go to its releases, and here's the beta version that we want.
0:33 And we can just copy that bit right there.
0:37 And so what we wanna put over is this. That'll allow us to use the
0:42 async features that are not in the 1.3 edition.
0:45 Let that reinstall here. All right,
0:48 that looks like that worked. Now the API is different for working with SQLAlchemy.
0:52 We don't create just a session and use the query the same,
0:55 what we do is we create an async session.
0:58 The first thing that we have to make changes to is to our db session
1:01 class because we need to do the async stuff.
1:04 The other difference in the API
1:05 here, in the traditional one,
1:07 we created a factory that creates the session.
1:10 But what we're going to need to actually store and notice if we go down to where we
1:13 put this together, We've got a connection string,
1:16 we create an engine, the engine is passed the factory,
1:19 and then the factory is used later. In the a async model,
1:22 what we do is we have to hang on to the engine and then each time,
1:26 use that to create kind of a context block type thing with the engine.
1:30 So we go over here and we're going to create an __async_engine
1:34 that's just this internal thing, It's gonna be an async engine,
1:38 which we can import from sqlalchemy.ext.asyncio and
1:43 we'll make that Optional of that because we want to set it to be None in
1:47 the beginning, and the way that we create it is pretty similar.
1:52 So here we have this create_engine and we're gonna do something like that.
1:56 But instead of calling this, we're gonna say,
1:58 create_async_engine like so, the parameters that go to it are the same
2:04 but this set here, this async create is what we need.
2:08 And it's telling us that this needs to be registered as a global for us to
2:12 make a change instead of just overriding a local.
2:14 Okay, so that's working. And then this create_session thing is gonna use that
2:18 over here. So we'll say def
2:20 create_async_session. Let's call it that.
2:23 This is gonna return an AsyncSession like so. We'll say we're gonna use
2:31 this global __async_engine here.
2:32 And let's just do a quick test, it was like this where we said,
2:35 you know, you've not set this up before,
2:38 so for some reason, if our __async_engine is not called,
2:42 then we know that they didn't call the right set up,
2:44 and this is gonna be a big problem for us,
2:46 perfect. And then this is pretty straightforward.
2:48 We're gonna create the session, which is an a AsyncSession.
2:52 Instead of just calling the factory,
2:54 we're just going to allocate it and pass the async engine along like this.
2:58 And from here on, it's pretty similar to what we have before.
3:02 Pretty similar, not the same.
3:03 Now notice this expire doesn't, expire_on_commit
3:06 doesn't exist. So this is the async session
3:08 but there's within it a sync session for which we can set the expire_on_commit.
3:13 All right, so that's pretty much the changes that we're going to have to make
3:17 in order to have our async engine stored and then the ability to create these
3:22 async sessions. We're gonna go and
3:24 use this function instead of this one to go through and do these async queries against
3:29 SQLAlchemy. Let's just run it to make sure when it runs this whole startup
3:33 code, everything's hanging together. Perfect,
3:35 it looks like it is. So our foundation to create these sessions
3:38 we need to rewrite the queries is all in place, looks good.