Backend: investigate a simpler solution for SQLAlchemy session management in FastAPI #22
Labels
enhancement
New feature or request
investigation_needed
Expertise needed to clearly define the issue
stale
Milestone
In every API method, we need an SQLAlchemy session to interact with the database.
FastAPI does not like the
@dbsession
decorator we usually use because it makes it consider that we should passargs
andkwargs
query parameter.Currently the backend is hence using dependency injection to inject the SQLAlchemy session. The drawback is that there is no more autocommit, we have to commit manually inside the API methods. And should we forget to commit, the transaction is rolled-back silently because the injected dependency is cleaned-up after the HTTP Response is sent AND it is cleaned-up with a
GeneratorExit
exception, which cause a transaction rollback. More details are provided in FastAPI documentation e.g. here.The other solution mentionned in FastAPI documentation would be to use an ASGI Middleware.
It works, but is not easier / cleaner than the dependency injection approach:
We have to investigate what could be a cleaner solution, maybe my ASGI / decorator experience is too limited or I missed an obvious solution. The solution would benefit other projects like openzim/nautilus-web (and next ones, or Zimfarm once we would have migrated it to FastAPI).
The text was updated successfully, but these errors were encountered: