Skip to main content

Mountain/IPC/WindServiceHandlers/Storage/
StorageUpdateItems.rs

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