-
Release v1.0.0 Stable
released this
2026-05-29 09:52:14 -06:00 | 0 commits to main since this releasev1.0.0
code.nochebuena.dev/einherjar/db-mysql
Architecture Decisions Resolved
Decision Outcome Tx Commit/Rollback context No ctx — honest database/sqlcontract;sql.Txdoes not support per-call contextBeginTx vs Begin Both — BeginTxexposes*sql.TxOptionsfor isolation level controlWrite mutex Not needed — MySQL is a server DB; concurrency handled server-side GetExecutor nil guard Explicit if db == nil { return nil }— avoids typed-nil interface pitfallHealth priority LevelCritical— MySQL outage halts the serviceDriver alias mysqldrv "github.com/go-sql-driver/mysql"in errors.go — avoids package-name collisionCollation Set at schema level (DDL), not in DSN — MariaDB 11.4+ collations exceed go-sql-driver's 1-byte handshake limit
API
import "code.nochebuena.dev/einherjar/db-mysql" // Interfaces type Executor interface { ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row } type Tx interface { Executor Commit() error // no ctx — database/sql limitation Rollback() error } type Provider interface { GetExecutor(ctx context.Context) Executor Begin(ctx context.Context) (Tx, error) BeginTx(ctx context.Context, opts *sql.TxOptions) (Tx, error) Ping(ctx context.Context) error HandleError(err error) error } type Component interface { lifecycle.Component observability.Checkable observability.Identifiable Provider Stats() sql.DBStats } type UnitOfWork interface { Do(ctx context.Context, fn func(ctx context.Context) error) error } // Config type Config struct { Host string `env:"EINHERJAR_MYSQL_HOST,required"` Port int `env:"EINHERJAR_MYSQL_PORT" envDefault:"3306"` User string `env:"EINHERJAR_MYSQL_USER,required"` Password string `env:"EINHERJAR_MYSQL_PASSWORD,required"` Name string `env:"EINHERJAR_MYSQL_NAME,required"` MaxConns int `env:"EINHERJAR_MYSQL_MAX_CONNS" envDefault:"5"` MinConns int `env:"EINHERJAR_MYSQL_MIN_CONNS" envDefault:"2"` MaxConnLifetime string `env:"EINHERJAR_MYSQL_MAX_CONN_LIFETIME" envDefault:"1h"` MaxConnIdleTime string `env:"EINHERJAR_MYSQL_MAX_CONN_IDLE_TIME" envDefault:"30m"` Charset string `env:"EINHERJAR_MYSQL_CHARSET" envDefault:"utf8mb4"` Loc string `env:"EINHERJAR_MYSQL_LOC" envDefault:"UTC"` ParseTime string `env:"EINHERJAR_MYSQL_PARSE_TIME" envDefault:"true"` } func DefaultConfig() Config func (c Config) DSN() string // Constructors func New(logger logging.Logger, cfg Config) Component func NewUnitOfWork(logger logging.Logger, client Provider) UnitOfWork // Package-level error handler (also available as Provider.HandleError) func HandleError(err error) error
Install
go get code.nochebuena.dev/einherjar/db-mysql@v1.0.0
Dependencies
Module Version Role code.nochebuena.dev/einherjar/contractsv1.0.0 lifecycle.Component,observability.Checkable,logging.Loggercode.nochebuena.dev/einherjar/corev1.0.0 xerrorsgithub.com/go-sql-driver/mysqlv1.8.1 MySQL driver Downloads