From b428fbcc86a0bfee8c52383e2bc5507a477a2390 Mon Sep 17 00:00:00 2001 From: officeyutong Date: Thu, 19 Dec 2024 22:31:12 +0800 Subject: [PATCH] update: use fixed store name for wasm; use path as db name --- network/src/peer_store/browser.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/network/src/peer_store/browser.rs b/network/src/peer_store/browser.rs index 20fd775fd5..691c88c34a 100644 --- a/network/src/peer_store/browser.rs +++ b/network/src/peer_store/browser.rs @@ -59,6 +59,8 @@ pub async fn get_db>(path: P) -> &'static Storage { DB.get_or_init(|| Storage::new(path)).await } +const STORE_NAME: &str = "main-store"; + #[derive(Clone)] pub struct Storage { chan: tokio::sync::mpsc::Sender, @@ -67,15 +69,14 @@ pub struct Storage { impl Storage { pub async fn new>(path: P) -> Self { let factory = Factory::new().unwrap(); - let mut open_request = factory.open("network", Some(1)).unwrap(); - let store_name = path.as_ref().to_str().unwrap().to_owned(); - let store_name_clone = store_name.clone(); + let database_name = path.as_ref().to_str().unwrap().to_owned(); + let mut open_request = factory.open(&database_name, Some(1)).unwrap(); open_request.on_upgrade_needed(move |event| { let database = event.database().unwrap(); let store_params = ObjectStoreParams::new(); let store = database - .create_object_store(&store_name_clone, store_params) + .create_object_store(STORE_NAME, store_params) .unwrap(); let mut index_params = IndexParams::new(); index_params.unique(true); @@ -92,9 +93,9 @@ impl Storage { match request.cmd { CommandRequest::Read { key } => { let tran = db - .transaction(&[&store_name], TransactionMode::ReadOnly) + .transaction(&[STORE_NAME], TransactionMode::ReadOnly) .unwrap(); - let store = tran.object_store(&store_name).unwrap(); + let store = tran.object_store(STORE_NAME).unwrap(); let key = serde_wasm_bindgen::to_value(&key).unwrap(); let value = store .get(key) @@ -107,9 +108,9 @@ impl Storage { } CommandRequest::Put { kv } => { let tran = db - .transaction(&[&store_name], TransactionMode::ReadWrite) + .transaction(&[STORE_NAME], TransactionMode::ReadWrite) .unwrap(); - let store = tran.object_store(&store_name).unwrap(); + let store = tran.object_store(STORE_NAME).unwrap(); let key = serde_wasm_bindgen::to_value(&kv.key).unwrap(); let value = serde_wasm_bindgen::to_value(&kv).unwrap();