Latest / Talk Python To Me / #349: Meet Beanie: A MongoDB ODM + Pydantic
Transcript
- 0:00This podcast episode that you're listening to right now was delivered to you in part by MongoDB and Python, powering our web apps and production processes.
- 0:09But if you're using PyMongo, the native driver from MongoDB, to talk to the server, then you might be doing it wrong.
- 0:17Basing your app on a foundation of exchanging raw dictionaries is a castle built on sand.
- 0:23And by the way, see the joke at the end of the show about that.
- 0:26You should be using an ODM, an object document mapper.
- 0:30This time we're talking about Beanie, which is one of the exciting new MongoDB ODMs, which is based on Pydantic and is async native.
- 0:38Join me as I discuss this project with its creator, Roman Wright.
- 0:41This is Talk Python To Me, episode 349, recorded November 18th, 2021.
- 0:47Welcome to Talk Python To Me, a weekly podcast on Python.
- 1:04This is your host, Michael Kennedy.
- 1:05Follow me on Twitter where I'm @mkennedy and keep up with the show and listen to past episodes at talkpython.fm.
- 1:12And follow the show on Twitter via at Talk Python.
- 1:16This episode is brought to you by Sentry and us over at Talk Python Training.
- 1:20Please check out what we're both offering during our segments.
- 1:23It really helps support the show.
- 1:25Hey folks, it's great to have you listening as always.
- 1:28A quick bit of news before we talk with Roman.
- 1:30I've been looking for a way to explore ideas teaching Python, get some feedback, and then bring what I've learned back to the course content.
- 1:36So I'm kicking off a new initiative over on YouTube.
- 1:39It's called Python Shorts.
- 1:41And the goal is to teach you one thing, both interesting and useful, as well as actionable, about Python.
- 1:47I have three videos out so far.
- 1:49The first, parsing data with Pydantic.
- 1:52The second, counting the number of times an item appears in a list with counter.
- 1:56And third, do you even need loops in Python?
- 1:58I also published a fourth video about using the Stream Deck, that little button device that a lot of gamers use, to make Python developers more productive.
- 2:08Check out all four of those videos on my personal YouTube channel, not the Talk Python one.
- 2:12Links in the show notes.
- 2:13Now, let's talk Beanie.
- 2:15Roman, welcome to Talk Python To Me.
- 2:19Hey, I'm happy to be here.
- 2:20Yeah, it's great to have you here.
- 2:22Oh, we get to talk about one of my favorite topics, MongoDB.
- 2:25I'm so excited.
- 2:26Yeah, my favorite topic, obviously too.
- 2:29Oh, yeah, you definitely put a lot of time into it.
- 2:32We're going to talk about your ODM.
- 2:34People often hear about ORMs, Object Relational Mappers, but traditionally, MongoDB and other document databases and NoSQL databases haven't modeled things through relationships.
- 2:45It's more through documents.
- 2:46So the D instead of R, ODM.
- 2:49Beanie, which is going to be super fun.
- 2:52It brings together so many cool topics and even relationships.
- 2:56So maybe if you really wanted, I guess you could put the R back in there as of recently.
- 3:00Anyway, super fun topic on deck for us.
- 3:04Before we get to that, let's just start with your story.
- 3:06How did you get into programming in Python?
- 3:07I started when I was a student, more than 10 years ago now.
- 3:12But I started not with Python.
- 3:14I started, it was 2008, I think, 2007 probably.
- 3:18I started with Flash, and nobody knew that Flash will die soon.
- 3:22Flash was such a big thing when it was new.
- 3:25I remember people were just completely getting amazing consulting jobs and building websites with Flash.
- 3:32I'm like, what is this?
- 3:33I'm not sure I want to learn this, but do I have to learn this?
- 3:35I hope not.
- 3:36But yeah, anyway, yeah.
- 3:37It did kind of get killed by HTML5.
- 3:39And, you know, I think, honestly, maybe it got killed by Steve Jobs, really.
- 3:43At least earlier than it would have been.
- 3:45Definitely.
- 3:45Yeah.
- 3:47So, but that time I didn't know that.
- 3:49And ActionScript, two and three.
- 3:51Right on.
- 3:53And then I decided to move to backend problem.
- 3:56So Flash is a client technology, if anybody don't know.
- 3:59There's no.
- 4:00And then I moved to, I just wanted to build websites and wanted to move to backend.
- 4:06And I chose Django.
- 4:09Mm-hmm.
- 4:09Not Python, but Django.
- 4:10Because Django was super fancy at that time.
- 4:13And now also, but that time, yes.
- 4:14And I was a Django developer, not Python developer, but Django developer for a year, probably.
- 4:18Because I knew a few tricks and tips about Django tools, models, views, and et cetera, templating.
- 4:25And then somehow I learned Python also.
- 4:28And now I'm always a good Python developer.
- 4:30Yeah, fantastic.
- 4:32I think I just realized as you were speaking, we might have to tell people what Flash is.
- 4:38I feel like it's just one of these, you know, iconic things out of the tech industry.
- 4:42But it might be like talking about AltaVista.
- 4:45At some point, the kids, they won't know what Flash is.
- 4:48They won't know there was this battle about getting this thing on people's computer.
- 4:52And it was always like viruses are being found in them, but it could always do this magical stuff.
- 4:57How interesting.
- 4:57Yeah, yeah.
- 4:58How old we are.
- 4:59Exactly.
- 5:01The other thing that is interesting is you said you chose Django.
- 5:05You didn't choose Python.
- 5:08Yeah.
- 5:09Yeah, I think that that's, you know, now people are often choosing Python because of the data science and computational story.
- 5:16But before 2012, there was not this massive influx of data science people.
- 5:21And I think that the big influx was people becoming Django developers and like, well, I guess I'll learn Python.
- 5:28Kind of like people becoming Ruby on Rails developers.
- 5:30Like, I don't know Ruby, but I want to do Ruby on Rails.
- 5:33So I guess I got to learn Ruby.
- 5:34Django is kind of our version of that, right?
- 5:36Right.
- 5:36Yeah.
- 5:37Then I think Flask appeared after a few years and everyone started to do microservices with Flask.
- 5:44And after that, the game started.
- 5:48That's right.
- 5:49That's right.
- 5:50Oh, my goodness.
- 5:50Out in the live stream, we're getting some high fives from Pradvan on Django.
- 5:55And Jeff says, he says Django the right way.
- 5:59Fantastic.
- 5:59So awesome.
- 6:01Yeah.
- 6:01And I think Django has been massively important for Python.
- 6:05And very, very cool.
- 6:07Yeah.
- 6:07How about now?
- 6:07What are you doing day to day?
- 6:08So I'm principal Python developer now.
- 6:11And this is interesting.
- 6:13Actually, I changed my job three months ago.
- 6:17And I didn't look for a new job.
- 6:19But one day, Moot, my current manager, just texted me into Twitter that, hey, I saw your library, Bini.
- 6:28And it's interesting.
- 6:30And we need something like this in our project.
- 6:32And probably you would like to participate in and integrate Bini into our project and develop Bini at the work time.
- 6:39Oh, how cool.
- 6:39And I decided, yeah.
- 6:41Yeah.
- 6:42That's amazing.
- 6:43I mean, on one hand, it would just be cool to have a fun job doing cool MongoDB stuff, right?
- 6:49On the other, it's like, oh, my gosh, I get to use my library and build up my library in a real production environment.
- 6:56Like, that's awesome, right?
- 6:57Yeah, yeah.
- 6:58And so I decided the same day, I think.
- 7:01Let me think about it.
- 7:04Yes, okay.
- 7:04We'll figure out how long it takes me to quit my current job.
- 7:08Awesome.
- 7:09Well, congratulations.
- 7:10That's really cool.
- 7:10Yeah, thank you.
- 7:12That only means good things for Bini, right?
- 7:14It only means more time and energy on it, I would suspect.
- 7:16Yeah, yeah.
- 7:17All this, the current release, the huge release is possible only because I can work a little bit time on work time, not only on weekend.
- 7:26Yeah, absolutely.
- 7:27It's easy to justify, like, the library needs this feature to work right for us.
- 7:32So let me add that feature to the library as just part of the sprint or whatever, right?
- 7:36And also probably it's even more important that when I can work on production with Bini, I can also see what does it mean.
- 7:44Yeah.
- 7:44Which feature and which improvement.
- 7:47It's so true because there's just these little edge cases.
- 7:50They don't show up under even a complicated little example you build for yourself.
- 7:56You know, you've got to put it into production and live with it.
- 8:00You know, some of that stuff might be migrations, right?
- 8:02Like, I never need these migrations.
- 8:03Oh, wait.
- 8:04We have the zero downtime promise.
- 8:07We kind of need migrations now or something, right?
- 8:09Right.
- 8:09Totally correct.
- 8:11Yeah, yeah.
- 8:12Cool.
- 8:12Well, that's great to hear.
- 8:13Congratulations.
- 8:14Now, I wanted to start our chat off not talking about Bini precisely, but like a little lower in the tech stack here.
- 8:22Let's just talk about MongoDB for a little bit.
- 8:27I'm a huge fan of MongoDB, as I'm sure many of the listeners know out there.
- 8:32I've been running Talk Python, Talk Python training those things on top of MongoDB for quite some time.
- 8:39In the very, very early days, some of that was SQLAlchemy stuff, but then I switched over to Mongo and whatnot.
- 8:46So I'm a huge fan of it.
- 8:47I definitely think there's a lot of value.
- 8:49And there's a lot of these architectures where people talk about, oh, we have a Redis middle tier cache because we got to have our website fast.
- 8:57You know what?
- 8:59We get 10 millisecond response time and there's no cache.
- 9:02It's just talking to the database because, you know, everything is structured the right way.
- 9:06I think it's, anyway, I'm kind of going on too much.
- 9:09But what I wanted to start with was I want to hear your thoughts on just sort of why build on top of Mongo.
- 9:15You know, so many people in the Python space build on Postgres, which is fine.
- 9:20It's a good database and all is just a completely different modeling story.
- 9:24So why are you interested in Mongo?
- 9:26First of all, Mongo is super flexible database by design.
- 9:30And I really like to do prototypes.
- 9:35So when I just come up with new idea of new project, home project and et cetera, it's quite simple to work with MongoDB instead of Postgres when you have to change painfully schema of your data.
- 9:49But with Mongo, you can just do what you want to.
- 9:53That's been my experience as well that I remember almost every release of my code would involve some migration on the SQLAlchemy version.
- 10:01I think I've done one, what you would consider a migration in like five years on MongoDB.
- 10:06Whereas everything else is like, I'm going to add this equivalent of a table or I'm going to add a field to this document.
- 10:12But it just goes in and it just, it adapts.
- 10:16It's fantastic.
- 10:17It's like plastic instead of something brittle.
- 10:20Even in the indexes.
- 10:21Yeah.
- 10:21That's why it's great for me, I think.
- 10:23Yeah, it's really easy for prototyping, right?
- 10:24You just, instead of trying to keep the database in sync or whatever, you just work on your models and magic happens.
- 10:31Yeah.
- 10:31Yeah, you're right.
- 10:32And then for sure for production, you have to understand your profile and your needs.
- 10:40And then you can move to Postgres or you can move to something like timestamp database, ClickHouse, for example.
- 10:47Yeah.
- 10:47But in most cases, Mongo is enough.
- 10:49And for some cases, Mongo is the best choice because of flexibility and because of many cool stuff like indexes and such.
- 10:57Yeah, absolutely.
- 10:58I think indexes.
- 11:00I don't know.
- 11:01This is maybe getting ahead of ourselves, but I think indexes are just so underappreciated in databases.
- 11:06I mean, I know a lot of people out there make sure their queries have the right indexes and stuff in it.
- 11:12But there's also so many websites I visit that take three seconds to load a page.
- 11:17I'm like, there's no way there's an index on this query.
- 11:18There's just no way.
- 11:20I don't know what it's doing, but somebody has just not even thought about it.
- 11:24And if it was a weird little, oh, here's like the reporting page.
- 11:27Fine.
- 11:28But it's like the homepage or something.
- 11:29You know what I mean?
- 11:30Like, how are they not making this faster even in like a Postgres story, right?
- 11:36Like, I feel like there's one thing to have a database that does something.
- 11:41There's another to like tune it to do the right thing, regardless of whether it's relational or NoSQL.
- 11:46Yeah.
- 11:47Totally correct.
- 11:47Yeah.
- 11:48You have a lot of experience with databases.
- 11:50I mean, you must have that feeling as well.
- 11:52You go to some website, you're like, what is it doing?
- 11:54Why is this thing spinning?
- 11:55What could it possibly be doing here?
- 11:57Yeah, probably somebody's going, you know, to get data.
- 12:00Exactly.
- 12:02I mean, you're thinking through the ideas.
- 12:04Okay.
- 12:04Is it just not half an index?
- 12:06Or is it an N plus one problem with some ORM?
- 12:09Or where, why am I waiting here?
- 12:11What mistake have they made?
- 12:12Fantastic.
- 12:14All right.
- 12:16So the next thing I want to sort of touch on is this tweet from Scott Stoltzman sent this out yesterday.
- 12:21I don't think he knew that we were coming up with this conversation, actually.
- 12:26So there was this programming humor joke.
- 12:29It says, it has like two, I guess one of them is like a kitchen.
- 12:35The other is an office, but it doesn't really matter.
- 12:38Like the kitchen is super organized and it says MySQL.
- 12:41Everything's little buckets and put away nice.
- 12:44And then there's a desk area that's just, it looks like a hoarder house or like a, you know, earthquake hit and destroyed this area.
- 12:53And it says MongoDB.
- 12:55And it's actually true.
- 12:57It is.
- 12:58It can be true.
- 13:00And the reason I bring this up is Scott said, you know, I know a guy who made this course that saved me from this chaos with MongoEngine.
- 13:07Because it can happen with, it can happen in about 15 seconds without a strong plan.
- 13:11And so out of the box, the way MongoDB, the dev folks there suggest that you, or at least provide, let's put it that way.
- 13:22The tools they provide for you to work with MongoDB are dictionaries.
- 13:27Like you can give us dictionaries and put them somewhere and then you can get dictionaries back.
- 13:32And Python dictionaries are just whatever, right?
- 13:35There's zero structure.
- 13:37There's zero discoverability.
- 13:38There's zero type safety, right?
- 13:40Sometimes it's a string that looks like a number.
- 13:43Other times it's a number.
- 13:44Good luck.
- 13:44Those don't match in a query.
- 13:46You know, like it's horrible.
- 13:47And so for me, I feel like what you need to do when you're working with databases that have less structure in the thing itself, like this is MySQL, say Postgres.
- 14:00Like Postgres says the table looks like this.
- 14:02This column is that size of a string.
- 14:05This is a number and that's it.
- 14:07You know, like the structure is in the database where in these document databases, the structure is in the code.
- 14:15And so you should have some kind of code that helps you not end up in a situation like this, right?
- 14:21Yeah.
- 14:21Honestly, Postgres also came up with JSONB fields now.
- 14:25Yeah.
- 14:26So they're kind of, maybe they fall into that bottom bucket like in a small little area.
- 14:30Yeah.
- 14:30This portion of Talk Python To Me is brought to you by Sentry.
- 14:36How would you like to remove a little stress from your life?
- 14:38Do you worry that users may be encountering errors, slowdowns, or crashes with your app right now?
- 14:44Would you even know it until they sent you that support email?
- 14:47How much better would it be to have the error or performance details immediately sent to you,
- 14:52including the call stack and values of local variables and the active user recorded in the report?
- 14:58With Sentry, this is not only possible, it's simple.
- 15:01In fact, we use Sentry on all the Talk Python web properties.
- 15:05We've actually fixed a bug triggered by a user and had the upgrade ready to roll out as we got the support email.
- 15:11That was a great email to write back.
- 15:13Hey, we already saw your error and have already rolled out the fix.
- 15:17Imagine their surprise.
- 15:18Surprise and delight your users.
- 15:20Create your Sentry account at talkpython.fm/sentry.
- 15:25It's also one of the things.
- 15:49Why schemas about structure?
- 15:53So the thing I think saves you from, you know, Scott mentioned MongoVidgin, which is pretty good.
- 15:59But I think the thing that saves you from this are these ODMs.
- 16:01Like you have a lot more structure in your classes and your Python layer, right?
- 16:07Yeah, yeah, correct.
- 16:08And also, so, yeah.
- 16:10Beanie is ODM based on Pydantic.
- 16:13Pydantic is Python library, the data and stuff.
- 16:17Yeah, let me read the little introduction bit here.
- 16:20Because I think there's so much in this first sentence.
- 16:23So Beanie is an asynchronous Python object document mapper, ODM, for MongoDB.
- 16:30So ODM we talked about, Mongo we talked about, asynchronous, and also I didn't finish the sentence,
- 16:37based on motor, which comes from MongoDB, and Pydantic.
- 16:40So it's also asynchronous, right?
- 16:43Which is pretty awesome, as in async and await.
- 16:46And so often, the models that we build for the databases are their own special thing.
- 16:55And then you've got to build maybe an API, and you might use Pydantic or something like that.
- 16:59But Pydantic's been really coming on strong as a super cool way to build object trees and object graphs and stuff.
- 17:07And so Pydantic is a perfect thing to say, well, let's just use that.
- 17:12Everyone already knows how to use that.
- 17:13And things like APIs can already exchange those on the wire.
- 17:17Yeah, yeah.
- 17:19And that's why I chose this.
- 17:23It's just such a neat combination of bringing the async and await stuff together, along with Pydantic,
- 17:30and saying, let's see if we can use those ideas for the basically for the ODM.
- 17:35So there's other ones.
- 17:36You know, Scott mentioned MongoEngine.
- 17:38That's actually what I'm using right now for my MongoDB stuff in Python.
- 17:41It's MongoEngine's pretty good.
- 17:43I feel like it's kind of wherever it's going to be.
- 17:48There's not a ton of excitement in terms of like new features and pushing stuff forward.
- 17:52For example, there's, to my knowledge, there's no async and await stuff happening in there.
- 17:58There's, I think it's synchronous.
- 18:01Possibly there's something that's happened that's changed up.
- 18:03But the last time I looked, it was synchronous still.
- 18:05Weeks ago, it was synchronous, yeah.
- 18:07Yeah, okay.
- 18:07That's much more recent than I've looked.
- 18:09And what are some of the other ones?
- 18:11I'm trying to think of some of the other MongoDB ODMs out there.
- 18:15I know in another language is probably like Mongoose in JavaScript.
- 18:20Yeah.
- 18:20Active record stuff for Ruby and things like that.
- 18:24Yeah, yeah.
- 18:24So a lot of these systems were based on the Django ORM model.
- 18:29So for example, like MongoEngine is basically Django ORM, but adapted for documents in Mongo, right?
- 18:36Like the terminology and everything is quite similar.
- 18:39Being based on Pydantic, yours is a little bit different, right?
- 18:43I feel like there's a lot of interesting things you've, choices you've made.
- 18:47One, to be based on Pydantic and how that works.
- 18:51But two, we'll get into the API and stuff.
- 18:54But when you look at the API, I feel like you've chosen to be very near MongoDB's native query syntax or query language.
- 19:00So for example, instead of doing a select, you would do like a find or find one or the updates.
- 19:08And like a set operator is one of the things you can do, like a set a value on there.
- 19:13Was that conscious?
- 19:14Did it say, like, I'm going to try to be really close to MongoDB or what was the thinking there?
- 19:18When I started to work on Beanie, it was not Beanie.
- 19:22It was just a side project because, so yeah.
- 19:26On the very beginning, I started to play with FastAPI.
- 19:29It was a very modern web framework at that time.
- 19:33Now it's still modern and great web framework.
- 19:36But at that time, it was quite new around a few years ago.
- 19:39And this is a synchronous also and uses Pydantic under the hood.
- 19:43And there were no ODM, no sync ODM for Python and MongoDB.
- 19:49I had the same experience.
- 19:51Like, I wanted to use MongoDB with some FastAPI stuff.
- 19:54I'm like, there's not a great library that I can pick here.
- 19:57So I guess I'll just use SQLAlchemy or something, right?
- 20:00And what I did, I just started with Pydantic.
- 20:05I've got Motor.
- 20:07It is PyMongo.
- 20:10So yeah, this is an engine over PyMongo.
- 20:13And it is a synchronous engine.
- 20:16Yeah, yeah.
- 20:16Let's talk about Motor a little bit because I suspect that most people who work with MongoDB work with PyMongo.
- 20:24Right?
- 20:25When I opened the conversation, I said, the tools they give you are just, here's a dictionary to put in.
- 20:31And then I get dictionaries back out.
- 20:32I was exactly thinking of the PyMongo library.
- 20:36Right?
- 20:37And so what's this Motor thing?
- 20:39This is also from MongoDB.
- 20:41This is also from MongoDB, right?
- 20:43And it also reflects each method and function from PyMongo.
- 20:48But it's converted, let's say, into a synchronous method and function.
- 20:54So it uses the same, mostly the same syntax as MongoDB itself and as PyMongo also.
- 21:01Okay.
- 21:02Yeah.
- 21:02So it's a lot like PyMongo.
- 21:03It says Motor supports nearly every method PyMongo does, but Motor methods that do network I.O.
- 21:12are coroutines.
- 21:13So async and await type of things, right?
- 21:16Yeah.
- 21:17Yeah.
- 21:17Yeah.
- 21:18Yeah.
- 21:18And what I did, I just combined together Pydantic and Motor without any query builder stuff and other ODM fancy stuff.
- 21:29Just querying, using dictionaries, the same dictionaries as Motor does.
- 21:33And PyMongo for models and nothing more.
- 21:37Small library.
- 21:38It wasn't Bini at that time.
- 21:40It was an internal project just to play with FastAPI.
- 21:43And then after a few months of working, I decided probably I can make it open source.
- 21:50I came up with the name Bini.
- 21:53And mostly that's why I'm following MongoDB naming, not select, not join, et cetera.
- 22:00Just find the many, find one, update, et cetera.
- 22:04Because I'm using, I started to use directly Motor functions inside of Pydantic stuff.
- 22:11And only after that, I just started to update Bini stuff with more fancy stuff.
- 22:18Yeah.
- 22:19So it was a pretty close match to like, how do I take Motor and just make it send and receive Pydantic instead of send and receiving straight dictionaries, right?
- 22:28Yeah.
- 22:28So the first project was just a parser from Motor to Pydantic and back.
- 22:33Cool.
- 22:34Well, I think it's really neat.
- 22:35And I, you know, however you got there, I think it's really nice that you have the API that matches that.
- 22:40Because then I can go to the MongoDB documentation and, or I can find some other example that somebody has on, well, here's how you do it with PyMongo.
- 22:50And you're like, well, that looks really close to the same thing over here.
- 22:54So we can talk about that.
- 22:56Now, I want to dig into some of the other features and stuff there.
- 23:01For example, data and schema migrations and support and whatnot.
- 23:04That's pretty cool.
- 23:05Yeah.
- 23:05But let's talk about modeling data here.
- 23:08That's, you know, the first letter in ODM, the object bit.
- 23:14So what does it look like?
- 23:16I know, well, maybe not everyone listening knows what it looks like to model something with Pydantic.
- 23:21So maybe we could give us a Pydantic example, and then we could talk about how to turn that into something that can be stored in MongoDB.
- 23:27Yeah.
- 23:28Yeah.
- 23:28Pydantic is base model.
- 23:29The main class of Pydantic is base model.
- 23:32And you inherit everything from base model.
- 23:35And it looks like data classes of Python.
- 23:37But it also supports validation and parsing, which data classes does.
- 23:43Yeah.
- 23:44And the conversions and stuff, it's really cool.
- 23:46That's one of the things I think Pydantic is so good about.
- 23:49Yeah.
- 23:49If you look at their example, the way you define these classes is you have a class and you just as a class level, not instance level, you'd say name, colon, or variable, colon, type, variable, colon, type.
- 24:02So in this example, you've got a category, say name, colon, str, description, colon, str.
- 24:08And this just means this thing has two fields.
- 24:11They're both strings.
- 24:12But the Pydantic example on their website has got some kind of model where it's got multiple fields.
- 24:19And one of them is a list of things that are supposed to be numbers.
- 24:22And if you pass it a data and that list happens to have a list of strings that can be parsed to numbers, it'll just convert it straight to numbers as part of loading it.
- 24:33It's really, really nice, right?
- 24:34Yeah.
- 24:34Yeah.
- 24:35This is a great feature.
- 24:36Yeah.
- 24:36This exchange across, especially across either files or API boundaries, right?
- 24:40Somebody writes some code and they send you some data.
- 24:45Well, if they didn't really use the native data type, but it could be turned into it, then, you know, that's really nice.
- 24:51And you also can add your own parser, like your own validation step.
- 24:56And then it will convert this here rule, like from string to number, to date.
- 25:02So, yeah.
- 25:04And the Bini uses the same schema.
- 25:07The same.
- 25:07So, document is a main class of Bini.
- 25:09And it's inherited from Pydantic base model and inherits all the methods, all the aspects of base model of Pydantic.
- 25:19So, it can use the same validation stuff, the same parsing stuff, and et cetera.
- 25:24Right.
- 25:24So, whatever people know and think about Pydantic, that's what the modeling looks like here.
- 25:29I guess the one difference is when you talk about, when you model the top-level documents that are going to be stored and queried in MongoDB, you don't derive from base model, right?
- 25:43Drive from something else?
- 25:44Yeah.
- 25:44From document.
- 25:45Right.
- 25:46And document, it comes from Bini.
- 25:48But that document itself is derived from base model, ultimately, right?
- 25:53So, even though you got to do this little more specialized class, it's still a Pydantic model in its behavior, right?
- 25:59Yeah.
- 25:59Yeah, correct.
- 25:59That's why you can use it as a response model, for example, in FastAPI, if you're familiar with.
- 26:06Yes.
- 26:06Because it uses identity base models and submodels of base models.
- 26:12Yeah, if people haven't seen that, let me, here's an example.
- 26:15One of the things you can do with FastAPI that's super, super neat is you can go and say, as part of returning the values out of the API, one of the things you can do in the decorator is you can just say,
- 26:28the response model is some Pydantic class.
- 26:30And if you just add that one line and that class happens to be a Pydantic model, you get all sorts of live documentation and API definition stuff.
- 26:41And there's even code that will consume that.
- 26:43So, I built this little weather thing in FastAPI for one of my classes over at weather.talkpython.fm.
- 26:51And you just go to slash docs.
- 26:52It pulls up, oh, here's all the APIs you can call.
- 26:57And here's like the return value with, you know, exactly what the schema is.
- 27:01And all of that's just from that one line of code.
- 27:04So, what you're saying is you could do that with these database models.
- 27:07You could just say, I'm going to return this directly from my API back to you.
- 27:11And if you just say response model equals your data entity model, you get all this for free, right?
- 27:17Yeah, it's also invalidated based on this model.
- 27:20If you're, for example, using some external source of data and they changed schema and you received it and responded back.
- 27:28And if the format is not correct, it just will erase an error and you can gracefully handle this.
- 27:34Yeah.
- 27:35Nice.
- 27:35Yeah, the error that it returns is also meaningful.
- 27:38Like, the third entry in this list cannot be parsed to an integer rather than, you know, 400 invalid data.
- 27:45Good luck.
- 27:46Okay, so we've modeled these objects in here.
- 27:51And I guess one more thing to throw in while we're talking about modeling.
- 27:54In your example, right at the bottom of the GitHub page, you have a category which has the name and description.
- 27:59And you have a product that has its own name and description.
- 28:02And then a price.
- 28:04And the product has a category.
- 28:07And here you just say colon category, right?
- 28:10And that would make, I guess, would make this, in this case, an embedded document inside of the product document, right?
- 28:18Yeah, totally correct.
- 28:19It will just create embedded documents.
- 28:21Yeah, so the way that you model.
- 28:23This is why Pydantic's a really good match because Pydantic allows you to compose these Pydantic models in this, like, hierarchy.
- 28:29You can have, like, lists of other Pydantic models within a Pydantic model, which is exactly the same modeling you get with document databases like Mongo, right?
- 28:37Yeah, yeah.
- 28:38So you don't have to do anything.
- 28:39It's like, it just, it keeps modeling the things I wanted to model.
- 28:42Cool.
- 28:42So, in other database systems, like SQLAlchemy or something, you would be able to say, this field is nullable.
- 28:52Or not, or it's required, or something like that.
- 28:56So how do you say that in this model?
- 28:59If it's nullable, yeah, I can just use Python type in optional.
- 29:04It's not a class, but optional, optional.
- 29:08Yeah.
- 29:09Mark it as optional.
- 29:10Like optional bracket str versus just str, right?
- 29:14Yeah.
- 29:14And I guess for a default value, you just set it equal to its default value, right?
- 29:18You're right.
- 29:19But if it's optional, so it's explicitly marked here, but Pydantic allows you to use optional without default value, and default would be none in that case.
- 29:28Because Pydantic is...
- 29:30I'm thinking, like, false or none or something like that.
- 29:34In this case, you probably wouldn't make it optional.
- 29:35You would just give it a default value, right?
- 29:37Yeah.
- 29:37Correct.
- 29:38Now, one of the things that I can do in, say, MongoEngine, is I can say the default is a function.
- 29:44Because maybe this is incredibly common in my world, is I want to know when something was created.
- 29:51You know, when did this user create an account?
- 29:53When was this purchase made?
- 29:54When did this person watch this video or whatever?
- 29:57And so almost all of my models have some kind of created date type of thing.
- 30:01And the default value is datetime.datetime.now without parentheses.
- 30:07You know, like, I want you to call the now function when you do an insert.
- 30:11How would I model that in Beanie?
- 30:13Again, you can use Pydantic stuff here.
- 30:15So I really like Pydantic.
- 30:17They did half a fork.
- 30:19You can use field class, equal field.
- 30:22And inside of that, you can use default factory parameter, where you can just provide function you want to.
- 30:29Okay.
- 30:29Oh, that's right.
- 30:31So you can, instead of setting it, like, you could say it's a string, but its value is, what is it?
- 30:36You say field factory or something?
- 30:38You said it to be one of these things that Pydantic knows about.
- 30:41Didn't get you.
- 30:42Sorry.
- 30:42Is that thing that I'm setting it to equal to?
- 30:44Does that come out of Pydantic?
- 30:46Like, that's not a Beanie thing?
- 30:47This is Pydantic stuff.
- 30:48Yeah.
- 30:49Default factory stuff.
- 30:51But there is interesting Beanie feature about this.
- 30:55I can show you in another code example here.
- 30:57For example, if you want to use not create at, field but updated at.
- 31:03So, and each time when you update, you want to update time.
- 31:07And for that case, default factory will not work.
- 31:10Because default factory...
- 31:11It's already got a value.
- 31:12Yeah.
- 31:12It's only on create.
- 31:13Yeah.
- 31:13Yeah.
- 31:14And Beanie allows you to use event-based actions for this.
- 31:19And you can just create a method of the class.
- 31:22And there, mark it like run before event.
- 31:27Before event decorator.
- 31:28Yeah.
- 31:28Before event.
- 31:30And inside of this, you can just write your logic.
- 31:33Self updated at field equals current time.
- 31:37And it will work.
- 31:38For events, you will choose like insert, replace, and anything else.
- 31:43Yeah.
- 31:43This is one of the new features.
- 31:44We're going to talk about some of the new things.
- 31:46But one of them are these event actions.
- 31:49So, you can say before the insert event or before the save event happens or after the
- 31:56thing's been replaced or any of those types of things, you can sort of put a decorator and
- 32:01say run this function when that happens on this type or in this collection effectively.
- 32:06Right?
- 32:06Yeah.
- 32:06Very, very cool.
- 32:07All right.
- 32:07We're going to dive back into that because that's good.
- 32:10And so, that would be actually a pretty good way, wouldn't it?
- 32:12Just do an event on insert.
- 32:14And when on insert happens, set my created date to be datetime.now.
- 32:19Yeah.
- 32:19Okay.
- 32:19Good.
- 32:20Good.
- 32:20And then, I guess, the other part that's interesting now is doing queries and inserts on this.
- 32:25So, you would create your objects just exactly the same as you would Pydantic, right?
- 32:30Just class name, key value, key value, key value.
- 32:33Like that?
- 32:34Yeah.
- 32:35Or even parse object like category dot parse object and dictionary with values.
- 32:40And yeah.
- 32:41If you want to parse more than one object.
- 32:43Right.
- 32:44If it's coming.
- 32:44Okay.
- 32:44Or if it's coming out of an API or something like that, right?
- 32:47And then you would say, here's where it gets interesting.
- 32:49You say await object dot insert or await class dot find one, right?
- 32:56Or await set some value there.
- 32:58Yeah.
- 32:59Because this is, I think, await library.
- 33:02So, that's why you have to use await here based on the asynchronous nature of the library.
- 33:07Sure.
- 33:07And that's the whole point, right?
- 33:09Is that it's built around that.
- 33:11I think, I mean, there's ways in which you could use it in asynchronous situations, right?
- 33:15You could always create your own event loop and just run the function and just block right that way.
- 33:20Or use something like unsync, which maybe we'll touch on a little bit later.
- 33:23But yeah.
- 33:25So, but if you're doing something like Flask or FastAPI, where the functions themselves,
- 33:32the thing being called by the framework is already handling it, it's basically no work, right?
- 33:38You just make an async method and then you just await things and you get this,
- 33:42you unlock this scalability right there.
- 33:44Yeah.
- 33:45Yeah.
- 33:45I think modern Python world is pretty everywhere asynchronous already.
- 33:50I don't know if I think framework development now, like most of them are asynchronous.
- 33:56Yeah, exactly.
- 33:58I think with Flask, there's limited support for async.
- 34:02And then if you want full async, you have to use court for the moment, but maybe stuff's happening.
- 34:07I know Django is working on an async story as well.
- 34:11Yeah.
- 34:11I think the big holdup for like full on async and Django has actually been the Django ORM.
- 34:15So this would fix that.
- 34:17Although, does it make any sense to use Beanie or another ODM or something like that out with Django?
- 34:24Or I mean, it's so tied into its ORM itself, right?
- 34:28I think for Django, it's a little bit tricky.
- 34:31Yeah.
- 34:31Probably things changed, but Django works with relative databases, yeah, with SQL databases,
- 34:38and also the Django model stuff.
- 34:41Yeah.
- 34:41It's possible.
- 34:45Definitely it's possible.
- 34:46It's Python.
- 34:47Everything is possible.
- 34:48Yeah.
- 34:49It's just, you know, if you're fighting against the system, then you maybe should just choose a different framework rather than try to fight the way that it works, right?
- 34:59Like if you're going to choose something like Django that gives you a lot of opinionated workflow, but a lot of benefit if you stay in that workflow, then just, I'd say, follow that.
- 35:08But the other frameworks are pretty wide open, right?
- 35:11You could easily use this with Flask.
- 35:13You could use this with pretty much anything.
- 35:16It's better if it supports async, right?
- 35:18There's not a synchronous version, is there?
- 35:21Yeah, there is no synchronous version of this, unless it uses a motor insight, as we said.
- 35:26I'm thinking about by Mongo support without motor.
- 35:30In that case, it would be just synchronous.
- 35:33I would think that would be great, honestly.
- 35:35I'm very excited that this is async first.
- 35:38I think that's really good.
- 35:40But let me give you an example.
- 35:41So for my website, I would love to be able to make all the view methods be async, right?
- 35:48That would give it a little bit more scalability.
- 35:50It's pretty quick, like I said, but it would still be way more scalable if it could do more while it's waiting on the server.
- 35:56But at the same time, I have all these little scripts that I run.
- 36:00And here's how I want to go and just show me all of the podcast episodes who is sponsoring them to make sure that if I had to reorder things, I don't mess up some sponsorship detail.
- 36:15Or show me all the people who have signed up for this class this month and whatnot.
- 36:21Those little scripts, right?
- 36:23It would be nice if I could just say these little things are going to be synchronous because it's the easier programming model.
- 36:28I don't have to do that.
- 36:35I don't have to do that.
- 36:36What do you think of that?
- 36:37I do agree totally.
- 36:38But I have limited time.
- 36:39That's why.
- 36:42Yes, of course you do.
- 36:43And I guess that pull requests are accepted or contributions are accepted if there's meaningful good work, right?
- 36:51Yeah, sure.
- 36:52Sure.
- 36:53Yeah, cool.
- 36:53I'm not suggesting that it's like a super shortcoming, right?
- 36:56It's not that hard to create an async method and just call async io.run on it.
- 37:00But having this ability to say this situation is really a synchronous one, don't need to go through the hoops to make that happen.
- 37:09Even I had this situation in the past, you know, and I had to create a loop inside of synchronous function.
- 37:16Yeah, exactly.
- 37:17Exactly.
- 37:18I think that's worth touching on a little bit because people say that async and await is like a virus or something.
- 37:26Like once one part of your code base gets async, like it sort of expands upward so that anything that might touch that function itself has to become async and then its colors have to become async and so on.
- 37:37In the most naive, straightforward way, that's totally true.
- 37:41But it's not true if you don't want it to be, right?
- 37:46Like halfway through that function, that call stack, you could say on this part, I'm going to create an async io event loop and just run it and just block.
- 37:54Right?
- 37:54And anyone who calls that function doesn't have to know it's async io, right?
- 37:57Yeah.
- 37:58You can sort of stop that async propagation.
- 38:01And that sounds like that's what you're talking about, like creating a loop and running it inside of a synchronous function.
- 38:05Yeah.
- 38:06But this looks really super ugly.
- 38:08Yeah.
- 38:10You even don't have any chance to await this.
- 38:14You're creating a task.
- 38:16Yeah.
- 38:17Yeah.
- 38:18It is a little bit weird if you haven't seen it.
- 38:20I, okay.
- 38:21So it's time for my requisites required.
- 38:24Shout out to the unsync library, which I think is just so neat in the way that it simplifies async and await in Python.
- 38:32We're talking about this just a little bit before I hit record, but it has basically two things that are frustrations that make this kind of stuff.
- 38:41We're talking about a little bit harder.
- 38:42Like one, wouldn't it be nice if you could just call an async function and it just runs?
- 38:47Like I want to write this to the log, but let's do that asynchronously and just go, go right to the log.
- 38:52I don't want to see from you again.
- 38:53I want to hear from you again.
- 38:54Go put it in the log.
- 38:56I'm going to keep on working, right?
- 38:57You can't do that with Python's async.
- 38:58You've got to like put it in a loop and make sure the loop is running.
- 39:01So this fire and forget model doesn't work.
- 39:04And the other is you can't block.
- 39:05You can't call dot result to make it block.
- 39:09If it's not done, it's going to throw an exception, right?
- 39:11So this unsync thing lets you put an at unsync decorator on an async method.
- 39:17And then if you need to stop the async propagation, you just call dot result and it'll block and then give you the answer.
- 39:23Builds done.
- 39:24And there's all sorts of cool integration with like threads plus asyncio plus multiprocessing.
- 39:30It's, I think this fixes a lot of those like little weird edge cases.
- 39:34I think I will try this after this podcast and will create a page on documentation.
- 39:40So if you need to try it out and see if it's a good idea.
- 39:43It might not be a good fit, but I think it is actually.
- 39:46I think it would be.
- 39:47I will try.
- 39:47So if it will not work, I will not.
- 39:49So basically the way it works is it creates, when it starts up, it creates a background thread.
- 39:55That its only job is to run an asyncio event loop.
- 39:59And then when you do stuff, when you call functions on this, instead of running the main thread, it just runs on that background thread.
- 40:04And when you block, it just waits for that background thread to finish its work and stuff like that.
- 40:08So it's, that's sort of the trick around it, but super big fan of unsync.
- 40:12I think it's, it does a lot of good for these situations that we're talking about where you're like, okay, almost all the time.
- 40:17And definitely in production, I want it async, but every now and then I want it to just stop.
- 40:22Talk Python To Me is partially supported by our training courses.
- 40:29We have a new course over at Talk Python.
- 40:32HTMX plus Flask.
- 40:33Modern Python web apps hold the JavaScript.
- 40:36HTMX is one of the hottest properties in web development today.
- 40:40And for good reason.
- 40:41You might even remember all the stuff we talked about with Carson Gross back on episode 321.
- 40:46HTMX, along with the libraries and techniques we introduced in our new course, will have you writing the best Python web apps you've ever written.
- 40:53Clean, fast, and interactive, all without that front-end overhead.
- 40:57If you're a Python web developer that has wanted to build more dynamic, interactive apps, but don't want to or can't write a significant portion of your app in rich front-end JavaScript frameworks, you'll absolutely love HTMX.
- 41:10Check it out over at talkpython.fm/HTMX or just click the link in your podcast player show notes.
- 41:18So let's talk about some of the other features.
- 41:20Back to this example here, there's one thing I wanted to highlight that I think was really neat that I saw.
- 41:26So you've got the standard.
- 41:29I've created an object that goes in the database, and I call insert, and I await that.
- 41:34That puts it in the database, right?
- 41:35Yeah.
- 41:36So I don't see a return value here.
- 41:38Does that actually set the ID on this thing that's being inserted after that function call?
- 41:42It updates.
- 41:44Okay.
- 41:44Perfect.
- 41:45Yeah.
- 41:46So .id is good to go after that, right?
- 41:48Yeah.
- 41:48Yeah.
- 41:49Okay, cool.
- 41:49And then the other one, you've got your await find one.
- 41:53The filter syntax is the first thing I wanted to talk about, which I think is really nice.
- 41:57Thank you.
- 41:58So even though these things are, does it have to be one of these indexed ones, or can you do these queries of the style on any of these?
- 42:08Could I do a category or a description equals something or a name equals something?
- 42:12Yeah.
- 42:13Yeah, sure.
- 42:13You can do it with everything.
- 42:14And you can do it even in the same line, in find one, price less than 10, comma, and name equals, I don't know, your name.
- 42:25So it will work.
- 42:27Nice.
- 42:27So the way that you would say, I want to find the product, or all the products, that has a price less than 10, is you just say, in this case, a product is a class with a price field.
- 42:39You just say product.price less than 10, right?
- 42:42Just like you would in an if statement or a while loop or something like that.
- 42:45Yeah.
- 42:45This is really nice because the alternative is something like what you have in MongoEngine, where what you would say is you would say price underscore underscore LLT equals 10.
- 42:58Yeah.
- 42:58Right?
- 42:59You would say, like, separate the operators on the field with double underscores.
- 43:03And so LT means less than, and then you equal the value you want it to be less than.
- 43:07And that is entirely not natural.
- 43:10It's not horrible.
- 43:11You can get used to it, but it sure isn't the same as price less than 10.
- 43:15Right?
- 43:16Yeah.
- 43:17That is really nice.
- 43:17On the very beginning, when I told that I was Django developer, not Python developer, it was about this.
- 43:22Because I knew how to do this stuff about Django, but it's not Python syntax, honestly.
- 43:29It's Django syntax, which moved to.
- 43:32Exactly.
- 43:32So you can do these natural queries.
- 43:35You got, like, less than, greater than, equal, not equal to, and so on.
- 43:38Yeah, yeah.
- 43:39For if it was none, I mean, thing is none is the most natural way, but you would just say equal, equal none.
- 43:45Is that how you would test this?
- 43:46Yeah.
- 43:47You cannot hear.
- 43:48So it's not supported to use is.
- 43:51That's fine.
- 43:51You can use only equal.
- 43:53It's better than underscore, you know, price equals 10 or equals none, just as an assignment.
- 44:00That's even weirder.
- 44:01So that's cool.
- 44:03Then the other thing that I thought was neat is so often in these ORMs, and it is worse in the Mongo story because each record that comes back represents more of the data, right?
- 44:16In this case, you've got a product, and the product has a category, whereas those might be two separate tables in a relational database.
- 44:24So the problem is I get one of these objects back from the database.
- 44:28I make a small change to it, like I want to change the name, and then I call save, and it's going to completely write everything back to the database, right?
- 44:36It's going to override everything, which can be a big problem.
- 44:39There's a couple of solutions you have for that, and one is you have the in-place update operators, like set, and I'm guessing do you have like increment and decrement and add to set and those kinds of things?
- 44:52Yeah, literally everything from which MongoDB supports.
- 44:55Yeah.
- 44:56Right.
- 44:56So in this case, you can say product.set, and then product name is gold bar, right?
- 45:04Rather than what was it before?
- 45:05It was Tony's or something like that, right?
- 45:08Yeah.
- 45:08And that'll do a MongoDB dollar set operation, which is an atomic operation.
- 45:14So somebody else could be updating, say, the category at the same time and sort of transactionally safe.
- 45:20And so this way you're both way more efficient.
- 45:23And it's also safer that you're not possibly overriding other changes.
- 45:27Yeah.
- 45:28And also in current version, it's possible to, so Beanie tracks all the changes of the object.
- 45:37And when you call instead of set, you can call a safe changes.
- 45:42And it will call set inside for all the changes which was happened with this object.
- 45:48Yeah.
- 45:49This was the other way that I was hinting at.
- 45:51And it's super cool.
- 45:52Where is this?
- 45:53Save changes.
- 45:54There we go.
- 45:55On all of these documents, you can optionally have a class, an inner class called settings.
- 46:00And then you can do things like use state management equals true.
- 46:04And you don't have to figure out how to write those set operations or increment operations or whatever.
- 46:10You can just make changes and call save changes and off it goes, right?
- 46:14Yeah.
- 46:14Yeah.
- 46:15So it's quite simple.
- 46:17Yeah.
- 46:17This is really, really cool.
- 46:18I like that about this, that it gives you that option to sort of use the most natural way of making very small changes to the data, right?
- 46:27Because so often ORMs and ODMs are, give me the object back and make a change to it.
- 46:32Put the whole thing back wherever it came from, you know?
- 46:35Yeah.
- 46:35Yeah.
- 46:36I agree.
- 46:36And also, if you don't want to fetch object at all and you want to set something to object, you can use update query here, right?
- 46:44Yeah.
- 46:45And you will not even fetch object into your application.
- 46:49That's a really good point because so often with ORMs and ODMs, the set-based behaviors are super hard to do, right?
- 46:57Like, let's suppose I've got 100,000 users.
- 47:00I want to go and, you know, set some field to like a default value that didn't previously exist in the database.
- 47:06Or I want to compute something that's a computed field that wasn't previously there.
- 47:11So I've got to go to each one, make a change or something.
- 47:14I mean, if it's always the same value, you still would need to go in the ORM stories, like, do a query, get the 100,000 records back, loop over them, set the little one value on the class and call save.
- 47:28But what you're saying is you could just do, like, product, or in my case, users.update, value equals what you want it to, and just update all of them, right?
- 47:38Update many, you might have to call or something like that.
- 47:40Or you can even find something and then .update.
- 47:42And it will update on only...
- 47:45Oh, really?
- 47:46So you could do like a find all, then a .update?
- 47:49Yeah.
- 47:49And it wouldn't actually pull them back from the database?
- 47:51It will not fetch them, yeah.
- 47:52Oh, my goodness.
- 47:53Okay.
- 47:54It's a bit magical.
- 47:54That's very magical.
- 47:56That's awesome, actually.
- 47:57Then, again, the other things you have on here that are really just simple is, like, you can do a find and then just a to list on it.
- 48:04You know, like, I don't want to loop over it or whatever.
- 48:07Just give me the list back.
- 48:08That's also nice.
- 48:09Yeah.
- 48:10Yeah.
- 48:10Let's see.
- 48:10How are we doing on time?
- 48:12We're getting a little short on time.
- 48:14Let me...
- 48:14Let's talk through a little bit of some...
- 48:16There's a really nice tutorial here that starts out with defining documents, setting up the code, which is pretty much just standard MongoDB, right?
- 48:26Like, you have to create one of these clients, but what you're really creating is just a motor client.
- 48:30So I'm guessing you can send as, like, complex of a MongoDB configuration as you need to, and it doesn't affect Beanie.
- 48:36Yeah, and also now it's not documented yet because I'm lazy, but Anthony Shaw, you know him, I think?
- 48:44Anthony Shaw makes common appearances here, yes.
- 48:47Yeah.
- 48:47He suggested me to add optional, so you can pass a connection string instead of all this stuff with database, just init, Beanie, and connection string, and it will work.
- 48:59Nice.
- 48:59But I have to add documentation about this because...
- 49:02Sure, so in the documentation, you create a motor client, and then you pass the client over to Beanie init, or you just create the client first, right?
- 49:11But if you just call init Beanie with the right connection string, it'll do that behind the scenes for you.
- 49:17Yeah, thanks, Anthony.
- 49:18That's really good.
- 49:19So, but if you're working with, like, sharded clusters and replica sets and all the kind of stuff that is, like, on the outer edge of these use cases, that should be supported, right?
- 49:30Yeah.
- 49:30It's just under, behind the scenes, you don't have to know about it.
- 49:33The other thing that's interesting is when you initialize it, you pass it all the document classes, like product or user or whatever, right, as a list?
- 49:43Yeah, yeah, because you have to, so under the hood, document must know to which database it's picked because, so for some use cases, you can use different databases in the same application.
- 49:55And in that case, you have to init Beanie for different databases with different set of models.
- 49:59So, yeah, you have to pass models there, in that case.
- 50:04Yeah, I do that in mine.
- 50:05I have multiple databases, like logging and analytics and all that kind of stuff goes to one database that gets managed and backed up less frequently because it's, like, gigs and gigs of data.
- 50:18But, you know, if you lost it, the only person who would care in the world is me, like, I lost my history of stuff, right?
- 50:24As opposed to the thing the website needs to run or user accounts or whatever.
- 50:29Like, those need to be backed up frequently and treated really specially.
- 50:34So, I actually have those as two separate databases based on classes.
- 50:38So, I guess what you're saying here is you can also call init Beanie on multiple times with different databases and different lists of documents.
- 50:46Yeah.
- 50:46And it will work.
- 50:47Nice.
- 50:48Yeah, that's really cool.
- 50:50Can you give it, like, a star type of thing?
- 50:52Like, everything in this folder, in this module or this sub package?
- 50:58Not yet, unfortunately.
- 51:00But it's a nice feature, I think.
- 51:03Yeah.
- 51:03Sounds like a feature request, yeah?
- 51:05Okay, sounds like a feature request.
- 51:06Yeah, so if you could give it something like, all my models live in this sub package of my project or in this folder.
- 51:13Like, there you go.
- 51:14That might be nice.
- 51:15Because, you know, one of the things that happens to me often is I'll add, like, a view to some part of my site and I'll forget to register it somewhere.
- 51:26Like, why is this a 404?
- 51:28Oh, yeah, yeah, yeah.
- 51:29I got to go and make sure the thing can see this file.
- 51:31It will erase an error, an exception, errorless, and then you will call an endpoint.
- 51:38Nice.
- 51:39If you will try to touch any document without initialization, it will erase an error.
- 51:43Yeah, cool.
- 51:43So, let's see.
- 51:45Let's talk indexes.
- 51:46I started our conversation with my utter disbelief that there are websites that take five seconds to load.
- 51:52And I'm like, I know they don't have more data than I have.
- 51:56I just know they've done something wrong.
- 51:58There's no way this has more data.
- 52:00So, indexes are critical, right?
- 52:03What is the index story?
- 52:04How do you create them over here?
- 52:06It's an interesting story about indexes, honestly.
- 52:08Like, I published my first version of Beanie and one guy texted me that probably it's possible to add indexes there.
- 52:17I don't see if it's supported or not.
- 52:20And in a few days, I added them.
- 52:24Yeah, that's right.
- 52:25We covered Beanie when it first came out on Python Bytes.
- 52:28I'm like, this is awesome, but where are the indexes?
- 52:30A bit of a stickler for those.
- 52:32That's awesome.
- 52:33So, yeah, the way you do it is instead of saying when you define a class, say the type is, say, a str or an int, you would say it's an index of int.
- 52:43And that just creates the index.
- 52:45And it looks like, you know, in Mongo, you have all these parameters and control.
- 52:49Is it a sparse index?
- 52:52Is it a uniqueness constraint as well?
- 52:54Is it ascending?
- 52:55Is it descending?
- 52:56And whatnot.
- 52:57And so, you can pass additional information like that it's a text index or something like that, right?
- 53:02Yeah.
- 53:03Yeah, it supports all the parameters.
- 53:04And uniqueness.
- 53:05This isn't super important, right?
- 53:07Like, your email on your user account had better be unique.
- 53:11Otherwise, a reset password is going to get really weird.
- 53:13You support multi-field indexes, which is something that's pretty common, like a composite index.
- 53:20If I'm going to do a query where the product is in this category and it's on sale, you want to have the index take both of those into account to be super fast, right?
- 53:29So, you have support for that?
- 53:30Yeah, it supports also.
- 53:31But it's not that neat, let's say.
- 53:34It's not that beautiful.
- 53:35But it supports it.
- 53:36Yeah.
- 53:37The payoff is worth it.
- 53:40It's also in this class called collection, right?
- 53:43So, it's kind of in its own special inner class of the model, in which case, you know, a lot of the IDs have a little chevron.
- 53:51You can just collapse that thing and not look at it anymore.
- 53:53So, it's easy to hide the complexity, I guess, there.
- 53:58Yeah.
- 53:58Cool.
- 53:58All right.
- 53:59What else?
- 54:00Aggregation.
- 54:01It sounds like that when we talk about, I'll have to get to it pretty quick.
- 54:05When we talk about relationships and stuff, you said that this is super efficient because it's using the aggregation framework.
- 54:13So, MongoDB has like two ways to query stuff, right?
- 54:16It's like the straight query style.
- 54:18And then it has something that's honestly harder to use but more flexible called aggregations.
- 54:24And so, you guys support, your library supports aggregation queries as well, right?
- 54:29Yeah.
- 54:29Yeah.
- 54:30And also, as before, with updates, it also supports find queries together with aggregations.
- 54:36Like an example, for example, there are some presets of aggregations, average here.
- 54:41And you can use this average with find queries together and you will see the result.
- 54:46And also, for sure, you can pass a list pipeline in MongoDB terms.
- 54:50You can pass pipeline of your aggregation steps there and it will work.
- 54:55Yeah.
- 54:56It's not super easy to write if you haven't done it before, but yes, it will work.
- 55:00Yeah.
- 55:01And also, the thing is, with aggregations, you have to set up what schema of the result
- 55:07would be because with find, everybody knows it would be the same schema of the document,
- 55:11of the original document.
- 55:12But with aggregations, it definitely can be any schema of results.
- 55:16Right.
- 55:17Because the whole point of aggregation and other people might know something similar with
- 55:21MapReduce is I want to take, say, a collection of sales and I want to get a result of show
- 55:28me the sales by country and the total sales for that country.
- 55:32Right.
- 55:32So you're not going to get a list of sales back.
- 55:34You're going to get a thing, a list of things that has a country and a total sales.
- 55:38Right.
- 55:39Yeah.
- 55:39For sure.
- 55:40It's optional and you cannot pass output model.
- 55:44And in that case, it will return dictionaries, but it's not that fancy.
- 55:48So it would be better to.
- 55:50Yeah.
- 55:51This is super cool.
- 55:51I love this projection model idea.
- 55:53Mario in the audience says, I created a model loader.
- 55:56Speaking of the passing the documents to Beanie and init, I created a model loader as a utility
- 56:03function that pulls dot separated pass and then passes it to document models.
- 56:08Works really well.
- 56:09Great.
- 56:10There you go.
- 56:11Right on.
- 56:13So let's talk about relationships.
- 56:16Because I started out talking about, you don't use the R, you use the D because you model documents,
- 56:21not relationships.
- 56:22And yet Beanie supports relationships.
- 56:25I'm super excited about this.
- 56:28Yeah.
- 56:28Tell us about this.
- 56:29Yeah.
- 56:29So it took around three months to come up how to do relations.
- 56:34MongoDB doesn't support relations.
- 56:37But relations is very popular feature in Orium and ODIMs.
- 56:45And I had to implement it finally.
- 56:47And I did it.
- 56:49For now, it's supported limited version of relations, like only top level fields are supported and
- 56:56only two kinds of relations, direct relation and list of relations.
- 57:02Right.
- 57:02A one-to-one or a one-to-many, I guess.
- 57:05One-to-one, one-to-many.
- 57:06Yeah.
- 57:06Uh-huh.
- 57:07And so the syntax is by tonic, I'd say.
- 57:11It uses generic class link inside of bracket, of square brackets, pass your document.
- 57:18Right.
- 57:19So maybe you would specify, normally you would say an optional int.
- 57:23Here you would say like link int.
- 57:25And that might, int doesn't make sense.
- 57:27But, you know, that type thing would be the relationship, right?
- 57:29It's like the same syntax as optional basically here.
- 57:31Yeah.
- 57:32It's a little bit tricky and with a black magic under the hood.
- 57:36But as long as I don't have to know about it.
- 57:39Thanks for creating the black magic.
- 57:40So you could say here your model is there's a door and a house.
- 57:44And then the door is of type link of door.
- 57:47And then you have another one.
- 57:49You have windows where the house has many windows.
- 57:52And you would say the windows is a list of link of window, which is, it's a little bit
- 57:58intense on the nesting there, but it's not bad, right?
- 58:02It's just, it's a list of relationships.
- 58:04Yeah.
- 58:04Yeah.
- 58:05Yeah.
- 58:05And for sure it's possible.
- 58:06And I think later it would be implemented.
- 58:09I will, you know, shorten this list of link to another.
- 58:13Links.
- 58:15Links.
- 58:16No, just kidding.
- 58:17Don't do that.
- 58:17Although it would be kind of awesome.
- 58:20As a syntax, I think it would be less discoverable.
- 58:25Yeah.
- 58:26Yeah.
- 58:26And it works.
- 58:27You can insert data inside of this linked documents to linked collections.
- 58:34And you can fetch data from linked collections.
- 58:35Yeah.
- 58:35And you can even have it cascade things.
- 58:39So for example, you could have created a house object and say dot windows is this list of window
- 58:45objects.
- 58:46And then you would say house dot save.
- 58:47And if you pass the link rule, then that says write the cascade the changes.
- 58:54It'll also go and insert all those windows and associate them.
- 58:57Right?
- 58:58Yeah.
- 58:58Correct.
- 58:59And I didn't use cascade term because it's not SQL database and a little bit different
- 59:06things.
- 59:07Sure.
- 59:07With relations, it would be a little different.
- 59:10Yeah.
- 59:10How does this look in the database itself?
- 59:13So if I go to MongoDB and I pull up the house, what is in its windows?
- 59:19Is that a list of the IDs of the window objects or what is that?
- 59:23In MongoDB, there is a special data type called ref ID.
- 59:28Okay.
- 59:28It's combined under the hood.
- 59:30It's binary and data type, but under the hood, it's a combination of ID of the document,
- 59:36name of the collection and name of the database.
- 59:39So it's a two point.
- 59:40Oh, interesting.
- 59:41Okay.
- 59:42So that's what I, in the document, what I end up with is a list of those things.
- 59:46Yeah.
- 59:47Yeah.
- 59:47Okay.
- 59:48And you will see a few collections like here, house, window and door, three collections
- 59:54with objects, separated objects.
- 59:57Okay.
- 59:58Cool.
- 59:58You can also tell it that you don't want to propagate those changes, right?
- 1:00:02As you save the house, which is interesting.
- 1:00:04Let's talk about refetch.
- 1:00:07So I told you one of the, when I see those websites that are just dragging super slow, I go through
- 1:00:13my thing.
- 1:00:13All right.
- 1:00:14Did they forget the index?
- 1:00:15Are they doing some terrible seven way join?
- 1:00:18Probably without indexes.
- 1:00:20Or the third thing.
- 1:00:21Is it an N plus one ORM problem where they get one thing, but then they've got to go back
- 1:00:25and back and back because they're touching this related field, which you could potentially run
- 1:00:31into that problem as well.
- 1:00:32So you have this prefetch idea, which is kind of like a joined load or something like that,
- 1:00:37right?
- 1:00:37Yeah.
- 1:00:38Yeah.
- 1:00:38It uses lookup aggregation in terms of MongoDB.
- 1:00:42it's not just find query, but aggregation and it avoids this N plus one problem.
- 1:00:48Yeah.
- 1:00:49As you said.
- 1:00:49Right.
- 1:00:50So you just do, and you're fine.
- 1:00:51You just say fetch links equals true.
- 1:00:53And that'll just go get the doors, the windows, everything in your house example, right?
- 1:00:57Yeah.
- 1:00:58And I like the speed of this.
- 1:01:00Like it's much faster than do it, you know, one by one, especially for list of objects.
- 1:01:05Yeah, absolutely.
- 1:01:06So you also have the ability to say fetch all links retroactively.
- 1:01:10If you've been like, oh, I should have, I should have done this join, but I didn't.
- 1:01:14Yeah.
- 1:01:15That might sound silly, right?
- 1:01:16Well, why not just always do the join, right?
- 1:01:18That's one, probably slower than not doing it, I would guess.
- 1:01:22And two, this happens to me all the time.
- 1:01:25Like for example, on the courses website, I want to show, I want to be able to get the
- 1:01:30courses, but the courses have like chapter information and other stuff inside of them.
- 1:01:35And then those have like links effectively over to say like all the details about each chapter,
- 1:01:41like the lectures and videos and all that.
- 1:01:44On say the page that lists the courses, I do not want those things.
- 1:01:49But on the course details page where it shows you like, here's all the stuff in the course
- 1:01:53and how long it is, like I definitely do want those things.
- 1:01:56So in my data access layer, I have a thing that says, should you get all the data or just
- 1:02:01the top level data?
- 1:02:02Basically.
- 1:02:03And this would be exactly the code you'd write.
- 1:02:05Like, well, if you want all the data, you'd say fetch all links on it, right?
- 1:02:09Yeah.
- 1:02:09Yeah, correct.
- 1:02:10This is cool.
- 1:02:11Is there a way to do that on a set?
- 1:02:13Like this is on one record.
- 1:02:15Is there a way to say I got 20 houses back, fetch all of their links?
- 1:02:19Or do I have to do it?
- 1:02:21Is that 20 calls?
- 1:02:22So it would be 20 calls, unfortunately.
- 1:02:23That's okay.
- 1:02:25I think in my example, it's also 20 calls or however many.
- 1:02:29The same setup is exactly the same.
- 1:02:31But I will improve it.
- 1:02:33I hope.
- 1:02:34Yeah.
- 1:02:35This is worth pointing out.
- 1:02:36This is a brand new feature, right?
- 1:02:37You announced this as one of the new features just two days ago.
- 1:02:42On Monday, yeah.
- 1:02:43Yeah.
- 1:02:43And for the people listening, we're recording on Wednesday morning.
- 1:02:47So yeah, this is not, this is like your first pass.
- 1:02:50But I really like how this looks with the relationships and the querying and whatnot.
- 1:02:54It would be nice, I think, if you could have some way to kind of globally configure to save.
- 1:03:00In general, if I call save, the rule to write the relationships is to not, to do nothing or to always write them or something and then only have to override it potentially.
- 1:03:10Yeah.
- 1:03:11Yeah.
- 1:03:11Sounds like default, default rule.
- 1:03:12Yeah, exactly.
- 1:03:13Exactly.
- 1:03:14That'd be pretty cool.
- 1:03:14So let's see a few other things we could talk about.
- 1:03:19We talked a little bit about the event-based actions, but you want to just kind of talk about them directly because it was like a quick, well, how do I add my default value?
- 1:03:28What's the overall picture with these default, with these event actions?
- 1:03:31Yeah.
- 1:03:32A lot of, somehow a lot of people wanted this.
- 1:03:35And I didn't know about this pattern before.
- 1:03:41Like it's implemented something like this, implemented in ActiveRap record pattern of Rails.
- 1:03:47And so finally I was inspired by this.
- 1:03:50I like this word inspired by.
- 1:03:53Not stolen, but inspired by.
- 1:03:56Exactly.
- 1:03:57Yeah.
- 1:03:57And we met this, yeah.
- 1:03:59And now it's supported only four types of events.
- 1:04:04There are events on each insert, replace, save, changes, and validate on save.
- 1:04:09It creates an event and two events before it called and after it called.
- 1:04:16And based on these events, actions already registered to the document would be called also.
- 1:04:23It supports and asynchronous and asynchronous methods for this.
- 1:04:28And you can do a lot of stuff with this.
- 1:04:30Yeah, that's cool.
- 1:04:31Yeah, that's cool.
- 1:04:31You can put the decorator on just an async version or a non-async version and being able to just call it correctly, right?
- 1:04:37Yeah.
- 1:04:38Yeah.
- 1:04:38Yeah, that's cool.
- 1:04:39I'm guessing if you're not doing any awaits, it'd be better if it was not asynchronous, but it doesn't matter that much, right?
- 1:04:46It's one and a half faster if it's not asynchronous.
- 1:04:49Yeah.
- 1:04:50So probably it's critical for something.
- 1:04:52Yeah, if it's doing something where it's waiting on something else, then maybe it should be.
- 1:04:55Yeah.
- 1:04:55Okay.
- 1:04:56Another feature that just came out is cache.
- 1:04:59Yeah, cache.
- 1:05:00Tell us about this.
- 1:05:01It's also an interesting feature.
- 1:05:04Like, so yeah, it's cache.
- 1:05:06I don't know if I have to explain what is cache.
- 1:05:09It's when you save data somewhere locally and use a copy of data for some time.
- 1:05:16Yeah, and what you're talking about is not using MongoDB as a caching backend, but caching the queries that would run through Beanie to not hit the database again if it knows the answer.
- 1:05:27Yeah.
- 1:05:27Yeah, and somehow it's a really important feature even for my projects.
- 1:05:32For example, if you have to validate stuff with user and you already asked for a user in this application with this ID, but you don't know the place where you did it and you don't want to provide this.
- 1:05:45It's an object through the whole pipeline because probably it will not be used in the end of this pipeline.
- 1:05:50But user is already cached in Beanie.
- 1:05:53And if you just will ask in the end of the pipeline again about the same user data, so it would be there.
- 1:06:00And with a bigger field of find queries, it works the same way.
- 1:06:05Yeah, the more complicated the query, the better.
- 1:06:07This makes a lot of sense, I think, for data that doesn't change much.
- 1:06:11Like if you've got a bookstore, you might have categories and books.
- 1:06:14And maybe the books change often.
- 1:06:17The reviews of the books change often.
- 1:06:19The books that are in different categories change.
- 1:06:21But the categories themselves very rarely change, right?
- 1:06:25So that could be something like the category query could just be like, you know what?
- 1:06:28This is cached.
- 1:06:29Yeah, definitely.
- 1:06:30So for now, it supports only local cache like dictionaries of Python.
- 1:06:36But I plan to add another cache backends like Redis and things like this.
- 1:06:41Yeah, that's cool.
- 1:06:42Or even possibly you could put, it might even make sense to use MongoDB itself as a cache.
- 1:06:47Yeah.
- 1:06:48Because it's more native.
- 1:06:49I mean, it'd be weird to kind of store the same thing back into it.
- 1:06:53But at the same time, if you've got a complicated query, what you're storing is like, these are the three things that came back from running that query against possibly hundreds of millions of records.
- 1:07:03Right?
- 1:07:03So in that case, it might make sense to just put it back in Mongo so it's just a straight, you know, table scan read.
- 1:07:09Something like Lambda architecture inside of the single database.
- 1:07:12Yeah.
- 1:07:12So I wanted to ask you, your example says, okay, what we're going to do is say sample.find num greater than 10 to list.
- 1:07:20And then if you call it again with the caching on, then you get the same thing.
- 1:07:25Well, it looks at the actual query, right?
- 1:07:28So if I said num greater than 11, I would get, that would be a separate result and a separate cached thing, right?
- 1:07:34Yeah.
- 1:07:35Yeah, totally.
- 1:07:36Cool.
- 1:07:36The other thing I guess that's worth noting is you can set an expiration date on the cache, right?
- 1:07:41Like I want this to live for 10 minutes or whatever to get the answer back.
- 1:07:46Yeah.
- 1:07:47Yeah.
- 1:07:47Yeah.
- 1:07:47Cool.
- 1:07:48All right.
- 1:07:49Let's talk about revisions.
- 1:07:50And then I'm going to propose one more idea that I think I could build out of revisions and events.
- 1:07:56So what are revisions?
- 1:07:58So it's not my idea again.
- 1:08:01It's neither one user asked me for this.
- 1:08:03And I really like users of Winnie because I have not that many ideas.
- 1:08:10And yeah, what is this?
- 1:08:12You, sometimes you have to protect document inside of the database of changes.
- 1:08:18So yeah, sometimes you have old version of the data in your backend and you do some updates.
- 1:08:24And if you update your document with this old data, you will lose data updated by another
- 1:08:31backend for the same document.
- 1:08:33So let me, let me give people some examples because I think understanding the context is
- 1:08:38really important.
- 1:08:38Like it could be even the same function basically.
- 1:08:41Right.
- 1:08:42So I could have a function that do complicated things.
- 1:08:45I could say, get me my user object for the current user who wants to make some changes.
- 1:08:49I could call, do a bunch of work, call a function with the past, say like the user ID over.
- 1:08:55Maybe that gets the user back, makes some changes, saves it to the database.
- 1:08:59And then I go to the end, I make some more changes, not realizing that to my in-memory version.
- 1:09:04And I call save and it overwrote what that intermediate function might have, whatever was there is gone
- 1:09:09now.
- 1:09:10You know what I mean?
- 1:09:11So you would want to know, is there, is the thing that I got back, if I'm going to replace
- 1:09:18it in the database, is it still the same thing or somebody somewhere behind the scenes changed
- 1:09:24it?
- 1:09:24You know, often I think that people think about very complicated.
- 1:09:27Well, some other process did some other thing, but it could just be some other part of your
- 1:09:32code that you didn't realize called save after a query.
- 1:09:35Yeah.
- 1:09:35Yeah.
- 1:09:36And for this case, I'm using a revision ID, a special token, which
- 1:09:40generates each time when data saves into the database.
- 1:09:45And when you save again, it will check if this ID is the same or it's already updated.
- 1:09:53If it's updated, it will raise an error, like you have all data in memory, but if it's the
- 1:09:58same, it will allow you to.
- 1:09:59Nice.
- 1:09:59That's really cool.
- 1:10:00I think it's a great feature.
- 1:10:02It's good.
- 1:10:02It definitely is.
- 1:10:03Because the alternative of this pattern is to do a blocking transaction.
- 1:10:08Yeah.
- 1:10:08Right.
- 1:10:09And that's also potentially possible.
- 1:10:11I think MongoDB does have transactions now, but I still haven't used them.
- 1:10:14I don't really have a use for that.
- 1:10:16But the alternative model in databases is to say, we're going to do a transaction that blocks
- 1:10:23and anyone else who tries to do a database thing whatsoever, they just wait until we're
- 1:10:27done.
- 1:10:28And that way, there's no chance of them seeing it in this intermediate state.
- 1:10:32A lot of the scalable systems don't end up doing that even in relational databases because
- 1:10:39this blocking model can really kill the concurrency.
- 1:10:41Yeah.
- 1:10:42Right.
- 1:10:42And so they end up doing optimistic concurrency with these types of revisions.
- 1:10:45Anyway, it's just so I think it's a really cool pattern.
- 1:10:49I love it.
- 1:10:50Yeah.
- 1:10:50This is great.
- 1:10:51It's great.
- 1:10:52It's not my idea, but it's great.
- 1:10:54It's really good.
- 1:10:55It's also, I guess, worth discussing set and increment and those types of things.
- 1:11:01So if I say I want to add a category to a product, I could do add to set on that thing
- 1:11:08and just put this thing in its category list.
- 1:11:12Will that also increment the revision?
- 1:11:15So only set, I think, yeah.
- 1:11:18Because if you will use internal methods of MongoDB, it will not understand that it needs
- 1:11:24to update another field.
- 1:11:25Got it.
- 1:11:26Okay.
- 1:11:26Interesting.
- 1:11:27But yeah, that's a good feature.
- 1:11:29I like it.
- 1:11:29This is a big release.
- 1:11:30It's a huge one.
- 1:11:31Cool.
- 1:11:32Well, we've been talking for a long time.
- 1:11:35As you can tell, I'm very excited about it.
- 1:11:37What else?
- 1:11:38What's coming next?
- 1:11:39So next, I have a big plan sales.
- 1:11:42I really like Pydantic.
- 1:11:44I'm a huge fan of Pydantic.
- 1:11:46You can see it.
- 1:11:47But for some cases, Pydantic is a heavy tool.
- 1:11:52And probably, I don't know how I will implement it, but I want to add support of native Python
- 1:11:59data classes here or here or to separate it smaller project like Bini data classes.
- 1:12:06I don't know yet.
- 1:12:07But anyway, I have planned to add native classes without Pydantic.
- 1:12:11Just with not, let's say, fancy parsing stuff without that great validation stuff.
- 1:12:18But somewhere in between, I just want to, you're just getting dictionaries back.
- 1:12:24Good luck.
- 1:12:24And you're getting Pydantic.
- 1:12:26Somewhere in the middle is you're getting classes back, but they don't necessarily, they're not
- 1:12:30as precise as, say, Pydantic.
- 1:12:32Yeah.
- 1:12:33There are cases.
- 1:12:35And people use Bini with these cases also.
- 1:12:39When you have a lot of huge JSONs as a document, and when you parse it on a fetching, it keeps
- 1:12:46a lot of time.
- 1:12:48Yeah.
- 1:12:49So it takes a lot of time just for parsing.
- 1:12:51And then for encoding back to dictionary to store this dictionary.
- 1:12:56Yeah.
- 1:12:57It's not Pydantic fault, definitely, because Pydantic is, again, it's great.
- 1:13:01But I need to avoid this step somehow.
- 1:13:06And probably, probably I will use data classes for this.
- 1:13:11Sure.
- 1:13:11They look very similar.
- 1:13:12Yeah.
- 1:13:12Another scenario where I find that Pydantic is not a good fit is where I might be getting
- 1:13:17bad data, but I don't want it just to be an exception.
- 1:13:20I want to be able to get all the bad data and say, here's the three errors that you made
- 1:13:25passing me this data.
- 1:13:27And I'm not going to accept it, but here's what you gave me.
- 1:13:31If you're doing like form exchange, right?
- 1:13:34Like from an HTML form.
- 1:13:37What you need to do is put the old value back in and say, that value right there, that's wrong.
- 1:13:42But with Pydantic, if you get the value from the form and you try to read it, it's just
- 1:13:49going to know it's wrong.
- 1:13:49And you're like, wait, but I need to give it back to them.
- 1:13:51Just don't run away.
- 1:13:52Come back.
- 1:13:53Where'd you go?
- 1:13:54And so there's situations like that where you need to kind of keep that exchange going,
- 1:13:58but you still want some sort of, you just got to do the validation yourself.
- 1:14:03But anyway, there's certainly times where Pydantic is as cool as it is, it's not the right fit
- 1:14:07for that situation.
- 1:14:08Yeah.
- 1:14:08Yeah.
- 1:14:09I agree.
- 1:14:09Totally.
- 1:14:10Good example.
- 1:14:11Thanks.
- 1:14:12All right.
- 1:14:13Well, this is a very cool project.
- 1:14:16I've, as you hinted at earlier, I've seen it from the beginning, at least when you open
- 1:14:21sourced it and it's really come a long ways.
- 1:14:24It's super compelling.
- 1:14:25It looks like something that I could possibly use on my next project.
- 1:14:30I constantly, as many people out there are, I'm sure as you are, I'm constantly resisting
- 1:14:35the urge to go, you know, I should rewrite that.
- 1:14:37I should rewrite that in FastAPI or I should rewrite that in this.
- 1:14:42I should rewrite my MongoEngine stuff with Beanie, but you know, maybe one day I'll break
- 1:14:47down and do it.
- 1:14:48It'd be fun.
- 1:14:48Yeah.
- 1:14:48Thank you.
- 1:14:49Thank you.
- 1:14:50Yeah.
- 1:14:51Very cool.
- 1:14:52So nice work on this project.
- 1:14:54You're looking for contributors and PR.
- 1:14:56Would you be, PRs, would you be happy to have people make contributions?
- 1:15:00I make a bunch of issues now.
- 1:15:02A lot of people found some, something doesn't fit to different use cases.
- 1:15:09And so, yes, it would be great to have other contributors here.
- 1:15:13I already have 15 contributors.
- 1:15:17Yeah.
- 1:15:18Yeah.
- 1:15:18That's why I saw there's quite a few people in the sidebar there.
- 1:15:22Yeah.
- 1:15:22That's awesome.
- 1:15:25Yeah.
- 1:15:25But I need more, more and more.
- 1:15:27And especially for documentation, because I don't know, I really much better in Python
- 1:15:34than in English.
- 1:15:35And documentation is my weak point.
- 1:15:40Yeah.
- 1:15:40Okay.
- 1:15:41Cool.
- 1:15:41Well, definitely a neat project.
- 1:15:43So thank you for building it.
- 1:15:45Now, before you get out of here, you have to answer the final two questions.
- 1:15:49If you're going to write some Python code, what editor are you using these days?
- 1:15:53What did you use to create Beanie with, for example?
- 1:15:55I'm using Pesharm.
- 1:15:56JetBrains also gave me ProVersion for Beanie as support program, I think.
- 1:16:03Yeah.
- 1:16:03Yeah.
- 1:16:04Fantastic.
- 1:16:05It's really great tool.
- 1:16:07I'm expecting a new one I don't remember the name of, but so they have a new idea.
- 1:16:15I don't remember the name.
- 1:16:16Fleet.
- 1:16:16Yeah.
- 1:16:17Yeah.
- 1:16:17I was about to mention Fleet.
- 1:16:18Fleet is interesting.
- 1:16:20Fleet is like JetBrains' response to VS Code.
- 1:16:23Yeah.
- 1:16:24I'm pretty excited about it.
- 1:16:27You know, I really love PyCharm.
- 1:16:29Nobody's going to be surprised that I say that.
- 1:16:31But if I've got just one file, like I'm going to open that in VS Code because all the JetBrains
- 1:16:38IDE tools, they expect like a project and they're going to create all these things.
- 1:16:42I'm like, I just want to look at the files.
- 1:16:43Just the files, please.
- 1:16:45Like just not too much.
- 1:16:46And this is kind of like that where you can later turn on some of the IDE features, right?
- 1:16:51It looks pretty cool.
- 1:16:52You're going to try it out?
- 1:16:53Not yet.
- 1:16:53I asked for it, but so JetBrains, if you hear me, please.
- 1:16:58Yes, exactly.
- 1:16:59JetBrains.
- 1:16:59I'm already on the early access list too.
- 1:17:01And I have an email.
- 1:17:03Maybe I haven't checked it this morning.
- 1:17:04Maybe it's there.
- 1:17:05Fantastic.
- 1:17:08Yeah.
- 1:17:09All right.
- 1:17:10So PyCharm, maybe Fleet in the future, potentially.
- 1:17:12Cool.
- 1:17:13And then notable PyPI package.
- 1:17:15I mean, we've talked about a bunch of libraries already.
- 1:17:18Yeah.
- 1:17:19So I really like one.
- 1:17:20It's not about Pydanticum or FastAPI stuff, but it's a great package called YARL.
- 1:17:27Y-A-R-L.
- 1:17:30Yeah.
- 1:17:30It's like a pass library, but for URLs.
- 1:17:34And it's great.
- 1:17:36You can combine strings with this URL stuff together and you can parse it.
- 1:17:42You can...
- 1:17:42Oh, that's cool.
- 1:17:43Yeah.
- 1:17:44So you can pass it a URL as a string, just like whatever you'd expect.
- 1:17:47But then you can say, give me the scheme, which is like HTTP, HTTPS, the host, the path,
- 1:17:53the query string.
- 1:17:53Yeah.
- 1:17:55I really like how they use this divide operator.
- 1:17:57You probably see it in the bottom of your page now.
- 1:18:01Like URL, divide full, divide bar.
- 1:18:04Oh, interesting.
- 1:18:04It's like Pathlib style.
- 1:18:07It's like Pathlib, yeah.
- 1:18:08It's like Pathlib for URLs.
- 1:18:10Cool.
- 1:18:11It's super great.
- 1:18:12All right.
- 1:18:12This is totally new to me.
- 1:18:14Awesome.
- 1:18:14Good recommendation.
- 1:18:15Good recommendation.
- 1:18:16Yeah.
- 1:18:16I use it in each project now.
- 1:18:18I don't know how to live without this.
- 1:18:20All right.
- 1:18:21Well, I'm going to check it out for sure.
- 1:18:23One quick follow from the audience here.
- 1:18:26Mario says, thank you for this project.
- 1:18:28I'm about to launch my FastAPI Beanie blog soon.
- 1:18:31Couldn't have done it without it.
- 1:18:32Thank you.
- 1:18:33Yeah.
- 1:18:33And Ollie's on it.
- 1:18:35He says, Pathlibs for URLs.
- 1:18:37Indeed, it's Pathlib for URLs.
- 1:18:39Cool.
- 1:18:40That's a great one.
- 1:18:41All right.
- 1:18:42Final call to action.
- 1:18:43People want to get started with Beanie.
- 1:18:44What do you say?
- 1:18:45I would say, have fun.
- 1:18:47Awesome.
- 1:18:49I would recommend that people go and check out the, if you go to the documentation, there's
- 1:18:55a tutorial that walks you through this pretty well right there on the left.
- 1:18:59It just starts by defining a document and then initialization and so on.
- 1:19:03Yeah.
- 1:19:03Yeah.
- 1:19:03We try to do this as much simple to understand as possible.
- 1:19:08Fantastic.
- 1:19:08All right.
- 1:19:09Roman, thank you for being here.
- 1:19:11Thank you very much for being here.
- 1:19:14This has been another episode of Talk Python To Me.
- 1:19:17Thank you to our sponsors.
- 1:19:19Be sure to check out what they're offering.
- 1:19:20It really helps support the show.
- 1:19:22Take some stress out of your life.
- 1:19:24Get notified immediately about errors and performance issues in your web or mobile applications with
- 1:19:30Sentry.
- 1:19:30Just visit talkpython.fm/sentry and get started for free.
- 1:19:35And be sure to use the promo code talkpython, all one word.
- 1:19:39Want to level up your Python?
- 1:19:40We have one of the largest catalogs of Python video courses over at Talk Python.
- 1:19:44Our content ranges from true beginners to deeply advanced topics like memory and async.
- 1:19:50And best of all, there's not a subscription in sight.
- 1:19:52Check it out for yourself at training.talkpython.fm.
- 1:19:55Be sure to subscribe to the show.
- 1:19:57Open your favorite podcast app and search for Python.
- 1:20:00We should be right at the top.
- 1:20:01You can also find the iTunes feed at /itunes, the Google Play feed at /play,
- 1:20:06and the direct RSS feed at /rss on talkpython.fm.
- 1:20:11We're live streaming most of our recordings these days.
- 1:20:14If you want to be part of the show and have your comments featured on the air,
- 1:20:17be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:20:22This is your host, Michael Kennedy.
- 1:20:24Thanks so much for listening.
- 1:20:25I really appreciate it.
- 1:20:26Now get out there and write some Python code.
- 1:20:28I'll see you next time.