Documentation
¶
Overview ¶
Example (PkgLevelTxFunctions) ¶
package main import ( "context" "go.llib.dev/frameless/pkg/logger" "go.llib.dev/frameless/pkg/logging" "go.llib.dev/frameless/pkg/txkit" ) func MyActionWhichMutateTheSystemState(ctx context.Context) error { return nil } func RollbackForMyActionWhichMutatedTheSystemState(ctx context.Context) error { return nil } func MyUseCase(ctx context.Context) (returnErr error) { ctx, err := txkit.Begin(ctx) if err != nil { return err } defer txkit.Finish(&returnErr, ctx) if err := MyActionWhichMutateTheSystemState(ctx); err != nil { return err } txkit.OnRollback(ctx, func(ctx context.Context) error { return RollbackForMyActionWhichMutatedTheSystemState(ctx) }) return nil } func main() { ctx := context.Background() ctx, err := txkit.Begin(ctx) if err != nil { logger.Error(ctx, "error with my tx", logging.ErrField(err)) } if err := MyUseCase(ctx); err != nil { txkit.Rollback(ctx) return } txkit.Commit(ctx) }
Index ¶
- Constants
- func Begin(ctx context.Context) (context.Context, error)
- func Commit(ctx context.Context) error
- func Finish(returnError *error, tx context.Context)
- func OnRollback[StepFn onRollbackStepFn](ctx context.Context, step StepFn) error
- func Rollback(ctx context.Context) error
- type Manager
- func (m Manager[DB, TX, Queryable]) BeginTx(ctx context.Context) (context.Context, error)
- func (m Manager[DB, TX, Queryable]) Close() error
- func (m Manager[DB, TX, Queryable]) CommitTx(ctx context.Context) error
- func (m Manager[DB, TX, Queryable]) LookupTx(ctx context.Context) (*TX, bool)
- func (m Manager[DB, TX, Queryable]) Q(ctx context.Context) Queryable
- func (m Manager[DB, TX, Queryable]) RollbackTx(ctx context.Context) error
- type TxRollbackError
Examples ¶
Constants ¶
Variables ¶
This section is empty.
Functions ¶
func OnRollback ¶
Types ¶
type Manager ¶ added in v0.244.0
type Manager[DB, TX, Queryable any] struct { // DB is the underlying Database type to access. // It is ideal if ConnectionAdapter used as the Connection type implementation, but you need access to not exposed functionalities. // // type Connection txkit.Manager[*sql.DB, *sql.Tx] // DB *DB // TxAdapter provides the mapping for a native driver specific TX type to be usable as a Queryable. TxAdapter func(tx *TX) Queryable // DBAdapter provides the mapping for a native driver specific DB type to be usable as a Queryable. DBAdapter func(db *DB) Queryable // Begin is a function that must create a new transaction that is also a connection. Begin func(ctx context.Context, db *DB) (*TX, error) // Commit is a function that must commit a given transaction. Commit func(ctx context.Context, tx *TX) error // Rollback is a function that must rollback a given transaction. Rollback func(ctx context.Context, tx *TX) error // ErrTxDone is the error returned when the transaction is already finished. // ErrTxDone is an optional field. // // default: txkit.ErrTxDone ErrTxDone error // OnClose [optional] is used to implement the io.Closer. // If The ConnectionAdapter needs to close something, // then this function can be used for that. // // default: DB.Close() OnClose func() error }
Manager is generic implementation to handle query interactions which are aware of trasnactions within the context.
Example:
type Connection = txkit.Manager[*sql.DB, *sql.Tx]
Type Arguments: - DB: the main connection that
type TxRollbackError ¶
func (*TxRollbackError) Error ¶
func (err *TxRollbackError) Error() string
func (*TxRollbackError) Unwrap ¶
func (err *TxRollbackError) Unwrap() error
Click to show internal directories.
Click to hide internal directories.