Documentation
¶
Index ¶
- func WithContext(ctx context.Context, db *gorm.DB, fn func(tx *gorm.DB) error) error
- func WithContextAndOptions(ctx context.Context, db *gorm.DB, opts TransactionOptions, ...) error
- func WithRetry(ctx context.Context, db *gorm.DB, maxAttempts int, fn func(tx *gorm.DB) error) error
- type Database
- type GormDatabase
- type TransactionOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithContext ¶
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
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
Click to show internal directories.
Click to hide internal directories.