Skip to content

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

@dantownsend

Description

@dantownsend

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():
    ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions