storage

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2025 License: MIT Imports: 5 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(ctx context.Context, connection DB, fn func(ctx context.Context, tx *sqlx.Tx) error) error

WithTransaction creates a new transaction and executes the given function inside the transaction

Types

type DB

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

DB defines an interface for a database connection with restricive minimum methods fro SQLX

type Reader

type Reader interface {
	// SelectContext gets a single row from the database
	GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
	// SelectContext gets multiple rows from the database
	SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
}

Reader defines an interface for a database connection with read methods. This is used to define a read-only connection. This helps to prevent accidental writes to the database when a read-only connection is used and moving the read operations to a separate database for performance reasons like read replicas.

type TX

type TX interface {
	DB
	Commit() error
	Rollback() error
}

TX defines an interface for a database transaction

type Writer

type Writer interface {
	// NamedExecContext executes a query without returning any rows
	NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error)
	// ExecContext executes a query without returning any rows
	ExecContext(context.Context, string, ...any) (sql.Result, error)
}

Writer defines an interface for a database connection with write methods. This is used to define a write-only connection. This helps with moving the write operations to a separate database for performance reasons.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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