db

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package db holds the drel glue of Avero: the transaction middleware, the transaction of a request and the repository of that transaction.

The router imports no database. An application that keeps its own storage therefore imports the router and never compiles drel. An application that uses drel imports this package, and `avero.Stack` takes the middleware that Transaction returns.

See the SDD, S4.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoTx = errors.New(
	"the call needs a transaction: put avero.Transaction in the Tx field of avero.Stack, or wrap the call in engine.WithTx")

ErrNoTx states the fault of a call that runs outside a transaction, and it states the repair. See DX-7.

Functions

func MustTx

func MustTx(ctx context.Context) *drel.Tx

MustTx returns the transaction and panics when none is present. Use it in a handler that a transaction must always surround, so that a wiring fault fails at once.

func Repo

func Repo[T any](ctx context.Context, meta drel.ModelMeta[T]) (*drel.TxRepository[T], error)

Repo returns the repository of the transaction of the request.

The transaction middleware opens the transaction, so a handler always holds one. A job that reads outside a request opens its own with engine.WithTx. A read therefore reads its own writes, and a write stages the change on the transaction. See the SDD, S4.

repo, err := db.Repo(ctx, model.PostMeta)

func Save

func Save(ctx context.Context) error

Save writes the staged changes inside the transaction of the request.

The transaction still commits at the end of the request. The flush only makes a later read of the same request see the write.

func Transaction

func Transaction(e *drel.Engine) router.Middleware

Transaction opens a drel transaction, puts it in the request context, and decides the commit from the response.

It commits on a 2xx or a 3xx response. It rolls back on every other status, on a handler error and on a panic. See the SDD, S4.

The handler returns a Response and writes nothing, so the commit happens before any byte reaches the client. A commit that fails therefore never follows a sent 200. NeedsResponse states that property, so a mounted handler, which writes for itself, runs no transaction.

A nil engine returns the zero middleware, which Stack leaves out. An inspection command passes a nil engine, and the chain of the inspection stays the chain of a run in every other member.

r.Use(avero.Stack{Secret: cfg.Secret, Tx: db.Transaction(engine)}.Middleware()...)

func Tx

func Tx(ctx context.Context) (*drel.Tx, bool)

Tx returns the transaction that Transaction opened, and reports whether one surrounds the request.

tx, ok := db.Tx(c)

Types

This section is empty.

Jump to

Keyboard shortcuts

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