Skip to content

Commit

Permalink
Reorganize array key_path code to fix errors found in testing with a …
Browse files Browse the repository at this point in the history
…real-world app (#12)

* removed unncessary match statement

* create keypath enum to separate logic for single and array key paths
  • Loading branch information
devashishdxt authored Apr 12, 2022
1 parent 47b1256 commit ac023a3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 36 deletions.
17 changes: 5 additions & 12 deletions src/index.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use js_sys::Array;
use wasm_bindgen::JsValue;
use web_sys::{IdbIndexParameters, IdbObjectStore};

use crate::{Error, Result};
use crate::{key_path::KeyPath, Error, Result};

/// An index builder.
pub struct Index {
pub(crate) name: String,
pub(crate) key_path: Vec<String>,
pub(crate) key_path: KeyPath,
pub(crate) unique: Option<bool>,
pub(crate) multi_entry: Option<bool>,
}
Expand All @@ -17,7 +15,7 @@ impl Index {
pub fn new(name: &str, key_path: &str) -> Self {
Self {
name: name.to_owned(),
key_path: vec![key_path.to_owned()],
key_path: KeyPath::new_str(key_path),
unique: None,
multi_entry: None,
}
Expand All @@ -27,7 +25,7 @@ impl Index {
pub fn new_array<'a>(name: &str, key_path_array: impl IntoIterator<Item = &'a str>) -> Self {
Self {
name: name.to_owned(),
key_path: key_path_array.into_iter().map(ToOwned::to_owned).collect(),
key_path: KeyPath::new_array(key_path_array),
unique: None,
multi_entry: None,
}
Expand Down Expand Up @@ -62,12 +60,7 @@ impl Index {
object_store
.create_index_with_str_sequence_and_optional_parameters(
&self.name,
&self
.key_path
.into_iter()
.map(|s| JsValue::from_str(&s))
.collect::<Array>()
.into(),
&self.key_path.into(),
&params,
)
.map_err(Error::IndexCreationFailed)?;
Expand Down
32 changes: 32 additions & 0 deletions src/key_path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use js_sys::Array;
use wasm_bindgen::prelude::*;

pub(crate) enum KeyPath {
String(String),
Array(Vec<String>),
}

impl KeyPath {
pub fn new_str(key_path: &str) -> Self {
Self::String(key_path.to_owned())
}

pub fn new_array<'a>(key_path_array: impl IntoIterator<Item = &'a str>) -> Self {
Self::Array(key_path_array.into_iter().map(ToOwned::to_owned).collect())
}
}

impl From<KeyPath> for JsValue {
fn from(key_path: KeyPath) -> Self {
match key_path {
KeyPath::String(key_path) => JsValue::from_str(&key_path),
KeyPath::Array(key_path_array) => {
let key_path = key_path_array
.iter()
.map(|s| JsValue::from_str(s))
.collect::<Array>();
key_path.into()
}
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
mod direction;
mod error;
mod index;
mod key_path;
mod key_range;
mod object_store;
mod request;
Expand Down
31 changes: 7 additions & 24 deletions src/object_store.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use std::collections::HashSet;

use js_sys::Array;
use wasm_bindgen::prelude::*;
use web_sys::{IdbDatabase, IdbObjectStoreParameters, IdbOpenDbRequest};

use crate::{Error, Index, Result};
use crate::{key_path::KeyPath, Error, Index, Result};

/// An object store builder.
pub struct ObjectStore {
pub(crate) name: String,
pub(crate) key_path: Vec<String>,
pub(crate) key_path: Option<KeyPath>,
pub(crate) auto_increment: Option<bool>,
pub(crate) indexes: Vec<Index>,
}
Expand All @@ -19,21 +17,21 @@ impl ObjectStore {
pub fn new(name: &str) -> Self {
Self {
name: name.to_owned(),
key_path: Vec::new(),
key_path: None,
auto_increment: None,
indexes: Vec::new(),
}
}

/// Specify key path for the object store
pub fn key_path(mut self, key_path: &str) -> Self {
self.key_path = vec![key_path.to_owned()];
self.key_path = Some(KeyPath::new_str(key_path));
self
}

/// Specify key path array for the object store
pub fn key_path_array<'a>(mut self, key_path_array: impl IntoIterator<Item = &'a str>) -> Self {
self.key_path = key_path_array.into_iter().map(ToOwned::to_owned).collect();
self.key_path = Some(KeyPath::new_array(key_path_array));
self
}

Expand Down Expand Up @@ -73,23 +71,8 @@ impl ObjectStore {
params.auto_increment(auto_increment);
}

match self.key_path.len() {
0 => {
params.key_path(None);
}
1 => {
params.key_path(Some(&(&self.key_path[0]).into()));
}
_ => {
params.key_path(Some(
&self
.key_path
.into_iter()
.map(|key| JsValue::from_str(&key))
.collect::<Array>()
.into(),
));
}
if let Some(key_path) = self.key_path {
params.key_path(Some(&key_path.into()));
}

idb.create_object_store_with_optional_parameters(&self.name, &params)
Expand Down

0 comments on commit ac023a3

Please sign in to comment.