1mod index;
2mod store;
3
4pub use self::{index::StoreIndex, store::Store};
5
6use idb::Transaction as IdbTransaction;
7
8use crate::{Error, Result, TransactionMode, TransactionResult};
9
10pub struct Transaction {
12 pub(crate) transaction: IdbTransaction,
13}
14
15impl Transaction {
16 pub fn mode(&self) -> Result<TransactionMode> {
18 self.transaction.mode().map_err(Into::into)
19 }
20
21 pub fn store_names(&self) -> Vec<String> {
23 self.transaction.store_names()
24 }
25
26 pub async fn abort(self) -> Result<()> {
28 let result = self.transaction.abort()?.await?;
29
30 if result.is_aborted() {
31 Ok(())
32 } else {
33 Err(Error::TransactionAbortFailed)
34 }
35 }
36
37 pub async fn commit(self) -> Result<()> {
46 let result = self.transaction.commit()?.await?;
47
48 if result.is_committed() {
49 Ok(())
50 } else {
51 Err(Error::TransactioncommitFailed)
52 }
53 }
54
55 pub async fn done(self) -> Result<TransactionResult> {
57 self.transaction.await.map_err(Into::into)
58 }
59
60 pub fn store(&self, store_name: &str) -> Result<Store> {
62 self.transaction
63 .object_store(store_name)
64 .map(|object_store| Store { object_store })
65 .map_err(Into::into)
66 }
67}