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:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user