Skip to content

Commit 42211d7

Browse files
committed
expose all cursor functions as expected
1 parent 0c37de1 commit 42211d7

File tree

4 files changed

+100
-64
lines changed

4 files changed

+100
-64
lines changed

src/cursor.rs

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
use crate::{
22
transaction::transaction_request,
33
utils::{
4-
map_cursor_advance_err, map_cursor_advance_until_err,
4+
make_key_range, map_cursor_advance_err, map_cursor_advance_until_err,
55
map_cursor_advance_until_primary_key_err, map_cursor_delete_err, map_cursor_update_err,
6+
map_open_cursor_err,
67
},
78
};
8-
use std::marker::PhantomData;
9+
use futures_util::future::Either;
10+
use std::{future::Future, marker::PhantomData, ops::RangeBounds};
911
use web_sys::{
1012
wasm_bindgen::{JsCast, JsValue},
11-
IdbCursor, IdbCursorDirection, IdbCursorWithValue, IdbRequest,
13+
IdbCursor, IdbCursorDirection, IdbCursorWithValue, IdbIndex, IdbObjectStore, IdbRequest,
1214
};
1315

1416
#[cfg(doc)]
15-
use crate::Index;
17+
use crate::{Index, ObjectStore};
1618
#[cfg(doc)]
1719
use web_sys::js_sys::Array;
1820

@@ -42,6 +44,86 @@ impl CursorDirection {
4244
}
4345
}
4446

47+
/// Helper to build cursors over [`ObjectStore`]s
48+
pub struct CursorBuilder<Err> {
49+
source: Either<IdbObjectStore, IdbIndex>,
50+
query: JsValue,
51+
direction: IdbCursorDirection,
52+
_phantom: PhantomData<Err>,
53+
}
54+
55+
impl<Err> CursorBuilder<Err> {
56+
pub(crate) fn from_store(store: IdbObjectStore) -> CursorBuilder<Err> {
57+
CursorBuilder {
58+
source: Either::Left(store),
59+
query: JsValue::UNDEFINED,
60+
direction: IdbCursorDirection::Next,
61+
_phantom: PhantomData,
62+
}
63+
}
64+
65+
pub(crate) fn from_index(index: IdbIndex) -> CursorBuilder<Err> {
66+
CursorBuilder {
67+
source: Either::Right(index),
68+
query: JsValue::UNDEFINED,
69+
direction: IdbCursorDirection::Next,
70+
_phantom: PhantomData,
71+
}
72+
}
73+
74+
/// Open the cursor
75+
///
76+
/// Internally, this uses [`IDBObjectStore::openCursor`](https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/openCursor).
77+
pub fn open(self) -> impl Future<Output = crate::Result<Cursor<Err>, Err>> {
78+
let req = match self.source {
79+
Either::Left(store) => {
80+
store.open_cursor_with_range_and_direction(&self.query, self.direction)
81+
}
82+
Either::Right(index) => {
83+
index.open_cursor_with_range_and_direction(&self.query, self.direction)
84+
}
85+
};
86+
match req {
87+
Ok(open_req) => Either::Right(Cursor::from(open_req)),
88+
Err(err) => Either::Left(std::future::ready(Err(map_open_cursor_err(err)))),
89+
}
90+
}
91+
92+
/// Open the cursor as a key-only cursor
93+
///
94+
/// Internally, this uses [`IDBObjectStore::openKeyCursor`](https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/openKeyCursor).
95+
pub fn open_key(self) -> impl Future<Output = crate::Result<Cursor<Err>, Err>> {
96+
let req = match self.source {
97+
Either::Left(store) => {
98+
store.open_key_cursor_with_range_and_direction(&self.query, self.direction)
99+
}
100+
Either::Right(index) => {
101+
index.open_key_cursor_with_range_and_direction(&self.query, self.direction)
102+
}
103+
};
104+
match req {
105+
Ok(open_req) => Either::Right(Cursor::from(open_req)),
106+
Err(err) => Either::Left(std::future::ready(Err(map_open_cursor_err(err)))),
107+
}
108+
}
109+
110+
/// Limit the range of the cursor
111+
///
112+
/// Internally, this sets [this property](https://developer.mozilla.org/en-US/docs/Web/API/IDBIndex/openCursor#range).
113+
pub fn range(mut self, range: impl RangeBounds<JsValue>) -> crate::Result<Self, Err> {
114+
self.query = make_key_range(range)?;
115+
Ok(self)
116+
}
117+
118+
/// Define the direction of the cursor
119+
///
120+
/// Internally, this sets [this property](https://developer.mozilla.org/en-US/docs/Web/API/IDBIndex/openCursor#direction).
121+
pub fn direction(mut self, direction: CursorDirection) -> Self {
122+
self.direction = direction.to_sys();
123+
self
124+
}
125+
}
126+
45127
/// Wrapper for [`IDBCursorWithValue`](https://developer.mozilla.org/en-US/docs/Web/API/IDBCursorWithValue)
46128
pub struct Cursor<Err> {
47129
sys: Option<IdbCursor>,

src/index.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ use crate::{
33
utils::{
44
array_to_vec, make_key_range, map_count_err, map_count_res, map_get_err, none_if_undefined,
55
},
6+
CursorBuilder,
67
};
78
use futures_util::future::{Either, FutureExt};
89
use std::{future::Future, marker::PhantomData, ops::RangeBounds};
910
use web_sys::{wasm_bindgen::JsValue, IdbIndex};
1011

12+
#[cfg(doc)]
13+
use crate::Cursor;
14+
1115
/// Wrapper for [`IDBIndex`](https://developer.mozilla.org/en-US/docs/Web/API/IDBIndex),
1216
/// for use in transactions
1317
///
@@ -202,6 +206,8 @@ impl<Err> Index<Err> {
202206
}
203207
}
204208

