pub struct Database<'a> { /* private fields */ }
Expand description
The database instance. Allows you to create rw_transaction and r_transaction, watch queries, and unwatch etc.
§Example
use native_db::*;
fn main() -> Result<(), db_type::Error> {
let models = Models::new();
// Define models ...
let db = Builder::new().create_in_memory(&models)?;
// Open transactions
// Watch data
// Create snapshots
// etc...
Ok(())
}
Implementations§
Source§impl Database<'_>
impl Database<'_>
Sourcepub fn rw_transaction(&self) -> Result<RwTransaction<'_>>
pub fn rw_transaction(&self) -> Result<RwTransaction<'_>>
Creates a new read-write transaction. This transaction allows you to read and write data.
Sourcepub fn r_transaction(&self) -> Result<RTransaction<'_>>
pub fn r_transaction(&self) -> Result<RTransaction<'_>>
Source§impl Database<'_>
impl Database<'_>
Sourcepub fn unwatch(&self, id: u64) -> Result<bool>
pub fn unwatch(&self, id: u64) -> Result<bool>
Unwatch the given id
.
You can get the id
from the return value of watch
.
If the id
is not valid anymore, this function will do nothing and return false
.
If the id
is valid, the corresponding watcher will be removed and return true
.
If the id
is valid but the watcher is already removed, this function will return false
.
Source§impl<'a> Database<'a>
impl<'a> Database<'a>
Sourcepub fn check_integrity(&mut self) -> Result<bool>
pub fn check_integrity(&mut self) -> Result<bool>
Check the integrity of the database.
Similar to redb::Database::check_integrity().
Sourcepub fn compact(&mut self) -> Result<bool>
pub fn compact(&mut self) -> Result<bool>
Compact the database.
Similar to redb::Database::compact().
Sourcepub fn upgrading_from_version(&self, selector: &str) -> Result<bool>
pub fn upgrading_from_version(&self, selector: &str) -> Result<bool>
Returns true if the database is upgrading from the given version selector.
- If the database is the old version, not matching the selector the function will return `false.
- If the database is not upgrading, the function will return always
false
.
Generally used with the method refresh, to refresh the data for the given model.
Check release notes to know when to use this method.
§Example
if db.upgrading_from_version("<0.8.0") {
// Do something that runs only when the database is upgrading from version <0.8.0.
// If the database is already at version 0.8.0, the function will return false and
// the code will not be executed.
let rw = db.rw_transaction().unwrap();
rw.refresh::<Item1>().unwrap();
rw.refresh::<Item2>().unwrap();
rw.commit().unwrap();
}