Skip to content

Commit

Permalink
Fix bugs and bump crate version
Browse files Browse the repository at this point in the history
  • Loading branch information
devashishdxt committed Jul 4, 2024
1 parent 36e2438 commit 2533d98
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 93 deletions.
12 changes: 9 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rexie"
version = "0.5.0"
version = "0.6.0"
authors = ["Devashish Dixit <[email protected]>"]
license = "MIT/Apache-2.0"
description = "Rexie is an easy-to-use, futures based wrapper around IndexedDB that compiles to webassembly"
Expand All @@ -9,15 +9,21 @@ repository = "https://github.com/devashishdxt/rexie"
categories = ["asynchronous", "database", "wasm", "web-programming"]
keywords = ["wasm", "indexeddb", "futures", "idb", "indexed"]
readme = "README.md"
include = ["Cargo.toml", "src/**/*.rs", "tests/**/*.rs", "README.md"]
include = [
"Cargo.toml",
"src/**/*.rs",
"tests/**/*.rs",
"README.md",
"LICENSE_*",
]
edition = "2021"

[lib]
path = "src/lib.rs"
crate-type = ["cdylib", "rlib"]

[dependencies]
idb = { version = "0.6.1", features = ["builder"] }
idb = { version = "0.6.2", features = ["builder"] }
thiserror = "1.0.61"
wasm-bindgen = "0.2.92"

Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ To use Rexie, you need to add the following to your `Cargo.toml`:

```toml
[dependencies]
rexie = "0.4"
rexie = "0.6"
```

### Example

To create a new database, you can use the `Rexie::builder` method:
To create a new database, you can use the [`Rexie::builder`] method:

```rust
use rexie::*;
Expand All @@ -39,14 +39,14 @@ async fn build_database() -> Result<Rexie> {

// Check basic details of the database
assert_eq!(rexie.name(), "test");
assert_eq!(rexie.version(), 1.0);
assert_eq!(rexie.version(), Ok(1));
assert_eq!(rexie.store_names(), vec!["employees"]);

Ok(rexie)
}
```

To add an employee, you can use the `Store::add` method after creating a `Transaction`:
To add an employee, you can use the [`Store::add`] method after creating a [`Transaction`]:

```rust
use rexie::*;
Expand Down Expand Up @@ -77,7 +77,7 @@ async fn add_employee(rexie: &Rexie, name: &str, email: &str) -> Result<u32> {
}
```

To get an employee, you can use the `Store::get` method after creating a `Transaction`:
To get an employee, you can use the [`Store::get`] method after creating a [`Transaction`]:

```rust
use rexie::*;
Expand All @@ -90,7 +90,7 @@ async fn get_employee(rexie: &Rexie, id: u32) -> Result<Option<serde_json::Value
let employees = transaction.store("employees")?;

// Get the employee
let employee = employees.get(&id.into()).await?;
let employee = employees.get(id.into()).await?.unwrap();

// Convert it to `serde_json::Value` from `JsValue`
let employee: Option<serde_json::Value> = serde_wasm_bindgen::from_value(employee).unwrap();
Expand Down
3 changes: 0 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Error, PartialEq)]
#[non_exhaustive]
pub enum Error {
/// Couldn't open a cursor
#[error("couldn't open a cursor")]
CursorNotFound,
/// Indexed DB error
#[error("idb error")]
IdbError(#[from] idb::Error),
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//!
//! ```toml
//! [dependencies]
//! rexie = "0.4"
//! rexie = "0.6"
//! ```
//!
//! ## Example
Expand Down Expand Up @@ -37,7 +37,7 @@
//!
//! // Check basic details of the database
//! assert_eq!(rexie.name(), "test");
//! assert_eq!(rexie.version(), 1.0);
//! assert_eq!(rexie.version(), Ok(1));
//! assert_eq!(rexie.store_names(), vec!["employees"]);
//!
//! Ok(rexie)
Expand Down Expand Up @@ -88,7 +88,7 @@
//! let employees = transaction.store("employees")?;
//!
//! // Get the employee
//! let employee = employees.get(&id.into()).await?.unwrap();
//! let employee = employees.get(id.into()).await?.unwrap();
//!
//! // Convert it to `serde_json::Value` from `JsValue`
//! let employee: Option<serde_json::Value> = serde_wasm_bindgen::from_value(employee).unwrap();
Expand Down
85 changes: 48 additions & 37 deletions src/transaction/index.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use idb::Index;
use wasm_bindgen::JsValue;

use crate::{Direction, Error, KeyRange, Result};
use crate::{Direction, KeyRange, Result};

