beyonder-store 0.1.0

SQLite persistence layer for the Beyond terminal
Documentation
pub mod block_store;
pub mod migrations;
pub mod session_store;

pub use block_store::BlockStore;
pub use session_store::SessionStore;

use rusqlite::Connection;
use std::path::Path;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum StoreError {
    #[error("SQLite error: {0}")]
    Sqlite(#[from] rusqlite::Error),
    #[error("Serialization error: {0}")]
    Serde(#[from] serde_json::Error),
    #[error("Block not found: {0}")]
    NotFound(String),
}

pub type StoreResult<T> = Result<T, StoreError>;

/// Central store holding the SQLite connection.
pub struct Store {
    pub conn: Connection,
}

impl Store {
    pub fn open(path: &Path) -> StoreResult<Self> {
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent).ok();
        }
        let conn = Connection::open(path)?;
        conn.execute_batch("PRAGMA journal_mode=WAL; PRAGMA foreign_keys=ON;")?;
        let store = Self { conn };
        store.run_migrations()?;
        Ok(store)
    }

    pub fn open_in_memory() -> StoreResult<Self> {
        let conn = Connection::open_in_memory()?;
        conn.execute_batch("PRAGMA journal_mode=WAL; PRAGMA foreign_keys=ON;")?;
        let store = Self { conn };
        store.run_migrations()?;
        Ok(store)
    }

    fn run_migrations(&self) -> StoreResult<()> {
        migrations::run(&self.conn)
    }
}