CARLOS rastrillo docs

๐Ÿค– db

amadan.net/rastrillo/rastrillo/db

Opens your SQLite database the way a CARLOS app needs it, as one *gorm.DB. It is a small package, and almost all its value is in what Open gets right. Data is the guide.

Open

func Open(path string, log *slog.Logger, options ...Option) (*DB, error)

Opens one file as two pools and returns a handle wrapping both. A nil logger defaults to slog.Default(). The only option is DurableCommits.

What it sets for you, and why each one is there:

DurableCommits

func DurableCommits() Option

Opens both pools with synchronous(FULL) instead of NORMAL. SQLite syncs the WAL after every commit, helping preserve recent commits through a power loss or operating system crash. With NORMAL, an app crash does not lose committed data, but a power loss or operating system crash can lose recent commits without corrupting the database. In the v0.34.0 benchmark, the toggle rate fell from about 5,700 per second to about 182. Use this option when preserving recent commits through a machine crash matters more than write speed.

DB

type DB struct {
	G *gorm.DB
}

G is the handle your models and handlers use. Writer and Reader return the two *sql.DB pools behind it.

DB.Writer

func (d *DB) Writer() *sql.DB

The write pool โ€” one connection. migrate.Apply takes it directly rather than through the resolver, because it pins a single connection for a whole run: PRAGMA foreign_keys is per-connection state, and SQLite's twelve-step table rebuild has to toggle it outside the transaction.

d.G.DB() gets you the same pool through GORM, and is what most code uses when handing a *sql.DB to a database/sql package like sessions.

DB.Reader

func (pools *DB) Reader() *sql.DB

The read pool has several connections, so reads can run while a write is in progress. Pass it as Reader to sessions or auth only if a mixed-load test on your instance shows it helps. On one core, under heavy mixed load, it slows writes. Only read through it. Writes go through Writer, which keeps them to one connection.

DB.Close

func (d *DB) Close() error

Closes both pools. It returns the writer's error in preference to the reader's, so a single returned error names the pool whose failure matters.

Read this page as markdown โ€” exact, unstyled, and cheap for an agent to fetch.