sanity_rs/orm.rs
1use super::client::SanityClient;
2use super::error::RequestError;
3use super::url::SanityURL;
4use serde::de::DeserializeOwned;
5
6/// A trait defining the interface for an Object-Relational Mapper (ORM). This trait provides methods for interacting with a data source,
7/// retrieving data in JSON format, and performing CRUD operations.
8pub trait ORM {
9 /// Serialized reterived data into JSON.
10 ///
11 /// # Type Parameters
12 ///
13 /// * `T`: The type to deserialize the JSON data into. Must implement `DeserializeOwned`.
14 ///
15 /// # Returns
16 ///
17 /// * `Result<T, RequestError>`: A Result containing the deserialized data or a `RequestError` if an error occurred.
18 fn json<T: serde::de::DeserializeOwned>(&mut self) -> Result<T, RequestError>;
19
20 /// Retrieves a single record from based on its ID.
21 ///
22 /// # Arguments
23 ///
24 /// * `id`: The ID of the record to retrieve.
25 ///
26 /// # Returns
27 ///
28 /// * `&mut SanityClient`: A mutable reference to the SanityClient. This likely needs further clarification
29 /// depending on the actual implementation. Consider returning a Result instead.
30 fn get_by_id(&mut self, id: &str) -> &mut SanityClient;
31
32 /// Retrieves multiple records based on their IDs.
33 ///
34 /// # Arguments
35 ///
36 /// * `ids`: A slice of IDs of the records to retrieve.
37 ///
38 /// # Returns
39 ///
40 /// * `&mut SanityClient`: A mutable reference to the SanityClient. This likely needs further clarification
41 /// depending on the actual implementation. Consider returning a Result instead.
42 fn get_by_ids(&mut self, ids: &[&str]) -> &mut SanityClient;
43
44 /// Sends a request to the data source.
45 ///
46 /// # Returns
47 ///
48 /// * `impl Future<Output = Result<&mut Self, RequestError>>`: A future that resolves to a Result containing a mutable reference to `Self`
49 /// or a `RequestError` if an error occurred.
50 fn send(&mut self) -> impl std::future::Future<Output = Result<&mut Self, RequestError>>;
51}
52
53impl ORM for SanityClient {
54 fn get_by_id(&mut self, id: &str) -> &mut SanityClient {
55 let string = format!("*[_id == '{}'][0]", id);
56 let query = &mut self.payload.query;
57 SanityURL::query(query, string.as_str());
58 self
59 }
60
61 fn get_by_ids(&mut self, ids: &[&str]) -> &mut SanityClient {
62 let string = format!(r#"*[_id in ["{}"]]"#, ids.join("\",\""));
63 let query = &mut self.payload.query;
64 SanityURL::query(query, string.as_str());
65 self
66 }
67
68 fn json<T: DeserializeOwned>(&mut self) -> Result<T, RequestError> {
69 let res = self.payload.query_result.as_ref().unwrap();
70 let value: T = serde_json::from_str(res).map_err(RequestError::JsonParsingError)?;
71 Ok(value)
72 }
73
74 async fn send(&mut self) -> Result<&mut Self, RequestError> {
75 let query = &mut self.payload.query;
76 let body = &self.payload.body;
77 let url = format!("{}{}", query.as_str(), body.as_ref().unwrap());
78 let v = self.client.get(url.as_str()).send();
79 let v = v.await?.text().await?;
80 self.payload.query_result = Some(v);
81 Ok(self)
82 }
83}
84