Skip to content

Inject context data within extensions #1709

Open
@xamgore

Description

@xamgore

I'd like to automatically open a PostgreSQL transaction and inject inside Context. If an operation is query the transaction can be read-only, which is a good thing to declare in advance. mutation operations can be opened with the user_id being logged. Transactions should be automatically closed: COMMIT on success and ROLLBACK when a failure has occurred. Such a strategy could be implemented with an axum middleware for example.

There is no way to implement such behavior with extensions.

  • request(&self, ctx: &ExtensionContext<'_>, next: NextRequest<'_>)
    • can't understand which kind of transaction to open (read-only/mutable)
    • has no means of injecting transactions into the context
    • has access to the Response to figure out whether the operation has failed (commit/rollback)
  • pre_request(&self, ctx: &ExtensionContext<'_>, mut request: Request, next: NextPrepareRequest<'_>)
    • knows which kind of transaction to open (read-only/mutable)
    • can inject the transaction into request.data, thus the context
    • can't close the transaction as it doesn't know the result of the operation (success/failure)

As can be seen in #1707&self and extension contexts are immutable. They are just views over the schema/session/query contexts.

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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy