Documentation
¶
Overview ¶
Package txctxv3 is the Fiber v3 adapter for the txcore engine. See the sibling txctx package for Fiber v2.
Index ¶
- func BoolPtr(v bool) *bool
- func DB(c fiber.Ctx) *gorm.DB
- func DBFromCtx(ctx context.Context) *gorm.DB
- func Middleware(db *gorm.DB, cfg Config, extractor ...ContextExtractor) fiber.Handler
- func OnCommit(c fiber.Ctx, fn func(*gorm.DB) error)
- func OnCommitCtx(ctx context.Context, fn func(*gorm.DB) error)
- func OnRollback(c fiber.Ctx, fn func(*gorm.DB) error)
- func OnRollbackCtx(ctx context.Context, fn func(*gorm.DB) error)
- func Outside(c fiber.Ctx) *gorm.DB
- func OutsideCtx(ctx context.Context) *gorm.DB
- type Config
- type ContextExtractor
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Middleware ¶
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 OnCommitCtx ¶
OnCommitCtx is the context.Context variant of OnCommit.
func OnRollback ¶
OnRollback registers fn to run if the transaction rolls back.
func OnRollbackCtx ¶
OnRollbackCtx is the context.Context variant of OnRollback.
Types ¶
type ContextExtractor ¶ added in v0.7.0
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() }