storage

package
v1.3.4 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound            = errors.New("storage: not found")
	ErrForeignKeyViolation = errors.New("storage: foreign key violation")
	ErrAlreadyExists       = errors.New("storage: already exists")
	ErrNotNullViolation    = errors.New("storage: not null violation")
)

Functions

func Error

func Error(err error) error

Error checks the error with SQL error codes and returns a storage error if it is nill it returns nil

func IsNotFound

func IsNotFound(err error) bool

func WithTransaction

func WithTransaction(db DB, fn func(tx DBTX) error) error

WithTransaction runs fn inside a database transaction. If fn returns an error the transaction is rolled back; otherwise it is committed. The deferred Rollback is a no-op after a successful Commit.

The callback receives a DBTX so that any repository's WithConn method can accept it, enabling cross-repository transactional operations:

storage.WithTransaction(db, func(tx storage.DBTX) error {
    webhookRepo.WithConn(tx).RegisterWebhook(ctx, ...)
    auditRepo.WithConn(tx).RecordAction(ctx, ...)
    return nil
})

Types

type DB

type DB interface {
	DBTX
	// Ping checks if the database is alive
	Ping() error
	// Close closes the database connection
	Close() error
	// Beginx starts a new transaction
	Beginx() (*sqlx.Tx, error)
}

DB defines an interface for a database connection with restrictive minimum methods for SQLX. DB is a superset of DBTX — it adds lifecycle and transaction methods.

type DBTX added in v0.2.0

type DBTX interface {
	GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
	SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
	NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error)
	ExecContext(context.Context, string, ...any) (sql.Result, error)
}

DBTX is the subset of methods shared by *sqlx.DB and *sqlx.Tx. Repositories use this for all query/exec operations, which allows them to work transparently with either a raw connection or a transaction.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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