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 bulk operations on object store #46

Merged
merged 2 commits into from
Jul 4, 2024
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
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", "john@example.com"),
("Scooby Doo", "scooby@example.com"),
]
.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;
}
Loading