MapModel remove

This commit is contained in:
Aiden McClelland
2021-08-24 15:18:49 -06:00
committed by Aiden McClelland
parent 60320e3083
commit 209c9860c3
2 changed files with 13 additions and 2 deletions

View File

@@ -4,12 +4,13 @@ use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use indexmap::{IndexMap, IndexSet};
use json_patch::{Patch, PatchOperation, RemoveOperation};
use json_ptr::JsonPointer;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::Error;
use crate::{locker::LockType, DbHandle};
use crate::{DiffPatch, Error};
#[derive(Debug)]
pub struct ModelData<T: Serialize + for<'de> Deserialize<'de>>(T);
@@ -459,6 +460,16 @@ where
.map(|s| serde_json::from_value(Value::String(s)))
.collect::<Result<_, _>>()?)
}
pub async fn remove<Db: DbHandle>(&self, db: &mut Db, key: &T::Key) -> Result<(), Error> {
db.lock(self.as_ref(), LockType::Write, false).await;
db.apply(DiffPatch(Patch(vec![PatchOperation::Remove(
RemoveOperation {
path: self.as_ref().clone().join_end(key.as_ref()),
},
)])))
.await?;
Ok(())
}
}
impl<T> MapModel<T>
where

View File

@@ -22,7 +22,7 @@ pub struct Dump {
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct DiffPatch(Patch);
pub struct DiffPatch(pub(crate) Patch);
impl DiffPatch {
pub fn prepend<S: AsRef<str>, V: SegList>(&mut self, ptr: &JsonPointer<S, V>) {
self.0.prepend(ptr)