BeanieODM / beanie

Asynchronous Python ODM for MongoDB
http://beanie-odm.dev/
Apache License 2.0
2.02k stars 213 forks source link

Add support for synchronous operations #173

Closed leynier closed 1 year ago

leynier commented 2 years ago

In some contexts, synchronous operations are required but in others, asynchronous operations are possible and more efficient. For example a CLI with Typer and a RestAPI with FastAPI.

It would be nice to be able from the same code base to use both synchronous and asynchronous operations using PyMongo or Motor depending on the case.

These are some repositories that can help and projects where a similar idea has been applied:

whophil commented 2 years ago

I'm interested in this as well. I am coming from umongo which supports synchronous and async, but I only use it synchronously.

I'm interested in beanie because I'd like to try pydantic, but having to run it async is a bit of a burden for my applications.

My synchronous use case might not be an aim of the project, but I just wanted to mention it.

roman-right commented 2 years ago

I thought about PyMongo support, but Beanie uses async generators inside, and this is too tricky to support and sync and async generators depending on the driver. Probably, later I'll refactor it the way when it will support both, but for now, it is not in the todo. Did you try unasync and others? I didn't but did hear that it works well.

guesswh0 commented 1 year ago

It would be great to see this feature! An alternative ODMantic support both sync (pymongo) and async (motor) engines.

mikeckennedy commented 1 year ago

Hey @roman-right With the release of the sync API, can you close this one out?

roman-right commented 1 year ago

Thank you @mikeckennedy, you are right.

roman-right commented 1 year ago

The sync version is available since 1.13.0. Here are the migration instructions: https://beanie-odm.dev/tutorial/sync/migrate-from-async/