pub struct Database { /* private fields */ }Expand description
Abstraction for flora’s primary datastore (SQLite).
Implementations§
Source§impl Database
impl Database
Sourcepub async fn optimize(&self) -> Result<(), DatabaseError>
pub async fn optimize(&self) -> Result<(), DatabaseError>
Runs “PRAGMA optimize=0x10002”.
See https://sqlite.org/lang_analyze.html.
§Errors
Returns DatabaseError if the operation fails.
Sourcepub async fn new_with_records_table(
path: impl AsRef<Path>,
) -> Result<Self, DatabaseError>
pub async fn new_with_records_table( path: impl AsRef<Path>, ) -> Result<Self, DatabaseError>
Initializes the database, activates WAL, creates a ‘records’ table, and creates indexes.
§Errors
Returns DatabaseError if the operation fails.
Sourcepub async fn count(&self) -> Result<i64, DatabaseError>
pub async fn count(&self) -> Result<i64, DatabaseError>
Queries the database to get a count of records it contains.
§Errors
Returns DatabaseError if the operation fails.
Sourcepub async fn insert(&self, record: DbRecord) -> Result<i64, DatabaseError>
pub async fn insert(&self, record: DbRecord) -> Result<i64, DatabaseError>
Inserts the given record into the Database.
Returns the ID of the inserted row.
§Errors
Returns DatabaseError if the insert operation fails.
Sourcepub async fn delete(&self, id: i64) -> Result<usize, DatabaseError>
pub async fn delete(&self, id: i64) -> Result<usize, DatabaseError>
Deletes the record corresponding to the given id.
Returns the number of documents affected (should be 1 if a document matching the id was deleted, and 0 if no matching document was found in the DB).
§Errors
Returns DatabaseError if the operation fails.
Sourcepub async fn get_record(&self, id: i64) -> Result<DbRecord, DatabaseError>
pub async fn get_record(&self, id: i64) -> Result<DbRecord, DatabaseError>
Sourcepub async fn get_many_records(
&self,
ids: &[i64],
) -> Result<Vec<DbRecord>, DatabaseError>
pub async fn get_many_records( &self, ids: &[i64], ) -> Result<Vec<DbRecord>, DatabaseError>
Get multiple records from SQLite given their IDs.
§Errors
Returns DatabaseError if the operation fails.
Sourcepub async fn get_all_records(
&self,
start_row: Option<i64>,
) -> Result<Vec<DbRecord>, DatabaseError>
pub async fn get_all_records( &self, start_row: Option<i64>, ) -> Result<Vec<DbRecord>, DatabaseError>
Selects all rows in the records table, without any pagination.
§Errors
Returns DatabaseError if the operation fails.
Sourcepub async fn get_all_meta(
&self,
start_row: Option<i64>,
) -> Result<Vec<DbRecordMeta>, DatabaseError>
pub async fn get_all_meta( &self, start_row: Option<i64>, ) -> Result<Vec<DbRecordMeta>, DatabaseError>
Selects all rows in the records table, without any pagination,
and returns their metadata (all fields except serialized data
of the page content).
§Errors
Returns DatabaseError if the operation fails.
Sourcepub async fn get_all_ids(&self) -> Result<Vec<i64>, DatabaseError>
pub async fn get_all_ids(&self) -> Result<Vec<i64>, DatabaseError>
Sourcepub async fn most_recent_meta(
&self,
start_idx: Option<usize>,
) -> Result<Vec<DbRecordMeta>, DatabaseError>
pub async fn most_recent_meta( &self, start_idx: Option<usize>, ) -> Result<Vec<DbRecordMeta>, DatabaseError>
Similar to Database::get_all_meta, except orders records by
descending order of timestamp_flora.
Returns an empty list if the given index is out of bounds.