Rock Solid Python with Python Typing Transcripts
Chapter: Frameworks Built on Typing
Lecture: Database Frameworks Built on Pydantic
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
We looked at the web layer. Let's look at the database layer. So I'll give you two examples here that are excellent
0:06
We'll talk about beanie and SQL models. So if you love MongoDB, I love MongoDB so much
0:13
It's such an awesome framework Talk Python Training and the podcast are all powered by Mongo Love it. It's been awesome and
0:21
Beanie is a really cool object document mapper not an ORM because there's not a relational
0:28
Well, there's just documents and so similar acronym but not the same an asynchronous Python ODM for MongoDB based on Pydantic.
0:39
So all the queries, all the modeling of your data you do, you do in Pydantic. It's excellent. Check out an example here.
0:47
Look at this should look super familiar. Here's a category derived from Pydantic dot base model.
0:52
And here's a top level entry into a MongoDB collection called a document.
0:59
And you can say, look, just like you would do with Pydantic, here's something that has
1:02
a name, it has an optional description, it has a index field, which is the price, and
1:09
it has a nested part of its document, as you would in a document database like MongoDB category.
1:16
And then to do a query, we'll skip around, you can just go product.find1 where the price is product.price is less than 10. Amazing.
1:25
It's all async, so everything has an await, but super, super cool. So if you work with MongoDB,
1:33
and you wanna use Type-B and Pydantic and all those things, this is a great one. It's what's powering what you're watching
1:40
the course on right now, actually. If you're like, no way am I doing NoSQL document databases, I'm a Postgres person or some other relational person.
1:48
Well, then you should find your way over to SQL model. Built by Sebastian Ramirez, same guy behind FastAPI and Typer, by the way, which we'll
1:58
talk about next. This is based on top of SQLAlchemy. Basically think of this as SQLAlchemy plus Pydantic and Async.
2:09
Base SQL model is based on Python type annotations and powered by Pydantic and SQLAlchemy. Let's go see an example. So here we go.
2:18
It's a class called hero, derives from SQL model, which is probably also a Pydantic model. And does it map over to a table? Yes, it does.
2:26
And look at this, we have an ID, which is field information, default is none, primary key, all these things, right?
2:34
Name, secret, down here, just create them. And from there, it's pretty much standard SQLAlchemy. So for example, with session from the engine,
2:46
you'll add, add, add, and then commit. It'll do that insert into the database for those new records. Here's your select.
2:54
This is the new SQLAlchemy2 syntax. Honestly, kind of yearn for the simple SQLAlchemy1 ORM style but c'est la vie.
3:04
So select hero, where hero name equals this, and you're gonna go and execute that statement. This one's not asynchronous,
3:13
but you could create an async session and then you would await this, right? you get the option with SQLAlchemy. Anyway, the core takeaway is
3:21
identic and types at the core. And I guess another one that's pretty neat here is this is based on SQLAlchemy,
3:28
which has been around and tested heavily. It's been around forever. So it's super stable. So this is a pretty neat example as well.
3:37
10,000 stars in terms of its popularity as well.