44//! via a parent `Collection` struct, for example:
55//! `client.collection::<Book>().document("123")`
66
7- use crate :: { Client , Error , execute_wrapper} ;
7+ use crate :: { Client , Error , execute_wrapper, traits } ;
88use serde:: { Serialize , de:: DeserializeOwned } ;
99use typesense_codegen:: apis:: documents_api;
1010
@@ -51,10 +51,32 @@ where
5151
5252 let result_value = execute_wrapper ! ( self , documents_api:: get_document, params) ?;
5353
54- // Deserialize the raw JSON value into the user's type T .
54+ // Deserialize the raw JSON value into the user's type D .
5555 serde_json:: from_value ( result_value) . map_err ( Error :: from)
5656 }
5757
58+ /// Deletes this individual document from the collection.
59+ /// The deleted document is returned.
60+ ///
61+ /// # Returns
62+ /// A `Result` containing the deleted document deserialized into `D`.
63+ pub async fn delete ( & self ) -> Result < D , Error < documents_api:: DeleteDocumentError > > {
64+ let params = documents_api:: DeleteDocumentParams {
65+ collection_name : self . collection_name . to_owned ( ) ,
66+ document_id : self . document_id . to_owned ( ) ,
67+ } ;
68+
69+ let result_value = execute_wrapper ! ( self , documents_api:: delete_document, params) ?;
70+
71+ // Deserialize the raw JSON value of the deleted document into T.
72+ serde_json:: from_value ( result_value) . map_err ( Error :: from)
73+ }
74+ }
75+
76+ impl < ' c , ' n , D > Document < ' c , ' n , D >
77+ where
78+ D : traits:: Document ,
79+ {
5880 /// Updates this individual document. The update can be partial.
5981 /// The updated full document is returned.
6082 ///
6890 /// # Example
6991 /// ```no_run
7092 /// # use serde::{Serialize, Deserialize};
71- /// # use typesense::{Client, models};
93+ /// # use typesense::{Client, Typesense, models};
7294 /// # use reqwest::Url;
73- /// # #[derive(Serialize, Deserialize)]
95+ /// # #[derive(Typesense, Serialize, Deserialize)]
7496 /// # struct Book { id: String, title: String, pages: i32 }
7597 /// #
7698 /// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
79101 /// # .api_key("xyz")
80102 /// # .build()
81103 /// # .unwrap();
82- /// let book_update = serde_json::json!({ " pages": 654 }) ;
104+ /// let book_update = BookPartial { pages: Some( 654), ..Default::default() } ;
83105 ///
84106 /// // Simple update
85107 /// let updated_book = client.collection_named::<Book>("books").document("123")
@@ -97,15 +119,15 @@ where
97119 /// # Ok(())
98120 /// # }
99121 /// ```
100- pub async fn update < U : Serialize > (
122+ pub async fn update (
101123 & self ,
102- partial_document : U ,
124+ partial_document : & D :: Partial ,
103125 params : Option < crate :: models:: DocumentIndexParameters > ,
104126 ) -> Result < D , Error < documents_api:: UpdateDocumentError > > {
105127 let params = documents_api:: UpdateDocumentParams {
106128 collection_name : self . collection_name . to_owned ( ) ,
107129 document_id : self . document_id . to_owned ( ) ,
108- body : serde_json :: to_value ( partial_document) ? ,
130+ body : partial_document,
109131 dirty_values : params. and_then ( |d| d. dirty_values ) ,
110132 } ;
111133
@@ -114,21 +136,4 @@ where
114136 // Deserialize the raw JSON value of the updated document into T.
115137 serde_json:: from_value ( result_value) . map_err ( Error :: from)
116138 }
117-
118- /// Deletes this individual document from the collection.
119- /// The deleted document is returned.
120- ///
121- /// # Returns
122- /// A `Result` containing the deleted document deserialized into `D`.
123- pub async fn delete ( & self ) -> Result < D , Error < documents_api:: DeleteDocumentError > > {
124- let params = documents_api:: DeleteDocumentParams {
125- collection_name : self . collection_name . to_owned ( ) ,
126- document_id : self . document_id . to_owned ( ) ,
127- } ;
128-
129- let result_value = execute_wrapper ! ( self , documents_api:: delete_document, params) ?;
130-
131- // Deserialize the raw JSON value of the deleted document into T.
132- serde_json:: from_value ( result_value) . map_err ( Error :: from)
133- }
134139}
0 commit comments