session

package
v5.0.0-rc9 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DB

func DB(ctx context.Context, fallback *gorm.DB) *gorm.DB

DB returns the Gorm instance stored in the given context. Returns the given fallback if no Gorm DB could be found in the context.

Types

type Gorm

type Gorm struct {
	TxOptions *sql.TxOptions
	// contains filtered or unexported fields
}

Gorm session implementation.

func GORM

func GORM(db *gorm.DB, opt *sql.TxOptions) Gorm

GORM create a new root session for Gorm. The transaction options are optional.

func (Gorm) Begin

func (s Gorm) Begin(ctx context.Context) (Session, error)

Begin returns a new session with the given context and a started DB transaction. The returned session has manual controls. Make sure a call to `Rollback()` or `Commit()` is executed before the session is expired (eligible for garbage collection). The Gorm DB associated with this session is injected as a value into the new session's context. If a Gorm DB is found in the given context, it will be used instead of this Session's DB, allowing for nested transactions.

func (Gorm) Commit

func (s Gorm) Commit() error

Commit the changes in the transaction. This action is final.

func (Gorm) Context

func (s Gorm) Context() context.Context

Context returns the session's context. If it's the root session, `context.Background()` is returned. If it's a child session started with `Begin()`, then the context will contain the associated Gorm DB and can be used in combination with `session.DB()`.

func (Gorm) Rollback

func (s Gorm) Rollback() error

Rollback the changes in the transaction. This action is final.

func (Gorm) Transaction

func (s Gorm) Transaction(ctx context.Context, f func(context.Context) error) error

Transaction executes a transaction. If the given function returns an error, the transaction is rolled back. Otherwise it is automatically committed before `Transaction()` returns.

The Gorm DB associated with this session is injected into the context as a value so `session.DB()` can be used to retrieve it.

type Session

type Session interface {
	// Begin returns a new session with the given context and a started transaction.
	// Using the returned session should have no side-effect on the parent session.
	// The underlying transaction mechanism is injected as a value into the new session's context.
	Begin(ctx context.Context) (Session, error)

	// Transaction executes a transaction. If the given function returns an error, the transaction
	// is rolled back. Otherwise it is automatically committed before `Transaction()` returns.
	// The underlying transaction mechanism is injected into the context as a value.
	Transaction(ctx context.Context, f func(context.Context) error) error

	// Rollback the changes in the transaction. This action is final.
	Rollback() error

	// Commit the changes in the transaction. This action is final.
	Commit() error

	// Context returns the session's context. If it's the root session, `context.Background()` is returned.
	// If it's a child session started with `Begin()`, then the context will contain the associated
	// transaction mechanism as a value.
	Context() context.Context
}

Session aims at facilitating business transactions while abstracting the underlying mechanism, be it a database transaction or another transaction mechanism. This allows services to execute multiple business use-cases and easily rollback changes in case of error, without creating a dependency to the database layer.

Sessions should be constituted of a root session created with a "New"-type constructor and allow the creation of child sessions with `Begin()` and `Transaction()`. Nested transactions should be supported as well.

Jump to

Keyboard shortcuts

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