Full Web Apps with FastAPI Transcripts
Chapter: Storing data with SQLAlchemy
Lecture: Intro to SQLAlchemy chapter
0:00 It is high time that we start working with a proper database. There's been way too many times that I've had to say: "And we'll
0:08 actually create users and save them when we have a database, we'll actually pull real package information, when we have a database."
0:15 So that's what this chapter is about. Were gonna put that database and that data access
0:19 layer together with SQLAlchemy. Now, notice it's not just databases with SQLAlchemy,
0:25 this is "Databases with SQLAlchemy Sync Edition" as in synchronous, not asynchronous. We're gonna talk about SQLAlchemy in two passes. First,
0:35 what I believe to be the familiar and common way of working with SQLAlchemy. And then we're gonna come back and work with a new A P I that they're
0:44 working with in a beta mode, it's not even fully released yet. Why are we doing this? We're doing this because the traditional way of working with
0:52 SQLAlchemy in no way supports asynchronous programming. And one of the massive, massive benefits of FastAPI
1:00 is it's natural and easy use of async and await. If you want your application to scale massively,
1:07 you need to make sure your data access layer allows you to write asynchronous code. So every single time that you're waiting on the database,
1:14 your web server's processing more requests instead of just blocking. So in order to make that happen, we need to use the asynchronous version.
1:21 But that's in beta mode, and I don't think a lot of people have really any experience with this new and not compatible way of writing.
1:29 This is why I pushed the database section further back in the course, because we have this sort of complication of like we're working with two APIs,
1:35 one of them is under beta. It's not a big deal, but I didn't wanna throw all that complexity,
1:41 as it doesn't really have that much to do with FastAPI directly, at you before you've seen most of what we're doing with the web framework.
1:48 So that said, we're gonna in this chapter build out the traditional synchronous version and then we're gonna convert that to the new asynchronous API.
1:56 The reason I'm doing it that way is cause I think most people are familiar with
2:00 this style of SQLAlchemy and the new style of SQLAlchemy will be uncommon, unfamiliar
2:05 with people. So I want to give you a first past where you're like "Oh yeah, I see what's happening,
2:08 I know what's going on" and then we can come to terms with the slightly different async style of working. Let's get to it.