taxonomy-editor
taxonomy-editor copied to clipboard
Use idiomatic FastAPI dependencies to access DB sessions
https://fastapi.tiangolo.com/tutorial/sql-databases/#create-a-dependency
Currently, accessing the neo4J sessions and transactions is quite error-prone (the difference between TransactionCtx and SyncTransactionCtx is not clear, the use/modification of the global variable can be confusing...). Moreover, handling transactions as part of a HTTP middleware seems to be an anti-pattern and error-prone (for example, when we communicate with the parser we have to commit the current transaction and create a new one, ...)
I believe that the idiomatic way to handle db sessions in FastAPI is to create a session dependencies to create a new session for every new HTTP request and pass it through the different functions and controllers called by the path function: it is okay to have a common session for a HTTP request, but transactions, if they are handled, are usually managed by the data access layer (or controllers)
As discuss, my opinion is:
Enclosing, by default, http request in transactions is a fool proof mechanism and we want to keep it (business inconsistencies in a database is a creeping nightmare)
Going to more idiomatic FastAPI is ok, but default should be to have a transaction around an API point unless explicitly stated by the developer.