txctxv3

package module
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 txctxv3 is the Fiber v3 adapter for the txcore engine. See the sibling txctx package for Fiber v2.

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.

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 v3 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.Context()).

func OnCommit

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

OnCommit registers fn to run only if the transaction commits successfully.

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 but preserves request values.

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

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

Correct 3-middleware ordering:

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

Example extractor for Fiber v3 (inherits the stdlib context which Fiber v3 populates from the fasthttp RequestCtx before invoking handlers):

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