mirror of
https://github.com/Start9Labs/patch-db.git
synced 2026-03-26 10:21:53 +00:00
MapModel remove
This commit is contained in:
committed by
Aiden McClelland
parent
60320e3083
commit
209c9860c3
@@ -4,12 +4,13 @@ use std::marker::PhantomData;
|
|||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
use indexmap::{IndexMap, IndexSet};
|
use indexmap::{IndexMap, IndexSet};
|
||||||
|
use json_patch::{Patch, PatchOperation, RemoveOperation};
|
||||||
use json_ptr::JsonPointer;
|
use json_ptr::JsonPointer;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
use crate::Error;
|
|
||||||
use crate::{locker::LockType, DbHandle};
|
use crate::{locker::LockType, DbHandle};
|
||||||
|
use crate::{DiffPatch, Error};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ModelData<T: Serialize + for<'de> Deserialize<'de>>(T);
|
pub struct ModelData<T: Serialize + for<'de> Deserialize<'de>>(T);
|
||||||
@@ -459,6 +460,16 @@ where
|
|||||||
.map(|s| serde_json::from_value(Value::String(s)))
|
.map(|s| serde_json::from_value(Value::String(s)))
|
||||||
.collect::<Result<_, _>>()?)
|
.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>
|
impl<T> MapModel<T>
|
||||||
where
|
where
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ pub struct Dump {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
pub struct DiffPatch(Patch);
|
pub struct DiffPatch(pub(crate) Patch);
|
||||||
impl DiffPatch {
|
impl DiffPatch {
|
||||||
pub fn prepend<S: AsRef<str>, V: SegList>(&mut self, ptr: &JsonPointer<S, V>) {
|
pub fn prepend<S: AsRef<str>, V: SegList>(&mut self, ptr: &JsonPointer<S, V>) {
|
||||||
self.0.prepend(ptr)
|
self.0.prepend(ptr)
|
||||||
|
|||||||
Reference in New Issue
Block a user