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