-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bulk operations on object store (#46)
* Add bulk operations on object store * remove unncessary import
- Loading branch information
1 parent
9b683b6
commit d13651e
Showing
2 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()); | ||
|
@@ -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; | ||
} |