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