Skip to content

Commit

Permalink
Replace IdbCursorDirection with custom Direction enum in public API
Browse files Browse the repository at this point in the history
  • Loading branch information
devashishdxt committed Jan 17, 2022
1 parent 248555e commit a95438e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ web-sys = { version = "0.3.55", features = [
"DomStringList",
"Event",
"IdbCursorWithValue",
"IdbCursorDirection",
"IdbCursorDirection",
"IdbDatabase",
"IdbFactory",
"IdbIndex",
Expand Down
21 changes: 21 additions & 0 deletions src/direction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use web_sys::IdbCursorDirection;

/// Direction in which the key-value paris are fetched from the store.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
Next,
NextUnique,
Prev,
PrevUnique,
}

impl From<Direction> for IdbCursorDirection {
fn from(direction: Direction) -> Self {
match direction {
Direction::Next => IdbCursorDirection::Next,
Direction::NextUnique => IdbCursorDirection::Nextunique,
Direction::Prev => IdbCursorDirection::Prev,
Direction::PrevUnique => IdbCursorDirection::Prevunique,
}
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
//! Ok(employee)
//! }
//! ```
mod direction;
mod error;
mod index;
mod key_range;
Expand All @@ -116,6 +117,7 @@ mod utils;
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

pub use self::{
direction::Direction,
error::{Error, Result},
index::Index,
key_range::KeyRange,
Expand Down
10 changes: 5 additions & 5 deletions src/transaction/index.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use wasm_bindgen::JsValue;
use web_sys::{IdbCursorDirection, IdbIndex};
use web_sys::IdbIndex;

use crate::{
request::{wait_cursor_request, wait_request},
Error, KeyRange, Result,
Direction, Error, KeyRange, Result,
};

/// Index of an object store.
Expand Down Expand Up @@ -48,16 +48,16 @@ impl StoreIndex {
key_range: Option<&KeyRange>,
limit: Option<u32>,
offset: Option<u32>,
direction: Option<IdbCursorDirection>,
direction: Option<Direction>,
) -> Result<Vec<(JsValue, JsValue)>> {
let request = match (key_range, direction) {
(Some(key_range), Some(direction)) => self
.idb_index
.open_cursor_with_range_and_direction(key_range.as_ref(), direction),
.open_cursor_with_range_and_direction(key_range.as_ref(), direction.into()),
(Some(key_range), None) => self.idb_index.open_cursor_with_range(key_range.as_ref()),
(None, Some(direction)) => self
.idb_index
.open_cursor_with_range_and_direction(&JsValue::null(), direction),
.open_cursor_with_range_and_direction(&JsValue::null(), direction.into()),
_ => self.idb_index.open_cursor(),
}
.map_err(Error::IndexedDbRequestError)?;
Expand Down
10 changes: 5 additions & 5 deletions src/transaction/store.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use wasm_bindgen::prelude::*;
use web_sys::{IdbCursorDirection, IdbObjectStore};
use web_sys::IdbObjectStore;

use crate::{
request::{wait_cursor_request, wait_request},
Error, KeyRange, Result, StoreIndex,
Direction, Error, KeyRange, Result, StoreIndex,
};

/// An object store.
Expand Down Expand Up @@ -73,16 +73,16 @@ impl Store {
key_range: Option<&KeyRange>,
limit: Option<u32>,
offset: Option<u32>,
direction: Option<IdbCursorDirection>,
direction: Option<Direction>,
) -> Result<Vec<(JsValue, JsValue)>> {
let request = match (key_range, direction) {
(Some(key_range), Some(direction)) => self
.idb_store
.open_cursor_with_range_and_direction(key_range.as_ref(), direction),
.open_cursor_with_range_and_direction(key_range.as_ref(), direction.into()),
(Some(key_range), None) => self.idb_store.open_cursor_with_range(key_range.as_ref()),
(None, Some(direction)) => self
.idb_store
.open_cursor_with_range_and_direction(&JsValue::null(), direction),
.open_cursor_with_range_and_direction(&JsValue::null(), direction.into()),
_ => self.idb_store.open_cursor(),
}
.map_err(Error::IndexedDbRequestError)?;
Expand Down
12 changes: 4 additions & 8 deletions tests/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ extern crate wasm_bindgen_test;

use std::{assert, assert_eq, option::Option};

use rexie::{Index, KeyRange, ObjectStore, Result, Rexie, TransactionMode};
use rexie::{Direction, Index, KeyRange, ObjectStore, Result, Rexie, TransactionMode};
use serde::{Deserialize, Serialize};
use wasm_bindgen::JsValue;
use wasm_bindgen_test::*;
use web_sys::IdbCursorDirection;

wasm_bindgen_test_configure!(run_in_browser);

Expand Down Expand Up @@ -117,10 +116,7 @@ async fn get_employee(rexie: &Rexie, id: u32) -> Result<Option<Employee>> {
Ok(employee)
}

async fn get_all_employees(
rexie: &Rexie,
direction: Option<IdbCursorDirection>,
) -> Result<Vec<Employee>> {
async fn get_all_employees(rexie: &Rexie, direction: Option<Direction>) -> Result<Vec<Employee>> {
let transaction = rexie.transaction(&["employees"], TransactionMode::ReadOnly);
assert!(transaction.is_ok());
let transaction = transaction.unwrap();
Expand Down Expand Up @@ -298,10 +294,10 @@ async fn test_get_all_pass() {
assert_eq!(employees.len(), 2);

// Test reversed
let asc_employees = get_all_employees(&rexie, Some(IdbCursorDirection::Next)).await;
let asc_employees = get_all_employees(&rexie, Some(Direction::Next)).await;
assert!(asc_employees.is_ok());
let asc_employees = asc_employees.unwrap();
let desc_employees = get_all_employees(&rexie, Some(IdbCursorDirection::Prev)).await;
let desc_employees = get_all_employees(&rexie, Some(Direction::Prev)).await;
assert!(desc_employees.is_ok());
let desc_employees = desc_employees.unwrap();
assert_eq!(desc_employees[0], asc_employees[1]);
Expand Down

0 comments on commit a95438e

Please sign in to comment.