10 个版本 (5 个破坏性更新)
新增 0.6.3 | 2024 年 8 月 12 日 |
---|---|
0.6.2 | 2024 年 7 月 4 日 |
0.6.1 | 2024 年 1 月 25 日 |
0.4.1 | 2023 年 3 月 11 日 |
0.1.0 | 2022 年 7 月 7 日 |
#131 在 WebAssembly
每月下载量 2,835 次
在 7 个 crate 中使用 (6 个直接使用)
150KB
3.5K SLoC
idb
基于 futures 的 crate,用于在浏览器中使用 WebAssembly 与 IndexedDB 交互。
用法
要使用 idb
,您需要从您的项目根目录运行以下命令
cargo add idb
如果您不想使用 async/
await
语法,您可以使用以下命令禁用 futures
功能
cargo add idb --no-default-features
禁用 futures
功能后,您可以在请求上使用 on_success
和 on_error
方法来附加回调。
示例
要创建一个新的数据库,您可以使用 Factory::open
use idb::{Database, DatabaseEvent, Error, Factory, IndexParams, KeyPath, ObjectStoreParams};
async fn create_database() -> Result<Database, Error> {
// Get a factory instance from global scope
let factory = Factory::new()?;
// Create an open request for the database
let mut open_request = factory.open("test", Some(1)).unwrap();
// Add an upgrade handler for database
open_request.on_upgrade_needed(|event| {
// Get database instance from event
let database = event.database().unwrap();
// Prepare object store params
let mut store_params = ObjectStoreParams::new();
store_params.auto_increment(true);
store_params.key_path(Some(KeyPath::new_single("id")));
// Create object store
let store = database
.create_object_store("employees", store_params)
.unwrap();
// Prepare index params
let mut index_params = IndexParams::new();
index_params.unique(true);
// Create index on object store
store
.create_index("email", KeyPath::new_single("email"), Some(index_params))
.unwrap();
});
// `await` open request
open_request.await
}
要将数据添加到对象存储中,您可以使用 ObjectStore::add
use idb::{Database, Error, TransactionMode};
use serde::Serialize;
use serde_wasm_bindgen::Serializer;
use wasm_bindgen::JsValue;
async fn add_data(database: &Database) -> Result<JsValue, Error> {
// Create a read-write transaction
let transaction = database.transaction(&["employees"], TransactionMode::ReadWrite)?;
// Get the object store
let store = transaction.object_store("employees").unwrap();
// Prepare data to add
let employee = serde_json::json!({
"name": "John Doe",
"email": "[email protected]",
});
// Add data to object store
let id = store
.add(
&employee.serialize(&Serializer::json_compatible()).unwrap(),
None,
)
.unwrap()
.await?;
// Commit the transaction
transaction.commit()?.await?;
Ok(id)
}
要从对象存储中获取数据,您可以使用 ObjectStore::get
use idb::{Database, Error, TransactionMode};
use serde_json::Value;
use wasm_bindgen::JsValue;
async fn get_data(database: &Database, id: JsValue) -> Result<Option<Value>, Error> {
// Create a read-only transaction
let transaction = database
.transaction(&["employees"], TransactionMode::ReadOnly)
.unwrap();
// Get the object store
let store = transaction.object_store("employees").unwrap();
// Get the stored data
let stored_employee: Option<JsValue> = store.get(id)?.await?;
// Deserialize the stored data
let stored_employee: Option<Value> = stored_employee
.map(|stored_employee| serde_wasm_bindgen::from_value(stored_employee).unwrap());
// Wait for the transaction to complete (alternatively, you can also commit the transaction)
transaction.await?;
Ok(stored_employee)
}
有关使用其他功能的更多示例,请参阅 测试目录。
许可证
根据您的选择,许可协议为
- Apache License,版本 2.0 (LICENSE-APACHE)
- MIT 许可证 (LICENSE-MIT)
。
贡献
除非您明确声明,否则根据 Apache-2.0 许可证定义的,您有意提交的任何贡献,都应按上述方式双许可,而无需任何附加条款或条件。
依赖项
~7–11MB
~198K SLoC