Skip to main content

Database

Struct Database 

Source
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

Source

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
Source

pub fn new_in_memory() -> Result<Self>

Creates a new in-memory database (primarily for testing).

§Errors

Returns an error if schema initialization fails.

Source

pub fn get_connection(&self) -> &Connection

Returns a reference to the underlying SQLite connection.

Source

pub fn with_transaction<T, F>(&self, f: F) -> Result<T>
where F: FnOnce() -> Result<T>,

Execute operations within an IMMEDIATE transaction.

This method wraps the provided closure in a SQLite transaction that:

  • Uses BEGIN IMMEDIATE to 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(())
})?;
Source

pub fn get_app_state(&self, key: &str) -> Result<Option<String>>

Source

pub fn set_app_state(&self, key: &str, value: &str) -> Result<()>

Source

pub fn get_vcs_mode(&self) -> Result<VcsMode>

Returns the configured VCS backend (jj by default).

Source

pub fn set_vcs_mode(&self, mode: VcsMode) -> Result<()>

Persists the VCS backend preference.

Source

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.

Source

pub fn set_current_task_id(&self, task_id: i64) -> Result<()>

Sets the current active task.

§Arguments
  • task_id - The ID of the task to set as current
Source

pub fn clear_current_task_id(&self) -> Result<()>

Clears the current active task.

Source

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”)
Source

pub fn get_rev(&self, section: &str) -> Result<i64>

Gets the current revision number for a section.

§Arguments
  • section - The section name
Source

pub fn get_all_revs(&self) -> Result<SectionRevs>

Gets all section revision numbers at once.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more