Based on this discussion:
piccolo-orm/piccolo#185
In some frameworks, such as Django, views are wrapped with transaction handling logic. If an exception is raised, then the transaction is rolled back.
To use transactions is more explicit currently in Piccolo - a context manager has to be used within the view itself.
@app.get("/")
async def my_endpoint():
try:
async with MyTable._meta.db.transaction():
# a bunch of queries
except Exception:
return JSONResponse({"message": "something went wrong"})
return JSONResponse({"message": "success"})
It would be nice to have a decorator which wraps a view with this logic:
@app.get("/")
@piccolo_transaction
async def my_endpoint():
...