idb_sys/object_store/
object_store_params.rs

1use wasm_bindgen::{JsCast, JsValue};
2use web_sys::IdbObjectStoreParameters;
3
4use crate::{Error, KeyPath};
5
6/// Options when creating an [`ObjectStore`](crate::ObjectStore).
7#[derive(Debug, Default, Clone, PartialEq, Eq)]
8pub struct ObjectStoreParams {
9    inner: IdbObjectStoreParameters,
10}
11
12impl ObjectStoreParams {
13    /// Creates an new instance of [`ObjectStoreParams`].
14    pub fn new() -> Self {
15        Default::default()
16    }
17
18    /// Sets the `auto_increment` flag.
19    pub fn auto_increment(&mut self, auto_increment: bool) -> &mut Self {
20        self.inner.auto_increment(auto_increment);
21        self
22    }
23
24    /// Sets the key path.
25    pub fn key_path(&mut self, key_path: Option<KeyPath>) -> &mut Self {
26        self.inner.key_path(key_path.map(Into::into).as_ref());
27        self
28    }
29}
30
31impl From<IdbObjectStoreParameters> for ObjectStoreParams {
32    fn from(inner: IdbObjectStoreParameters) -> Self {
33        Self { inner }
34    }
35}
36
37impl From<ObjectStoreParams> for IdbObjectStoreParameters {
38    fn from(params: ObjectStoreParams) -> Self {
39        params.inner
40    }
41}
42
43impl TryFrom<JsValue> for ObjectStoreParams {
44    type Error = Error;
45
46    fn try_from(value: JsValue) -> Result<Self, Self::Error> {
47        value
48            .dyn_into::<IdbObjectStoreParameters>()
49            .map(Into::into)
50            .map_err(|value| Error::UnexpectedJsType("IdbObjectStoreParameters", value))
51    }
52}
53
54impl From<ObjectStoreParams> for JsValue {
55    fn from(value: ObjectStoreParams) -> Self {
56        value.inner.into()
57    }
58}