/// Index of an object store.
pub struct StoreIndex {
Expand Down Expand Up @@ -62,49 +62,60 @@ impl StoreIndex {
offset: Option<u32>,
direction: Option<Direction>,
) -> Result<Vec<(JsValue, JsValue)>> {
let mut cursor = self
let cursor = self
.index
.open_cursor(key_range.map(Into::into), direction)?
.await?
.ok_or(Error::CursorNotFound)?
.into_managed();

let mut result = Vec::new();

match limit {
Some(limit) => {
if let Some(offset) = offset {
cursor.advance(offset).await?;
}

for _ in 0..limit {
let key = cursor.key()?;
let value = cursor.value()?;

match (key, value) {
(Some(key), Some(value)) => result.push((key, value)),
_ => break,
.await?;

match cursor {
None => Ok(Vec::new()),
Some(cursor) => {
let mut cursor = cursor.into_managed();

let mut result = Vec::new();

match limit {
Some(limit) => {
if let Some(offset) = offset {
cursor.advance(offset).await?;
}

for _ in 0..limit {
let key = cursor.key()?;
let value = cursor.value()?;

match (key, value) {
(Some(key), Some(value)) => {
result.push((key, value));
cursor.next(None).await?;
}
_ => break,
}
}
}
}
}
None => {
if let Some(offset) = offset {
cursor.advance(offset).await?;
}

loop {
let key = cursor.key()?;
let value = cursor.value()?;

match (key, value) {
(Some(key), Some(value)) => result.push((key, value)),
_ => break,
None => {
if let Some(offset) = offset {
cursor.advance(offset).await?;
}

loop {
let key = cursor.key()?;
let value = cursor.value()?;

match (key, value) {
(Some(key), Some(value)) => {
result.push((key, value));
cursor.next(None).await?;
}
_ => break,
}
}
}
}

Ok(result)
}
}

Ok(result)
}

/// Counts the number of key value pairs in the store
Expand Down
85 changes: 48 additions & 37 deletions src/transaction/store.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use idb::ObjectStore;
use wasm_bindgen::JsValue;

use crate::{Direction, Error, KeyPath, KeyRange, Result, StoreIndex};
use crate::{Direction, KeyPath, KeyRange, Result, StoreIndex};

/// An object store.
pub struct Store {
Expand Down Expand Up @@ -90,49 +90,60 @@ impl Store {
offset: Option<u32>,
direction: Option<Direction>,
) -> Result<Vec<(JsValue, JsValue)>> {
let mut cursor = self
let cursor = self
.object_store
.open_cursor(key_range.map(Into::into), direction)?
.await?
.ok_or(Error::CursorNotFound)?
.into_managed();

let mut result = Vec::new();

match limit {
Some(limit) => {
if let Some(offset) = offset {
cursor.advance(offset).await?;
}

for _ in 0..limit {
let key = cursor.key()?;
let value = cursor.value()?;

match (key, value) {
(Some(key), Some(value)) => result.push((key, value)),
_ => break,
.await?;

match cursor {
None => Ok(Vec::new()),
Some(cursor) => {
let mut cursor = cursor.into_managed();

let mut result = Vec::new();

match limit {
Some(limit) => {
if let Some(offset) = offset {
cursor.advance(offset).await?;
}

for _ in 0..limit {
let key = cursor.key()?;
let value = cursor.value()?;

match (key, value) {
(Some(key), Some(value)) => {
result.push((key, value));
cursor.next(None).await?;
}
_ => break,
}
}
}
}
}
None => {
if let Some(offset) = offset {
cursor.advance(offset).await?;
}

loop {
let key = cursor.key()?;
let value = cursor.value()?;

match (key, value) {
(Some(key), Some(value)) => result.push((key, value)),
_ => break,
None => {
if let Some(offset) = offset {
cursor.advance(offset).await?;
}

loop {
let key = cursor.key()?;
let value = cursor.value()?;

match (key, value) {
(Some(key), Some(value)) => {
result.push((key, value));
cursor.next(None).await?;
}
_ => break,
}
}
}
}

Ok(result)
}
}

Ok(result)
}

/// Adds a key value pair in the store. Note that the key can be `None` if store has auto increment enabled.
Expand Down
4 changes: 0 additions & 4 deletions tests/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,6 @@ async fn test_db_duplicate_add_fail() {
// Write a duplicate value (with same email) to the database.
let id = add_employee(&rexie, "John Doe New", "[email protected]").await;
assert!(id.is_err());
let err = id.unwrap_err();
assert!(err
.to_string()
.starts_with("failed to execute indexed db request: ConstraintError"));

close_and_delete_db(rexie).await;
}
Expand Down

0 comments on commit 2533d98

Please sign in to comment.