205-
// TODO: openCursor
206-
// TODO: openKeyCursor
209+
/// Open a [`Cursor`] on this index
210+
pub fn cursor(&self) -> CursorBuilder<Err> {
211+
CursorBuilder::from_index(self.sys.clone())
212+
}
207213
}

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ mod object_store;
1717
mod transaction;
1818
mod utils;
1919

20-
pub use cursor::{Cursor, CursorDirection};
20+
pub use cursor::{Cursor, CursorBuilder, CursorDirection};
2121
pub use database::{Database, ObjectStoreBuilder};
2222
pub use error::{Error, Result};
2323
pub use factory::{Factory, VersionChangeEvent};
2424
pub use index::Index;
25-
pub use object_store::{CursorBuilder as ObjectStoreCursorBuilder, IndexBuilder, ObjectStore};
25+
pub use object_store::{IndexBuilder, ObjectStore};
2626
pub use transaction::{Transaction, TransactionBuilder};

src/object_store.rs

Lines changed: 4 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
use crate::{
2-
cursor::CursorDirection,
32
transaction::transaction_request,
43
utils::{
54
array_to_vec, make_key_range, map_add_err, map_count_err, map_count_res, map_delete_err,
6-
map_get_err, map_open_cursor_err, none_if_undefined, str_slice_to_array,
5+
map_get_err, none_if_undefined, str_slice_to_array,
76
},
8-
Cursor, Index,
7+
CursorBuilder, Index,
98
};
109
use futures_util::future::{Either, FutureExt};
1110
use std::{future::Future, marker::PhantomData, ops::RangeBounds};
12-
use web_sys::{
13-
js_sys::JsString, wasm_bindgen::JsValue, IdbCursorDirection, IdbIndexParameters, IdbObjectStore,
14-
};
11+
use web_sys::{js_sys::JsString, wasm_bindgen::JsValue, IdbIndexParameters, IdbObjectStore};
1512

1613
/// Wrapper for [`IDBObjectStore`](https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore),
1714
/// for use in transactions
@@ -404,7 +401,7 @@ impl<Err> ObjectStore<Err> {
404401

405402
/// Open a [`Cursor`] on this object store
406403
pub fn cursor(&self) -> CursorBuilder<Err> {
407-
CursorBuilder::from(self.sys.clone())
404+
CursorBuilder::from_store(self.sys.clone())
408405
}
409406
}
410407

@@ -454,52 +451,3 @@ impl<'a, Err> IndexBuilder<'a, Err> {
454451
self
455452
}
456453
}
457-
458-
/// Helper to build cursors over [`ObjectStore`]s
459-
pub struct CursorBuilder<Err> {
460-
store: IdbObjectStore,
461-
query: JsValue,
462-
direction: IdbCursorDirection,
463-
_phantom: PhantomData<Err>,
464-
}
465-
466-
impl<Err> CursorBuilder<Err> {
467-
pub(crate) fn from(store: IdbObjectStore) -> CursorBuilder<Err> {
468-
CursorBuilder {
469-
store,
470-
query: JsValue::UNDEFINED,
471-
direction: IdbCursorDirection::Next,
472-
_phantom: PhantomData,
473-
}
474-
}
475-
476-
/// Open the cursor
477-
///
478-
/// Internally, this uses [`IDBObjectStore::openCursor`](https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/openCursor).
479-
pub fn open(self) -> impl Future<Output = crate::Result<Cursor<Err>, Err>> {
480-
match self
481-
.store
482-
.open_cursor_with_range_and_direction(&self.query, self.direction)
483-
{
484-
Ok(open_req) => Either::Right(Cursor::from(open_req)),
485-
Err(err) => Either::Left(std::future::ready(Err(map_open_cursor_err(err)))),
486-
}
487-
}
488-
// TODO: implement `open_key`
489-
490-
/// Limit the range of the cursor
491-
///
492-
/// Internally, this sets [this property](https://developer.mozilla.org/en-US/docs/Web/API/IDBIndex/openCursor#range).
493-
pub fn range(mut self, range: impl RangeBounds<JsValue>) -> crate::Result<Self, Err> {
494-
self.query = make_key_range(range)?;
495-
Ok(self)
496-
}
497-
498-
/// Define the direction of the cursor
499-
///
500-
/// Internally, this sets [this property](https://developer.mozilla.org/en-US/docs/Web/API/IDBIndex/openCursor#direction).
501-
pub fn direction(mut self, direction: CursorDirection) -> Self {
502-
self.direction = direction.to_sys();
503-
self
504-
}
505-
}

0 commit comments

Comments
 (0)