mfreeborn / fastapi-sqlalchemy

Adds simple SQLAlchemy support to FastAPI
MIT License
622 stars 39 forks source link

query support a la Flask SQLAlchemy #26

Open acidjunk opened 4 years ago

acidjunk commented 4 years ago

Whilst transitioning our stack to FastAPI I luckily found this package. Thanks for the work on this package; it's a nice and easy way to get a DB connection without the need for a dependency that injects it in all controllers.

Our old Flask project uses some functionality from flask-sqlalchemy, that seems absent in fastapi-sqlalchemy: a query property on all models. So instead of db.session.query(User).all() we are using User.query.all()

Are there any plans to support this kind of querying?

acidjunk commented 4 years ago

FWIW: For now I'm using the https://github.com/absent1706/sqlalchemy-mixins which seems to work OK. After including the middleware in main.py I inject the functionality like this:

with db():
    # Enable all sqlalchemy mixin (like query support)
    BaseModel.set_session(db.session)

Whilst the Base model stuff looks like this:

Base = declarative_base()

class BaseModel(Base, SessionMixin):
    __abstract__ = True
    pass
conradogarciaberrotaran commented 3 years ago

@acidjunk Hi, thanks for the input. Does sqlalchemy-mixins work with async? specifically, using https://github.com/encode/databases

acidjunk commented 3 years ago

I don't know: probably not (yet)

Async stuff is alpha level: https://docs.sqlalchemy.org/en/14/orm/extensions/asyncio.html

in the end we wrote our own sqlalchemy bindings; and we didn't use this library as it had to much boilerplate for managing connections. We also needed an easy way to run pytests tests with migrations applied to DB.

https://github.com/workfloworchestrator/orchestrator-core/blob/main/orchestrator/db/database.py

maksad commented 2 years ago

@mfreeborn @conradogarciaberrotaran any updates on this issue? It would be very useful to have.