txctx

package
v0.8.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 10, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package txctx is a Fiber v2 + GORM middleware that manages a request-scoped database transaction. See txctxv3 for the Fiber v3 variant.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolPtr

func BoolPtr(v bool) *bool

BoolPtr is a helper for setting Config.LazyTx inline.

func DB

func DB(c *fiber.Ctx) *gorm.DB

DB returns the request-scoped *gorm.DB. With LazyTx the first write call transparently opens BEGIN; reads stay outside any transaction.

func DBFromCtx

func DBFromCtx(ctx context.Context) *gorm.DB

DBFromCtx is the context.Context variant of DB.

func Middleware

func Middleware(db *gorm.DB, cfg Config, extractor ...ContextExtractor) fiber.Handler

Middleware returns a Fiber v2 middleware that manages a request-scoped GORM transaction with timeout-triggered rollback and commit/rollback callbacks. An optional ContextExtractor may be supplied to override the base context (default: c.UserContext()).

func OnCommit

func OnCommit(c *fiber.Ctx, fn func(*gorm.DB) error)

OnCommit registers fn to run only if the transaction commits successfully. Callbacks run in registration order; the first error stops the chain and is reported via Config.OnCallbackError (the commit itself is not undone).

func OnCommitCtx

func OnCommitCtx(ctx context.Context, fn func(*gorm.DB) error)

OnCommitCtx is the context.Context variant of OnCommit.

func OnRollback

func OnRollback(c *fiber.Ctx, fn func(*gorm.DB) error)

OnRollback registers fn to run if the transaction rolls back.

func OnRollbackCtx

func OnRollbackCtx(ctx context.Context, fn func(*gorm.DB) error)

OnRollbackCtx is the context.Context variant of OnRollback.

func Outside

func Outside(c *fiber.Ctx) *gorm.DB

Outside returns a *gorm.DB whose context is decoupled from the request cancellation (so it survives request timeout) but preserves request values for tracing/logging propagation.

func OutsideCtx

func OutsideCtx(ctx context.Context) *gorm.DB

OutsideCtx is the context.Context variant of Outside.

Types

type Config

type Config = txcore.Config

Config re-exports the shared core configuration.

type ContextExtractor added in v0.7.0

type ContextExtractor func(c *fiber.Ctx) context.Context

ContextExtractor is called once per request to obtain the base context for the transaction holder. When provided to Middleware it is used instead of c.UserContext().

When to use it: Elastic APM stores its transaction on the fasthttp RequestCtx (accessible via c.Context()), while Fiber keeps its own stdlib context on c.UserContext(). Without an extractor, GORM callbacks that call apmgorm.WithContext lose the APM transaction. Supply an extractor whenever apmfiber.Middleware is in the chain.

Correct 3-middleware ordering:

app.Use(apmfiber.Middleware())   // 1. captures APM tx on RequestCtx
app.Use(logfiber.Middleware())   // 2. reads APM tx for trace fields
app.Use(txctx.Middleware(db, cfg, extractor)) // 3. inherits APM ctx

Example extractor for Fiber v2 (pulls the fasthttp RequestCtx which carries the APM transaction set by apmfiber.Middleware):

func(c *fiber.Ctx) context.Context { return (*c).UserContext() }

To forward the APM-enriched fasthttp context into GORM, use:

func(c *fiber.Ctx) context.Context { return c.Context() }

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL