rexie/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
//! Rexie is an easy-to-use, futures based wrapper around IndexedDB that compiles to webassembly.
//!
//! # Usage
//!
//! To use Rexie, you need to add the following to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! rexie = "0.6"
//! ```
//!
//! ## Example
//!
//! To create a new database, you can use the [`Rexie::builder`] method:
//!
//! ```rust
//! use rexie::*;
//!
//! async fn build_database() -> Result<Rexie> {
//! // Create a new database
//! let rexie = Rexie::builder("test")
//! // Set the version of the database to 1.0
//! .version(1)
//! // Add an object store named `employees`
//! .add_object_store(
//! ObjectStore::new("employees")
//! // Set the key path to `id`
//! .key_path("id")
//! // Enable auto increment
//! .auto_increment(true)
//! // Add an index named `email` with the key path `email` with unique enabled
//! .add_index(Index::new("email", "email").unique(true)),
//! )
//! // Build the database
//! .build()
//! .await?;
//!
//! // Check basic details of the database
//! assert_eq!(rexie.name(), "test");
//! assert_eq!(rexie.version(), Ok(1));
//! assert_eq!(rexie.store_names(), vec!["employees"]);
//!
//! Ok(rexie)
//! }
//! ```
//!
//! To add an employee, you can use the [`Store::add`] method after creating a [`Transaction`]:
//!
//! ```rust
//! use rexie::*;
//!
//! async fn add_employee(rexie: &Rexie, name: &str, email: &str) -> Result<u32> {
//! // Create a new read-write transaction
//! let transaction = rexie.transaction(&["employees"], TransactionMode::ReadWrite)?;
//!
//! // Get the `employees` store
//! let employees = transaction.store("employees")?;
//!
//! // Create an employee
//! let employee = serde_json::json!({
//! "name": name,
//! "email": email,
//! });
//! // Convert it to `JsValue`
//! let employee = serde_wasm_bindgen::to_value(&employee).unwrap();
//!
//! // Add the employee to the store
//! let employee_id = employees.add(&employee, None).await?;
//!
//! // Waits for the transaction to complete
//! transaction.done().await?;
//!
//! // Return the employee id
//! Ok(num_traits::cast(employee_id.as_f64().unwrap()).unwrap())
//! }
//! ```
//!
//! To get an employee, you can use the [`Store::get`] method after creating a [`Transaction`]:
//!
//! ```rust
//! use rexie::*;
//!
//! async fn get_employee(rexie: &Rexie, id: u32) -> Result<Option<serde_json::Value>> {
//! // Create a new read-only transaction
//! let transaction = rexie.transaction(&["employees"], TransactionMode::ReadOnly)?;
//!
//! // Get the `employees` store
//! let employees = transaction.store("employees")?;
//!
//! // Get the employee
//! let employee = employees.get(id.into()).await?.unwrap();
//!
//! // Convert it to `serde_json::Value` from `JsValue`
//! let employee: Option<serde_json::Value> = serde_wasm_bindgen::from_value(employee).unwrap();
//!
//! // Return the employee
//! Ok(employee)
//! }
//! ```
mod error;
mod index;
mod object_store;
mod rexie;
mod rexie_builder;
mod transaction;
pub use idb::{
CursorDirection as Direction, KeyPath, KeyRange, TransactionMode, TransactionResult,
};
pub use self::{
error::{Error, Result},
index::Index,
object_store::ObjectStore,
rexie::Rexie,
rexie_builder::RexieBuilder,
transaction::{Store, StoreIndex, Transaction},
};