Skip to main content

Mountain/IPC/WindServiceHandlers/Storage/
StorageUpdateItems.rs

1
2//! Bulk insert + delete in one round-trip. VS Code's
3//! `IndexedDBStorageDatabase` batches every write through this
4//! shape: `{ insert: [[key,value], …] | { key: value }, delete: [keys…] }`.
5//! Both insert encodings (array-of-pairs and object-map) accepted.
6
7use std::sync::Arc;
8
9use CommonLibrary::{Environment::Requires::Requires, Storage::StorageProvider::StorageProvider};
10use serde_json::Value;
11
12use crate::RunTime::ApplicationRunTime::ApplicationRunTime;
13
14pub async fn Fn(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
15	let provider:Arc<dyn StorageProvider> = RunTime.Environment.Require();
16
17	if let Some(Updates) = Arguments.first().and_then(|V| V.as_object()) {
18		if let Some(Inserts) = Updates.get("insert") {
19			if let Some(Arr) = Inserts.as_array() {
20				for Item in Arr {
21					if let Some(Pair) = Item.as_array()
22						&& let (Some(Key), Some(Val)) = (Pair.first().and_then(|V| V.as_str()), Pair.get(1))
23					{
24						let _ = provider.UpdateStorageValue(true, Key.to_string(), Some(Val.clone())).await;
25					}
26				}
27			} else if let Some(Obj) = Inserts.as_object() {
28				for (Key, Val) in Obj {
29					let _ = provider.UpdateStorageValue(true, Key.clone(), Some(Val.clone())).await;
30				}
31			}
32		}
33
34		if let Some(Deletes) = Updates.get("delete").and_then(|V| V.as_array()) {
35			for Key in Deletes {
36				if let Some(K) = Key.as_str() {
37					let _ = provider.UpdateStorageValue(true, K.to_string(), None).await;
38				}
39			}
40		}
41	}
42
43	Ok(Value::Null)
44}