11use crate :: {
22 transaction:: transaction_request,
33 utils:: {
4- array_to_vec, make_key_range_from_slice, map_count_err, map_count_res, map_get_err,
5- none_if_undefined, slice_to_array,
4+ array_to_vec, make_key_range, map_count_err, map_count_res, map_get_err, none_if_undefined,
65 } ,
76} ;
87use futures_util:: future:: { Either , FutureExt } ;
@@ -11,6 +10,10 @@ use web_sys::{wasm_bindgen::JsValue, IdbIndex};
1110
1211/// Wrapper for [`IDBIndex`](https://developer.mozilla.org/en-US/docs/Web/API/IDBIndex),
1312/// for use in transactions
13+ ///
14+ /// Most of the functions here take a [`JsValue`] as the key(s) to use in the index. If the index was
15+ /// built with a compound key, then you should use eg. `js_sys::Array::from_iter([key_1, key_2])` as
16+ /// the key.
1417pub struct Index < Err > {
1518 sys : IdbIndex ,
1619 _phantom : PhantomData < Err > ,
@@ -27,8 +30,8 @@ impl<Err> Index<Err> {
2730 /// Checks whether the provided key (for this index) already exists
2831 ///
2932 /// Internally, this uses [`IDBIndex::count`](https://developer.mozilla.org/en-US/docs/Web/API/IDBIndex/count).
30- pub fn contains ( & self , key : & [ & JsValue ] ) -> impl Future < Output = crate :: Result < bool , Err > > {
31- match self . sys . count_with_key ( & slice_to_array ( key) ) {
33+ pub fn contains ( & self , key : & JsValue ) -> impl Future < Output = crate :: Result < bool , Err > > {
34+ match self . sys . count_with_key ( key) {
3235 Ok ( count_req) => Either :: Right (
3336 transaction_request ( count_req) . map ( |res| res. map ( map_count_res) . map ( |n| n != 0 ) ) ,
3437 ) ,
@@ -39,11 +42,11 @@ impl<Err> Index<Err> {
3942 /// Count all the keys (for this index) in the provided range
4043 ///
4144 /// Internally, this uses [`IDBIndex::count`](https://developer.mozilla.org/en-US/docs/Web/API/IDBIndex/count).
42- pub fn count_in < ' a > (
45+ pub fn count_in (
4346 & self ,
44- range : impl RangeBounds < [ & ' a JsValue ] > ,
47+ range : impl RangeBounds < JsValue > ,
4548 ) -> impl Future < Output = crate :: Result < usize , Err > > {
46- let range = match make_key_range_from_slice ( range) {
49+ let range = match make_key_range ( range) {
4750 Ok ( range) => range,
4851 Err ( e) => return Either :: Left ( std:: future:: ready ( Err ( e) ) ) ,
4952 } ;
@@ -58,11 +61,8 @@ impl<Err> Index<Err> {
5861 /// Get the object with key `key` for this index
5962 ///
6063 /// Internally, this uses [`IDBIndex::get`](https://developer.mozilla.org/en-US/docs/Web/API/IDBIndex/get).
61- pub fn get (
62- & self ,
63- key : & [ & JsValue ] ,
64- ) -> impl Future < Output = crate :: Result < Option < JsValue > , Err > > {
65- match self . sys . get ( & slice_to_array ( key) ) {
64+ pub fn get ( & self , key : & JsValue ) -> impl Future < Output = crate :: Result < Option < JsValue > , Err > > {
65+ match self . sys . get ( key) {
6666 Ok ( get_req) => {
6767 Either :: Right ( transaction_request ( get_req) . map ( |res| res. map ( none_if_undefined) ) )
6868 }
@@ -75,11 +75,11 @@ impl<Err> Index<Err> {
7575 /// Note that the unbounded range is not a valid range for IndexedDB.
7676 ///
7777 /// Internally, this uses [`IDBIndex::get`](https://developer.mozilla.org/en-US/docs/Web/API/IDBIndex/get).
78- pub fn get_first_in < ' a > (
78+ pub fn get_first_in (
7979 & self ,
80- range : impl RangeBounds < [ & ' a JsValue ] > ,
80+ range : impl RangeBounds < JsValue > ,
8181 ) -> impl Future < Output = crate :: Result < Option < JsValue > , Err > > {
82- let range = match make_key_range_from_slice ( range) {
82+ let range = match make_key_range ( range) {
8383 Ok ( range) => range,
8484 Err ( e) => return Either :: Left ( std:: future:: ready ( Err ( e) ) ) ,
8585 } ;
@@ -116,12 +116,12 @@ impl<Err> Index<Err> {
116116 /// results of `limit`, ordered by this index
117117 ///
118118 /// Internally, this uses [`IDBIndex::getAll`](https://developer.mozilla.org/en-US/docs/Web/API/IDBIndex/getAll).
119- pub fn get_all_in < ' a > (
119+ pub fn get_all_in (
120120 & self ,
121- range : impl RangeBounds < [ & ' a JsValue ] > ,
121+ range : impl RangeBounds < JsValue > ,
122122 limit : Option < u32 > ,
123123 ) -> impl Future < Output = crate :: Result < Vec < JsValue > , Err > > {
124- let range = match make_key_range_from_slice ( range) {
124+ let range = match make_key_range ( range) {
125125 Ok ( range) => range,
126126 Err ( e) => return Either :: Left ( std:: future:: ready ( Err ( e) ) ) ,
127127 } ;
@@ -140,11 +140,11 @@ impl<Err> Index<Err> {
140140 /// Get the first existing key (for this index) in the provided range
141141 ///
142142 /// Internally, this uses [`IDBIndex::getKey`](https://developer.mozilla.org/en-US/docs/Web/API/IDBIndex/getKey).
143- pub fn get_first_key_in < ' a > (
143+ pub fn get_first_key_in (
144144 & self ,
145- range : impl RangeBounds < [ & ' a JsValue ] > ,
145+ range : impl RangeBounds < JsValue > ,
146146 ) -> impl Future < Output = crate :: Result < Option < JsValue > , Err > > {
147- let range = match make_key_range_from_slice ( range) {
147+ let range = match make_key_range ( range) {
148148 Ok ( range) => range,
149149 Err ( e) => return Either :: Left ( std:: future:: ready ( Err ( e) ) ) ,
150150 } ;
@@ -181,12 +181,12 @@ impl<Err> Index<Err> {
181181 /// ordered by this index
182182 ///
183183 /// Internally, this uses [`IDBIndex::getAllKeys`](https://developer.mozilla.org/en-US/docs/Web/API/IDBIndex/getAllKeys).
184- pub fn get_all_keys_in < ' a > (
184+ pub fn get_all_keys_in (
185185 & self ,
186- range : impl RangeBounds < [ & ' a JsValue ] > ,
186+ range : impl RangeBounds < JsValue > ,
187187 limit : Option < u32 > ,
188188 ) -> impl Future < Output = crate :: Result < Vec < JsValue > , Err > > {
189- let range = match make_key_range_from_slice ( range) {
189+ let range = match make_key_range ( range) {
190190 Ok ( range) => range,
191191 Err ( e) => return Either :: Left ( std:: future:: ready ( Err ( e) ) ) ,
192192 } ;
0 commit comments