sqlite

Pure-Go SQLite client (modernc.org/sqlite, no CGO) with launcher lifecycle and health check integration.

Install

go get code.nochebuena.dev/go/sqlite

Usage

db := sqlite.New(logger, cfg)
lc.Append(db)
r.Get("/health", health.NewHandler(logger, db))

Testing with :memory:

db := sqlite.New(logger, sqlite.Config{Path: ":memory:"})
db.OnInit()

Unit of Work

uow := sqlite.NewUnitOfWork(logger, db)

err := uow.Do(ctx, func(ctx context.Context) error {
    exec := db.GetExecutor(ctx) // returns the active Tx
    _, err := exec.ExecContext(ctx, "INSERT INTO orders ...")
    return err
})

The UnitOfWork serialises writes via an internal mutex to prevent SQLITE_BUSY errors.

Configuration

Env var Default Description
SQLITE_PATH required File path or :memory:
SQLITE_MAX_OPEN_CONNS 1 Max open connections
SQLITE_MAX_IDLE_CONNS 1 Max idle connections
SQLITE_PRAGMAS ?_journal=WAL&_timeout=5000&_fk=true DSN pragmas

Foreign key enforcement (PRAGMA foreign_keys = ON) is always enabled in OnInit.

Description
SQLite client with health check and lifecycle hooks.
Readme MIT 57 KiB
2026-05-11 19:44:39 -06:00
Languages
Go 100%