database

package
v0.0.0-...-b3d25bc Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetTx

func GetTx(ctx context.Context) (*sql.Tx, bool)

GetTx retrieves the transaction from context if present.

func IsCommitError

func IsCommitError(err error) bool

IsCommitError checks if an error is a CommitError.

func NewCommitError

func NewCommitError(err error) error

NewCommitError creates an error that will commit the transaction but still return an error.

Types

type CommitError

type CommitError struct {
	Err error
}

CommitError wraps an error that should still allow transaction to commit.

func (*CommitError) Error

func (e *CommitError) Error() string

func (*CommitError) Unwrap

func (e *CommitError) Unwrap() error

type Config

type Config struct {
	Host     string
	Port     int
	Database string
	Username string
	Password string
	Schema   string
}

Config holds database configuration

type PoolConfig

type PoolConfig struct {
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime time.Duration
	ConnMaxIdleTime time.Duration
}

PoolConfig holds connection pool settings

type Service

type Service interface {
	Close() error
	GetDB() *sql.DB
	Health() map[string]string
}

Service defines database operations

func New

func New(cfg Config, logger *logger.Logger) (Service, error)

New creates a new database service

type TransactionManager

type TransactionManager interface {
	WithTransaction(ctx context.Context, fn func(ctx context.Context) error) error
}

TransactionManager defines the interface for transaction management.

type TxKey

type TxKey struct{}

type TxManager

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

func NewTxManager

func NewTxManager(db *sql.DB) *TxManager

func (*TxManager) WithTransaction

func (tm *TxManager) WithTransaction(ctx context.Context, fn func(ctx context.Context) error) error

WithTransaction runs fn within a transaction. If fn returns nil, the transaction is committed. If fn returns a CommitError, the transaction is committed but the error is returned. If fn returns any other error, the transaction is rolled back.

Jump to

Keyboard shortcuts

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