database

package
v0.0.0-...-5a3d283 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithContext

func WithContext(ctx context.Context, db *gorm.DB, fn func(tx *gorm.DB) error) error

WithContext executes a function within a transaction with a context If the function returns an error, the transaction is rolled back Otherwise, the transaction is committed

func WithContextAndOptions

func WithContextAndOptions(ctx context.Context, db *gorm.DB, opts TransactionOptions, fn func(tx *gorm.DB) error) error

WithContextAndOptions executes a function within a transaction with a context and options

func WithRetry

func WithRetry(ctx context.Context, db *gorm.DB, maxAttempts int, fn func(tx *gorm.DB) error) error

WithRetry executes a function within a transaction with retry logic It will retry the transaction up to maxAttempts times if it encounters a conflict or deadlock error

Types

type Database

type Database interface {
	// Ping checks database connectivity
	Ping(ctx context.Context) error

	// GetDB returns the underlying database connection
	GetDB() *gorm.DB

	// Transaction runs operations in a transaction
	Transaction(fn func(tx *gorm.DB) error) error

	// Close closes the database connection
	Close() error
}

Database defines the interface for database operations

func NewDatabase

func NewDatabase(cfg *config.DatabaseConfig) (Database, error)

NewDatabase creates a new database connection with retry mechanism

type GormDatabase

type GormDatabase struct {
	// contains filtered or unexported fields
}

GormDatabase implements Database interface using GORM

func (*GormDatabase) Close

func (d *GormDatabase) Close() error

Close closes the database connection

func (*GormDatabase) GetDB

func (d *GormDatabase) GetDB() *gorm.DB

GetDB returns the underlying database connection

func (*GormDatabase) Ping

func (d *GormDatabase) Ping(ctx context.Context) error

Ping checks database connectivity

func (*GormDatabase) Transaction

func (d *GormDatabase) Transaction(fn func(tx *gorm.DB) error) error

Transaction runs operations in a transaction

type TransactionOptions

type TransactionOptions struct {
	// Timeout is the maximum duration a transaction can run before being rolled back
	Timeout time.Duration

	// IsolationLevel sets the transaction isolation level
	IsolationLevel sql.IsolationLevel

	// ReadOnly sets whether the transaction is read-only
	ReadOnly bool
}

TransactionOptions contains options for database transactions

func DefaultTransactionOptions

func DefaultTransactionOptions() TransactionOptions

DefaultTransactionOptions returns the default transaction options

Jump to

Keyboard shortcuts

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