Skip to main content

Mountain/IPC/WindServiceHandlers/Storage/
StorageUpdateItems.rs

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