feat(tx): run funcspec OnTxBegin and BeforeResponse in transactions

This commit is contained in:
2026-09-30 22:50:41 +02:00
parent 4cbe4f597d
commit 6b6f540ab0
4 changed files with 321 additions and 183 deletions
+9
View File
@@ -32,6 +32,12 @@ const (
// BeforeOp fires immediately before every SQL operation (query, query list, SQL exec).
// It fires at each individual SQL-operation hook point, so it runs once per statement executed.
BeforeOp HookType = "before_op"
// OnTxBegin fires once, first, inside every transaction the handler opens
// (the main transaction and the short one that runs BeforeResponse).
// hookCtx.Tx is the transaction; use it to stamp transaction-local state such
// as RLS settings. An error or abort rolls the transaction back.
OnTxBegin HookType = common.TxHookName
)
// HookContext contains all the data available to a hook
@@ -75,6 +81,9 @@ type HookContext struct {
AbortCode int // HTTP status code if aborted
}
// SetTx points the context at the transaction in use (common.TxContext).
func (c *HookContext) SetTx(tx common.Database) { c.Tx = tx }
// HookFunc is the signature for hook functions
// It receives a HookContext and can modify it or return an error
// If an error is returned, the operation will be aborted