pub struct Database { /* private fields */ }Expand description
SQLite database connection wrapper.
Provides methods for storing and querying sessions, messages, and session-to-commit links. Handles schema migrations automatically when opening the database.
Implementations§
Source§impl Database
impl Database
Sourcepub fn open(path: &PathBuf) -> Result<Self>
pub fn open(path: &PathBuf) -> Result<Self>
Opens or creates a database at the specified path.
Runs schema migrations automatically to ensure tables exist.
Sourcepub fn open_default() -> Result<Self>
pub fn open_default() -> Result<Self>
Opens the default database at ~/.lore/lore.db.
Creates the database file and directory if they do not exist.
Sourcepub fn insert_session(&self, session: &Session) -> Result<()>
pub fn insert_session(&self, session: &Session) -> Result<()>
Inserts a new session or updates an existing one.
If a session with the same ID already exists, updates the ended_at
and message_count fields. Also updates the sessions_fts index for
full-text search on session metadata.
Sourcepub fn get_session(&self, id: &Uuid) -> Result<Option<Session>>
pub fn get_session(&self, id: &Uuid) -> Result<Option<Session>>
Retrieves a session by its unique ID.
Returns None if no session with the given ID exists.
Sourcepub fn list_sessions(
&self,
limit: usize,
working_dir: Option<&str>,
) -> Result<Vec<Session>>
pub fn list_sessions( &self, limit: usize, working_dir: Option<&str>, ) -> Result<Vec<Session>>
Lists sessions ordered by start time (most recent first).
Optionally filters by working directory prefix. Returns at most
limit sessions.
Sourcepub fn session_exists_by_source(&self, source_path: &str) -> Result<bool>
pub fn session_exists_by_source(&self, source_path: &str) -> Result<bool>
Checks if a session with the given source path already exists.
Used to detect already-imported sessions during import operations.
Sourcepub fn insert_message(&self, message: &Message) -> Result<()>
pub fn insert_message(&self, message: &Message) -> Result<()>
Inserts a message into the database.
If a message with the same ID already exists, the insert is ignored. Message content is serialized to JSON for storage. Also inserts extracted text content into the FTS index for full-text search.
Sourcepub fn get_messages(&self, session_id: &Uuid) -> Result<Vec<Message>>
pub fn get_messages(&self, session_id: &Uuid) -> Result<Vec<Message>>
Retrieves all messages for a session, ordered by index.
Messages are returned in conversation order (by their index field).
Sourcepub fn insert_link(&self, link: &SessionLink) -> Result<()>
pub fn insert_link(&self, link: &SessionLink) -> Result<()>
Inserts a link between a session and a git commit.
Links can be created manually by users or automatically by the auto-linking system based on time and file overlap heuristics.
Sourcepub fn get_links_by_commit(&self, commit_sha: &str) -> Result<Vec<SessionLink>>
pub fn get_links_by_commit(&self, commit_sha: &str) -> Result<Vec<SessionLink>>
Retrieves all session links for a commit.
Supports prefix matching on the commit SHA, allowing short SHAs (e.g., first 8 characters) to be used for lookup.
Sourcepub fn get_links_by_session(
&self,
session_id: &Uuid,
) -> Result<Vec<SessionLink>>
pub fn get_links_by_session( &self, session_id: &Uuid, ) -> Result<Vec<SessionLink>>
Retrieves all links associated with a session.
A session can be linked to multiple commits if it spans several git operations.
Sourcepub fn delete_link(&self, link_id: &Uuid) -> Result<bool>
pub fn delete_link(&self, link_id: &Uuid) -> Result<bool>
Deletes a specific session link by its ID.
Returns true if a link was deleted, false if no link with that ID existed.
Note: This method is part of the public API for programmatic use, though the CLI currently uses session/commit-based deletion.
Sourcepub fn delete_links_by_session(&self, session_id: &Uuid) -> Result<usize>
pub fn delete_links_by_session(&self, session_id: &Uuid) -> Result<usize>
Deletes all links for a session.
Returns the number of links deleted.
Sourcepub fn delete_link_by_session_and_commit(
&self,
session_id: &Uuid,
commit_sha: &str,
) -> Result<bool>
pub fn delete_link_by_session_and_commit( &self, session_id: &Uuid, commit_sha: &str, ) -> Result<bool>
Deletes a link between a specific session and commit.
The commit_sha is matched as a prefix, so short SHAs work.
Returns true if a link was deleted, false if no matching link existed.
Sourcepub fn search_messages(
&self,
query: &str,
limit: usize,
working_dir: Option<&str>,
since: Option<DateTime<Utc>>,
role: Option<&str>,
) -> Result<Vec<SearchResult>>
pub fn search_messages( &self, query: &str, limit: usize, working_dir: Option<&str>, since: Option<DateTime<Utc>>, role: Option<&str>, ) -> Result<Vec<SearchResult>>
Searches message content using full-text search.
Uses SQLite FTS5 to search for messages matching the query. Returns results ordered by FTS5 relevance ranking.
Optional filters:
working_dir: Filter by working directory prefixsince: Filter by minimum timestamprole: Filter by message role
Note: This is the legacy search API. For new code, use search_with_options.
Sourcepub fn search_with_options(
&self,
options: &SearchOptions,
) -> Result<Vec<SearchResult>>
pub fn search_with_options( &self, options: &SearchOptions, ) -> Result<Vec<SearchResult>>
Searches messages and session metadata using full-text search with filters.
Uses SQLite FTS5 to search for messages matching the query. Also searches session metadata (tool, project, branch) via sessions_fts. Returns results ordered by FTS5 relevance ranking.
Supports extensive filtering via SearchOptions:
tool: Filter by AI tool namesince/until: Filter by date rangeproject: Filter by project name (partial match)branch: Filter by git branch (partial match)role: Filter by message rolerepo: Filter by working directory prefix
Sourcepub fn get_context_messages(
&self,
session_id: &Uuid,
message_index: i32,
context_count: usize,
) -> Result<(Vec<Message>, Vec<Message>)>
pub fn get_context_messages( &self, session_id: &Uuid, message_index: i32, context_count: usize, ) -> Result<(Vec<Message>, Vec<Message>)>
Gets messages around a specific message for context.
Returns N messages before and N messages after the specified message, useful for displaying search results with surrounding context.
Sourcepub fn get_message_by_index(
&self,
session_id: &Uuid,
index: i32,
) -> Result<Option<Message>>
pub fn get_message_by_index( &self, session_id: &Uuid, index: i32, ) -> Result<Option<Message>>
Gets a single message by its index within a session.
Sourcepub fn rebuild_search_index(&self) -> Result<usize>
pub fn rebuild_search_index(&self) -> Result<usize>
Rebuilds the full-text search index from existing messages and sessions.
This should be called when:
- Upgrading from a database without FTS support
- The FTS index becomes corrupted or out of sync
Returns the number of messages indexed.
Sourcepub fn search_index_needs_rebuild(&self) -> Result<bool>
pub fn search_index_needs_rebuild(&self) -> Result<bool>
Checks if the search index needs rebuilding.
Returns true if there are messages or sessions in the database but the FTS indexes are empty, indicating data was imported before FTS was added.
Sourcepub fn session_count(&self) -> Result<i32>
pub fn session_count(&self) -> Result<i32>
Returns the total number of sessions in the database.
Sourcepub fn message_count(&self) -> Result<i32>
pub fn message_count(&self) -> Result<i32>
Returns the total number of messages across all sessions.
Sourcepub fn link_count(&self) -> Result<i32>
pub fn link_count(&self) -> Result<i32>
Returns the total number of session links in the database.
Sourcepub fn db_path(&self) -> Option<PathBuf>
pub fn db_path(&self) -> Option<PathBuf>
Returns the path to the database file, if available.
Returns None for in-memory databases.
Sourcepub fn find_sessions_near_commit_time(
&self,
commit_time: DateTime<Utc>,
window_minutes: i64,
working_dir: Option<&str>,
) -> Result<Vec<Session>>
pub fn find_sessions_near_commit_time( &self, commit_time: DateTime<Utc>, window_minutes: i64, working_dir: Option<&str>, ) -> Result<Vec<Session>>
Finds sessions that were active around a commit time.
A session is considered active if the commit time falls within the window before and after the session’s time range (started_at to ended_at).
§Arguments
commit_time- The timestamp of the commitwindow_minutes- The window in minutes before/after the sessionworking_dir- Optional working directory filter (prefix match)
§Returns
Sessions that were active near the commit time, ordered by proximity.