Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add direction to get_all api calls #1

Merged
merged 1 commit into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ web-sys = { version = "0.3.55", features = [
"DomStringList",
"Event",
"IdbCursorWithValue",
"IdbCursorDirection",
"IdbDatabase",
"IdbFactory",
"IdbIndex",
Expand Down
17 changes: 12 additions & 5 deletions src/transaction/index.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use wasm_bindgen::JsValue;
use web_sys::IdbIndex;
use web_sys::{IdbCursorDirection, IdbIndex};

use crate::{
request::{wait_cursor_request, wait_request},
Expand Down Expand Up @@ -42,16 +42,23 @@ impl StoreIndex {
wait_request(request, Error::IndexedDbRequestError).await
}

/// Gets all key-value pairs from the store with given key range, limit and offset
/// Gets all key-value pairs from the store with given key range, limit, offset and direction
pub async fn get_all(
&self,
key_range: Option<&KeyRange>,
limit: Option<u32>,
offset: Option<u32>,
direction: Option<IdbCursorDirection>,
) -> Result<Vec<(JsValue, JsValue)>> {
let request = match key_range {
Some(key_range) => self.idb_index.open_cursor_with_range(key_range.as_ref()),
None => self.idb_index.open_cursor(),
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),
(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),
_ => self.idb_index.open_cursor(),
}
.map_err(Error::IndexedDbRequestError)?;

Expand Down
17 changes: 12 additions & 5 deletions src/transaction/store.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use wasm_bindgen::prelude::*;
use web_sys::IdbObjectStore;
use web_sys::{IdbCursorDirection, IdbObjectStore};

use crate::{
request::{wait_cursor_request, wait_request},
Expand Down Expand Up @@ -67,16 +67,23 @@ impl Store {
wait_request(request, Error::IndexedDbRequestError).await
}

/// Gets all key-value pairs from the store with given key range, limit and offset
/// Gets all key-value pairs from the store with given key range, limit, offset and direction
pub async fn get_all(
&self,
key_range: Option<&KeyRange>,
limit: Option<u32>,
offset: Option<u32>,
direction: Option<IdbCursorDirection>,
) -> Result<Vec<(JsValue, JsValue)>> {
let request = match key_range {
Some(key_range) => self.idb_store.open_cursor_with_range(key_range.as_ref()),
None => self.idb_store.open_cursor(),
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),
(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),
_ => self.idb_store.open_cursor(),
}
.map_err(Error::IndexedDbRequestError)?;

Expand Down
23 changes: 18 additions & 5 deletions tests/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ use rexie::{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);

#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, PartialEq)]
struct Employee {
id: u32,
name: String,
Expand Down Expand Up @@ -116,7 +117,10 @@ async fn get_employee(rexie: &Rexie, id: u32) -> Result<Option<Employee>> {
Ok(employee)
}

async fn get_all_employees(rexie: &Rexie) -> Result<Vec<Employee>> {
async fn get_all_employees(
rexie: &Rexie,
direction: Option<IdbCursorDirection>,
) -> Result<Vec<Employee>> {
let transaction = rexie.transaction(&["employees"], TransactionMode::ReadOnly);
assert!(transaction.is_ok());
let transaction = transaction.unwrap();
Expand All @@ -126,7 +130,7 @@ async fn get_all_employees(rexie: &Rexie) -> Result<Vec<Employee>> {
let employees = employees.unwrap();

let employees: Vec<JsValue> = employees
.get_all(None, None, None)
.get_all(None, None, None, direction)
.await?
.into_iter()
.map(|pair| pair.1)
Expand Down Expand Up @@ -288,12 +292,21 @@ async fn test_get_all_pass() {
let id2 = add_employee(&rexie, "Scooby Doo", "scooby@example.com").await;
assert_eq!(id2, Ok(2));

let employees = get_all_employees(&rexie).await;
let employees = get_all_employees(&rexie, None).await;
assert!(employees.is_ok());
let employees = employees.unwrap();

assert_eq!(employees.len(), 2);

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

// TODO: check employee details

close_and_delete_db(rexie).await;
Expand Down