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.
0:03 There's been way too many times that I've had to say: "And we'll
0:07 actually create users and save them when we have a database, we'll actually pull real package
0:12 information, when we have a database."
0:14 So that's what this chapter is about. Were gonna put that database and that data access
0:18 layer together with SQLAlchemy. Now, notice it's not just databases with SQLAlchemy,
0:24 this is "Databases with SQLAlchemy Sync Edition" as in synchronous, not asynchronous.
0:30 We're gonna talk about SQLAlchemy in two passes. First,
0:34 what I believe to be the familiar and common way of working with SQLAlchemy.
0:39 And then we're gonna come back and work with a new A P I that they're
0:43 working with in a beta mode,
0:45 it's not even fully released yet.
0:47 Why are we doing this? We're doing this because the traditional way of working with
0:51 SQLAlchemy in no way supports asynchronous programming.
0:56 And one of the massive, massive benefits of FastAPI
0:59 is it's natural and easy use of async and await.
1:03 If you want your application to scale massively,
1:06 you need to make sure your data access layer allows you to write asynchronous code.
1:10 So every single time that you're waiting on the database,
1:13 your web server's processing more requests instead of just blocking.
1:17 So in order to make that happen,
1:18 we need to use the asynchronous version.
1:20 But that's in beta mode, and I don't think a lot of people have really
1:24 any experience with this new and not compatible way of writing.
1:28 This is why I pushed the database section further back in the course, because we have
1:32 this sort of complication of like we're working with two APIs,
1:34 one of them is under beta.
1:36 It's not a big deal, but I didn't wanna throw all that complexity,
1:40 as it doesn't really have that much to do with FastAPI directly, at you before you've
1:45 seen most of what we're doing with the web framework.
1:47 So that said, we're gonna in this chapter build out the traditional synchronous version and
1:52 then we're gonna convert that to the new asynchronous API.
1:55 The reason I'm doing it that way is cause I think most people are familiar with
1:59 this style of SQLAlchemy and the new style of SQLAlchemy will be uncommon, unfamiliar
2:04 with people. So I want to give you a first past where you're like "Oh
2:06 yeah, I see what's happening,
2:07 I know what's going on" and then we can come to terms with the slightly different
2:11 async style of working. Let's get to it.