50 lines
1.1 KiB
Markdown
50 lines
1.1 KiB
Markdown
|
|
# 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
|
||
|
|
|
||
|
|
```go
|
||
|
|
db := sqlite.New(logger, cfg)
|
||
|
|
lc.Append(db)
|
||
|
|
r.Get("/health", health.NewHandler(logger, db))
|
||
|
|
```
|
||
|
|
|
||
|
|
## Testing with :memory:
|
||
|
|
|
||
|
|
```go
|
||
|
|
db := sqlite.New(logger, sqlite.Config{Path: ":memory:"})
|
||
|
|
db.OnInit()
|
||
|
|
```
|
||
|
|
|
||
|
|
## Unit of Work
|
||
|
|
|
||
|
|
```go
|
||
|
|
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`.
|