use crate::{database::Database, error::Error, model::Model, transaction::Transaction};
#[derive(Debug)]
pub struct TransactionBuilder<'a> {
database: &'a idb::Database,
mode: idb::TransactionMode,
stores: Vec<&'a str>,
}
impl<'a> TransactionBuilder<'a> {
pub fn new(database: &'a Database) -> Self {
Self {
database: database.as_idb_database(),
mode: idb::TransactionMode::ReadOnly,
stores: Vec::new(),
}
}
pub fn writable(mut self) -> Self {
self.mode = idb::TransactionMode::ReadWrite;
self
}
pub fn with_model<M>(mut self) -> Self
where
M: Model,
{
self.stores.push(M::NAME);
self
}
pub fn build(self) -> Result<Transaction, Error> {
self.database
.transaction(&self.stores, self.mode)
.map(Transaction::new)
.map_err(Into::into)
}
}