store

SQLite-backed store: connection management, migrations, models, and repos.

The store opens a per-user libsql database (local file or remote URL), runs schema migrations at startup, and exposes repo helpers that translate between domain models and database rows. When a project has been resolved, Db::configure_sync wires in transparent import/export against the project’s .gest/ directory.

Name Summary
avatar_cache Filesystem-backed Gravatar avatar cache. Filesystem-backed cache for Gravatar avatar bytes.
meta JSON metadata helpers shared by all meta subcommands. JSON metadata helpers for reading and writing dot-delimited key paths.
migration Sequential schema migrations applied at startup. Sequential schema migrations for the application database.
model Domain model types persisted by the store. Domain model types persisted by the store.
repo Repository helpers that read and write domain models. Data-access repositories for domain models.
search_query Parsed representation of search query strings. Parsed representation of a search query string with structured filters.
sync Import and export between the database and a project’s .gest/ directory. Local sync mirror for .gest/ directories.

const BUSY_TIMEOUT

source

const BUSY_TIMEOUT: Duration = Duration::from_secs(5);

How long a connection waits for a lock that another connection holds before it fails with SQLITE_BUSY. Several agents run gest on one store at the same time, so a write may meet another process’s write.

struct Db

source

pub struct Db

Thin wrapper around a libsql::Database with optional transparent sync.

Field Type Description
file_backed bool Whether the database is a file-backed local SQLite database.
imported AtomicBool Whether the initial sync import has already run this process.
inner Database The libsql database handle that every connection is opened from.
export_lock tokio::sync::Mutex<()> Held while an export writes .gest/, so two exports in one process never write the same files at once.
sync_ctx OnceLock<(Id, PathBuf)> Sync context set after project resolution: (project_id, gest_dir).

Implements Debug.

Db::connect

source

pub async fn connect(&self) -> Result<Connection, Error>

Obtain a new connection to the underlying database.

Each connection has PRAGMA foreign_keys = ON enabled so that REFERENCES constraints are enforced, and waits up to BUSY_TIMEOUT for a lock that another connection holds; a remote database ignores the timeout. A file-backed database also switches the connection to WAL journal mode.

Parameter Type Description
returns Result<Connection, Error> The new connection, or an error when libsql cannot open it or a PRAGMA fails.

Db::configure_sync

source

pub fn configure_sync(&self, project_id: Id, gest_dir: PathBuf)

Configure transparent sync with a .gest/ directory.

Must be called after project resolution and before the first import_if_needed() call. Subsequent calls are no-ops.

Parameter Type Description
project_id Id the resolved project whose entities are synced
gest_dir PathBuf the project’s .gest/ directory to import from and export to

Db::import_if_needed

source

pub async fn import_if_needed(&self) -> Result<(), Error>

Run the sync import if configured and not yet imported this process.

Called automatically at application startup; safe to call multiple times (only the first call actually imports).

Parameter Type Description
returns Result<(), Error> Ok(()) when there is nothing to import or the import ran; an error only when a connection cannot be opened, since a failed import is logged as a warning, not returned.

Db::export_if_needed

source

pub async fn export_if_needed(&self) -> Result<(), Error>

Run the sync export if configured.

Called at application exit to flush any database changes back to the .gest/ directory, and by gest serve after each web write. Exports in one process run one at a time. An export runs in no transaction, so it takes the database’s write lock only for each short digest update.

Parameter Type Description
returns Result<(), Error> Ok(()) when sync is not configured or the export ran; an error only when a connection cannot be opened, since a failed export is logged as a warning, not returned.

Db::web_write_lock

source

pub async fn web_write_lock(&self) -> tokio::sync::MutexGuard<'_, ()>

Take the lock that gest serve holds around each web write.

The web layer holds it across the import before a write, the write and the export after it, so no other web write in this process can import between one write’s commit and its export and put the old .gest/ back.

Parameter Type Description
returns tokio::sync::MutexGuard<'_, ()>

Db::import_now

source

pub async fn import_now(&self) -> Result<(), Error>

Import .gest/ into the database now, if sync is configured, and return any error.

gest serve calls this before each web write, so files that a git pull added while it runs reach the database before the export after the write, which would otherwise delete them as orphans.

Parameter Type Description
returns Result<(), Error>

Db::export_now

source

pub async fn export_now(&self) -> Result<(), Error>

Export the database to .gest/ now, if sync is configured, and return any error.

The caller must hold Db::web_write_lock.

Parameter Type Description
returns Result<(), Error>

enum Error

source

pub enum Error

Errors that can occur anywhere in the store subsystem.

This is the single error type used by every module under src/store/: connection management, schema migrations, model row decoding, repositories, and sync. CLI and web callers consume store::Error directly — no layer inside the store should construct per-module error types on their behalf.

Variant Description
Ambiguous(String, usize) An id prefix matched multiple entities.
Config(crate::config::Error) A configuration value (such as the resolved data directory) could not be read.
Database(Error) The underlying libsql driver returned an error.
InvalidPrefix(String) The given id prefix is invalid.
InvalidValue(String) A column value or row field could not be parsed into the expected domain type.
Io(Error) A filesystem I/O error occurred while accessing the database or sync files.
NotFound(String) The requested entity could not be found.
NothingToUndo No undoable transaction was available.
Serialization(serde_json::Error) A JSON serialization error while encoding or decoding payloads.
Yaml(yaml_serde::Error) A YAML serialization error while reading or writing the .gest/ directory.

Implements From<Error>, Debug, Error, Display.

fn begin_write

source

pub async fn begin_write(conn: &Connection) -> Result<(), Error>

Open a write transaction on conn with BEGIN IMMEDIATE.

IMMEDIATE takes the write lock before the first statement, waiting up to the busy timeout for another writer. A deferred BEGIN that reads first must upgrade its lock later, and in WAL mode that upgrade fails at once with SQLITE_BUSY when another connection has written in between, without waiting.

Parameter Type Description
conn &Connection
returns Result<(), Error> Ok(()) once the transaction holds the write lock, or the database error.

fn open_with_gest_dir

source

pub async fn open_with_gest_dir(
    settings: &crate::config::Settings,
    gest_dir: Option<&Path>,
) -> Result<Arc<Db>, Error>

Open (or create) the database, optionally preferring a project-local .gest directory when no explicit database or data-dir override was configured.

A configured database.url is refused with crate::config::Error::RemoteDatabaseUnsupported. Otherwise the local gest.db lives in the data-dir override, else in gest_dir, else in the default data directory. The chosen directory is created if missing. Pending migrations run before the handle is returned.

Parameter Type Description
settings &crate::config::Settings the loaded configuration, which names the database URL and the data directory
gest_dir Option<&Path> the project’s .gest directory, if any
returns Result<Arc<Db>, Error> The shared database handle, or an error when the directory, the database or a migration fails.