Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a decorator for wrapping views / endpoints in a database transaction #75

Open
dantownsend opened this issue Aug 22, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@dantownsend
Copy link
Member

dantownsend commented Aug 22, 2021

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():
    ...
@dantownsend dantownsend added the enhancement New feature or request label Aug 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant