feat(db-sqlite): initial implementation — database/sql SQLite with lifecycle and UnitOfWork (v1.0.0)
Introduces code.nochebuena.dev/einherjar/db-sqlite — the SQLite database starter for the Einherjar framework. Absorbs the sqlite package from micro-lib using the pure-Go modernc.org/sqlite driver (no CGO, cross-compilation compatible). Interfaces (CT-6: one TypeSpec per file): - Executor — ExecContext, QueryContext, QueryRowContext (database/sql types) - Tx — Executor + Commit(), Rollback() — no ctx; honest database/sql contract - Provider — GetExecutor, Begin, Ping, HandleError - Component — lifecycle.Component + observability.Checkable + Provider - UnitOfWork — Do(ctx, fn) Implementation: - New(logger, cfg) Component — pool not created until OnInit - OnInit: sql.Open with DSN (Path + Pragmas); sets MaxOpenConns, MaxIdleConns - OnStart: Ping with 5s timeout; logs "sqlite: connected" - OnStop: db.Close(); logs "sqlite: closing pool" - GetExecutor: returns active Tx from context (ctxTxKey) or *sql.DB; explicit nil return when db uninitialized to avoid typed-nil interface pitfall - Begin: wraps db.BeginTx; wrapped in xerrors on error - HealthCheck: delegates to Ping; Priority LevelDegraded (file DB; absence is non-fatal on a server restart — file always present if configured) - NewUnitOfWork(logger, provider) UnitOfWork — acquires writeMu before Begin to prevent SQLITE_BUSY under concurrent goroutines (SQLite single-writer model) - writeMu: extracted from *sqliteImpl via type assertion in NewUnitOfWork; gracefully skipped when provider is a mock (nil mu) - HandleError: sql.ErrNoRows→ErrNotFound, unique/pk constraint→ErrAlreadyExists, foreign key→ErrInvalidInput, all others→ErrInternal Config (EINHERJAR_SQLITE_* env vars): Path(required), MaxOpenConns(1), MaxIdleConns(1), Pragmas(?_journal=WAL&_timeout=5000&_fk=true) - Component interface embeds observability.Identifiable; identifiable.go implements ModulePath and ModuleVersion via runtime/debug.ReadBuildInfo() — prints in launcher banner
This commit is contained in:
69
doc.go
Normal file
69
doc.go
Normal file
@@ -0,0 +1,69 @@
|
||||
// Package sqlite provides a pure-Go SQLite client with lifecycle management,
|
||||
// health checks, and unit-of-work transaction support for Einherjar applications.
|
||||
//
|
||||
// # Overview
|
||||
//
|
||||
// [New] returns a [Component] that manages a [database/sql] connection pool,
|
||||
// satisfies the [lifecycle.Component] lifecycle hooks (OnInit, OnStart, OnStop),
|
||||
// and implements [observability.Checkable] with critical priority.
|
||||
//
|
||||
// [NewUnitOfWork] wraps multiple repository operations in a single serialized
|
||||
// transaction via context injection — no transaction object is passed between
|
||||
// functions. Write transactions are serialized through an internal mutex to
|
||||
// prevent SQLITE_BUSY errors under concurrent goroutines.
|
||||
//
|
||||
// # Lifecycle Registration
|
||||
//
|
||||
// db := sqlite.New(logger, cfg)
|
||||
// launcher.Register(db)
|
||||
// health.Register(db)
|
||||
//
|
||||
// # Querying
|
||||
//
|
||||
// Repository code receives a [Provider] or [Executor] and calls [Provider.GetExecutor]
|
||||
// to obtain the active transaction (if inside a [UnitOfWork]) or the connection pool:
|
||||
//
|
||||
// exec := db.GetExecutor(ctx)
|
||||
// row := exec.QueryRowContext(ctx, "SELECT id FROM users WHERE email = ?", email)
|
||||
// if err := row.Scan(&id); err != nil {
|
||||
// return db.HandleError(err) // maps sql.ErrNoRows → ErrNotFound, etc.
|
||||
// }
|
||||
//
|
||||
// # Unit of Work
|
||||
//
|
||||
// [NewUnitOfWork] wraps operations in a single serialized write transaction.
|
||||
// The transaction is injected into the context; [Provider.GetExecutor] returns
|
||||
// it automatically.
|
||||
//
|
||||
// uow := sqlite.NewUnitOfWork(logger, db)
|
||||
// err := uow.Do(ctx, func(ctx context.Context) error {
|
||||
// exec := db.GetExecutor(ctx) // returns active Tx
|
||||
// _, err := exec.ExecContext(ctx, "INSERT INTO orders ...")
|
||||
// return err
|
||||
// })
|
||||
//
|
||||
// # Error Handling
|
||||
//
|
||||
// [HandleError] translates database/sql and SQLite error codes into typed
|
||||
// [xerrors] values. Call it at every point where a database error is first observed:
|
||||
//
|
||||
// if err := row.Scan(&out); err != nil {
|
||||
// return db.HandleError(err)
|
||||
// }
|
||||
//
|
||||
// Mapped codes:
|
||||
// - sql.ErrNoRows → ErrNotFound
|
||||
// - SQLITE_CONSTRAINT_UNIQUE (2067) → ErrAlreadyExists
|
||||
// - SQLITE_CONSTRAINT_PRIMARYKEY (1555) → ErrAlreadyExists
|
||||
// - SQLITE_CONSTRAINT_FOREIGNKEY (787) → ErrInvalidInput
|
||||
// - all others → ErrInternal
|
||||
//
|
||||
// # Configuration
|
||||
//
|
||||
// All fields are read from environment variables with the EINHERJAR_SQLITE_* prefix:
|
||||
//
|
||||
// - EINHERJAR_SQLITE_PATH (required) — file path or ":memory:"
|
||||
// - EINHERJAR_SQLITE_MAX_OPEN_CONNS — default: 1
|
||||
// - EINHERJAR_SQLITE_MAX_IDLE_CONNS — default: 1
|
||||
// - EINHERJAR_SQLITE_PRAGMAS — default: "?_journal=WAL&_timeout=5000&_fk=true"
|
||||
package sqlite
|
||||
Reference in New Issue
Block a user