idb_sys/transaction/
transaction_mode.rs

1use wasm_bindgen::JsValue;
2use web_sys::IdbTransactionMode;
3
4use crate::Error;
5
6/// Specifies the transaction mode.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum TransactionMode {
9    /// The transaction is only allowed to read data.
10    ReadOnly,
11    /// The transaction is allowed to read, modify and delete data from existing object stores.
12    ReadWrite,
13    /// The transaction is allowed to read, modify and delete data from existing object stores, and can also create and
14    /// remove object stores and indexes. This type of transaction can’t be manually created, but instead is created
15    /// automatically when an `upgradeneeded` event is fired.
16    VersionChange,
17}
18
19impl TryFrom<IdbTransactionMode> for TransactionMode {
20    type Error = Error;
21
22    fn try_from(value: IdbTransactionMode) -> Result<Self, Self::Error> {
23        match value {
24            IdbTransactionMode::Readonly => Ok(TransactionMode::ReadOnly),
25            IdbTransactionMode::Readwrite => Ok(TransactionMode::ReadWrite),
26            IdbTransactionMode::Versionchange => Ok(TransactionMode::VersionChange),
27            _ => Err(Error::InvalidTransactionMode),
28        }
29    }
30}
31
32impl From<TransactionMode> for IdbTransactionMode {
33    fn from(value: TransactionMode) -> Self {
34        match value {
35            TransactionMode::ReadOnly => IdbTransactionMode::Readonly,
36            TransactionMode::ReadWrite => IdbTransactionMode::Readwrite,
37            TransactionMode::VersionChange => IdbTransactionMode::Versionchange,
38        }
39    }
40}
41
42impl TryFrom<JsValue> for TransactionMode {
43    type Error = Error;
44
45    fn try_from(value: JsValue) -> Result<Self, Self::Error> {
46        IdbTransactionMode::from_js_value(&value)
47            .ok_or(Error::InvalidCursorDirection)?
48            .try_into()
49    }
50}
51
52impl From<TransactionMode> for JsValue {
53    fn from(direction: TransactionMode) -> Self {
54        let inner: IdbTransactionMode = direction.into();
55        inner.into()
56    }
57}