Skip to content

Commit

Permalink
Add bulk operations on object store (#46)
Browse files Browse the repository at this point in the history
* Add bulk operations on object store

* remove unncessary import
  • Loading branch information
devashishdxt authored Jul 4, 2024
1 parent 9b683b6 commit d13651e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/transaction/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,50 @@ impl Store {
self.object_store.add(value, key)?.await.map_err(Into::into)
}

/// Puts (adds or updates) a key value pair in the store.
/// Adds all key value pairs (`(value, Option<key>)`) in the store. Note that the keys can be `None` if store has
/// auto increment enabled.
pub async fn add_all(
&self,
iter: impl Iterator<Item = (JsValue, Option<JsValue>)>,
) -> Result<()> {
let mut request = None;

for (value, key) in iter {
request = Some(self.object_store.add(value.as_ref(), key.as_ref())?);
}

if let Some(request) = request {
request.await.map(|_| ()).map_err(Into::into)
} else {
Ok(())
}
}

/// Puts (adds or updates) a key value pair in the store. Note that the keys can be `None` if store has auto
/// increment enabled.
pub async fn put(&self, value: &JsValue, key: Option<&JsValue>) -> Result<JsValue> {
self.object_store.put(value, key)?.await.map_err(Into::into)
}

/// Puts (adds or updates) a key value pairs (`(value, Option<key>)`) in the store. Note that the keys can be `None`
/// if store has auto increment enabled.
pub async fn put_all(
&self,
iter: impl Iterator<Item = (JsValue, Option<JsValue>)>,
) -> Result<()> {
let mut request = None;

for (value, key) in iter {
request = Some(self.object_store.put(value.as_ref(), key.as_ref())?);
}

if let Some(request) = request {
request.await.map(|_| ()).map_err(Into::into)
} else {
Ok(())
}
}

/// Deletes a key value pair from the store
pub async fn delete(&self, key: JsValue) -> Result<()> {
self.object_store.delete(key)?.await.map_err(Into::into)
Expand Down
45 changes: 45 additions & 0 deletions tests/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,27 @@ async fn add_employee(rexie: &Rexie, name: &str, email: &str) -> Result<u32> {
Ok(num_traits::cast(employee_id.as_f64().unwrap()).unwrap())
}

async fn add_all_employees(rexie: &Rexie, iter: impl Iterator<Item = (&str, &str)>) -> Result<()> {
let transaction = rexie.transaction(&["employees"], TransactionMode::ReadWrite);
assert!(transaction.is_ok());
let transaction = transaction.unwrap();

let employees = transaction.store("employees");
assert!(employees.is_ok());
let employees = employees.unwrap();

let requests = iter.map(|(name, email)| {
let request = EmployeeRequest { name, email };
let request = serde_wasm_bindgen::to_value(&request).unwrap();
(request, None)
});

employees.add_all(requests).await?;

transaction.commit().await?;
Ok(())
}

async fn get_employee(rexie: &Rexie, id: u32) -> Result<Option<Employee>> {
let transaction = rexie.transaction(&["employees"], TransactionMode::ReadOnly);
assert!(transaction.is_ok());
Expand Down Expand Up @@ -479,3 +500,27 @@ async fn check_transaction_abort() {

close_and_delete_db(rexie).await;
}

#[wasm_bindgen_test]
async fn test_add_all_pass() {
let rexie = create_db().await;

// Write values to the database.
add_all_employees(
&rexie,
vec![
("John Doe", "[email protected]"),
("Scooby Doo", "[email protected]"),
]
.into_iter(),
)
.await
.unwrap();

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

close_and_delete_db(rexie).await;
}

0 comments on commit d13651e

Please sign in to comment.