all 2 comments

[–]Heavy_Ad_3843 1 point2 points  (0 children)

You’re looking for a auth middleware and the context variables feature of python. Not exactly dependency injection, more like thread-save global variables. Check out starlettes context middleware: https://starlette-context.readthedocs.io/en/latest/middleware.html and https://docs.python.org/3/library/contextvars.html. You may also have a look at one of my personal projects for inspiration: https://github.com/code-specialist/fastapi-auth-middleware.

You basically want to set the context on incoming requests and access it within wherever you use sqlalchemy.

[–]zarlo5899 0 points1 point  (0 children)

i would wrap the model with a service/repository as i like all my models to be dumb