pub struct Database { /* private fields */ }Expand description
Database connection and management.
Handles SQLite database operations including schema initialization, migrations, and application state persistence.
Implementations§
Source§impl Database
impl Database
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Creates a new database instance with the default file location.
The database file is stored in the platform-specific data directory. The schema is automatically initialized if the database is new.
§Errors
Returns an error if:
- The data directory cannot be determined
- The database file cannot be created or opened
- Schema initialization fails
Sourcepub fn new_in_memory() -> Result<Self>
pub fn new_in_memory() -> Result<Self>
Creates a new in-memory database (primarily for testing).
§Errors
Returns an error if schema initialization fails.
Sourcepub fn get_connection(&self) -> &Connection
pub fn get_connection(&self) -> &Connection
Returns a reference to the underlying SQLite connection.
Sourcepub fn with_transaction<T, F>(&self, f: F) -> Result<T>
pub fn with_transaction<T, F>(&self, f: F) -> Result<T>
Execute operations within an IMMEDIATE transaction.
This method wraps the provided closure in a SQLite transaction that:
- Uses
BEGIN IMMEDIATEto acquire the write lock upfront - Automatically commits on success
- Automatically rolls back on error
Using IMMEDIATE mode prevents race conditions in read-modify-write sequences by acquiring the write lock before reading, ensuring no other process can modify the data between the read and write steps.
§Arguments
f- A closure that performs database operations and returns a Result
§Example
db.with_transaction(|| {
let next_index = get_next_index()?;
insert_with_index(next_index)?;
Ok(())
})?;pub fn get_app_state(&self, key: &str) -> Result<Option<String>>
pub fn set_app_state(&self, key: &str, value: &str) -> Result<()>
Sourcepub fn get_vcs_mode(&self) -> Result<VcsMode>
pub fn get_vcs_mode(&self) -> Result<VcsMode>
Returns the configured VCS backend (jj by default).
Sourcepub fn set_vcs_mode(&self, mode: VcsMode) -> Result<()>
pub fn set_vcs_mode(&self, mode: VcsMode) -> Result<()>
Persists the VCS backend preference.
Sourcepub fn get_current_task_id(&self) -> Result<Option<i64>>
pub fn get_current_task_id(&self) -> Result<Option<i64>>
Gets the ID of the current active task.
§Returns
Some(task_id) if a task is currently active, None otherwise.
Sourcepub fn set_current_task_id(&self, task_id: i64) -> Result<()>
pub fn set_current_task_id(&self, task_id: i64) -> Result<()>
Sourcepub fn clear_current_task_id(&self) -> Result<()>
pub fn clear_current_task_id(&self) -> Result<()>
Clears the current active task.
Sourcepub fn increment_rev(&self, section: &str) -> Result<i64>
pub fn increment_rev(&self, section: &str) -> Result<i64>
Increments the revision number for a section and returns the new value.
§Arguments
section- The section name (e.g., “todos”, “scraps”, “links”, “repos”, “worktrees”, “task”)
Sourcepub fn get_all_revs(&self) -> Result<SectionRevs>
pub fn get_all_revs(&self) -> Result<SectionRevs>
Gets all section revision numbers at once.