Open
Description
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.