surrealdb/api/method/
delete.rs

1use crate::api::conn::Command;
2use crate::api::method::BoxFuture;
3use crate::api::opt::Resource;
4use crate::api::Connection;
5use crate::api::Result;
6use crate::method::OnceLockExt;
7use crate::opt::KeyRange;
8use crate::Surreal;
9use crate::Value;
10use serde::de::DeserializeOwned;
11use std::borrow::Cow;
12use std::future::IntoFuture;
13use std::marker::PhantomData;
14
15/// A record delete future
16#[derive(Debug)]
17#[must_use = "futures do nothing unless you `.await` or poll them"]
18pub struct Delete<'r, C: Connection, R> {
19	pub(super) client: Cow<'r, Surreal<C>>,
20	pub(super) resource: Result<Resource>,
21	pub(super) response_type: PhantomData<R>,
22}
23
24impl<C, R> Delete<'_, C, R>
25where
26	C: Connection,
27{
28	/// Converts to an owned type which can easily be moved to a different thread
29	pub fn into_owned(self) -> Delete<'static, C, R> {
30		Delete {
31			client: Cow::Owned(self.client.into_owned()),
32			..self
33		}
34	}
35}
36
37macro_rules! into_future {
38	($method:ident) => {
39		fn into_future(self) -> Self::IntoFuture {
40			let Delete {
41				client,
42				resource,
43				..
44			} = self;
45			Box::pin(async move {
46				let router = client.router.extract()?;
47				router
48					.$method(Command::Delete {
49						what: resource?,
50					})
51					.await
52			})
53		}
54	};
55}
56
57impl<'r, Client> IntoFuture for Delete<'r, Client, Value>
58where
59	Client: Connection,
60{
61	type Output = Result<Value>;
62	type IntoFuture = BoxFuture<'r, Self::Output>;
63
64	into_future! {execute_value}
65}
66
67impl<'r, Client, R> IntoFuture for Delete<'r, Client, Option<R>>
68where
69	Client: Connection,
70	R: DeserializeOwned,
71{
72	type Output = Result<Option<R>>;
73	type IntoFuture = BoxFuture<'r, Self::Output>;
74
75	into_future! {execute_opt}
76}
77
78impl<'r, Client, R> IntoFuture for Delete<'r, Client, Vec<R>>
79where
80	Client: Connection,
81	R: DeserializeOwned,
82{
83	type Output = Result<Vec<R>>;
84	type IntoFuture = BoxFuture<'r, Self::Output>;
85
86	into_future! {execute_vec}
87}
88
89impl<C> Delete<'_, C, Value>
90where
91	C: Connection,
92{
93	/// Restricts a range of records to delete
94	pub fn range(mut self, range: impl Into<KeyRange>) -> Self {
95		self.resource = self.resource.and_then(|x| x.with_range(range.into()));
96		self
97	}
98}
99
100impl<C, R> Delete<'_, C, Vec<R>>
101where
102	C: Connection,
103{
104	/// Restricts a range of records to delete
105	pub fn range(mut self, range: impl Into<KeyRange>) -> Self {
106		self.resource = self.resource.and_then(|x| x.with_range(range.into()));
107		self
108	}
109}