Skip to content

Commit 57a1f6e

Browse files
committed
feat: documents .create() and .upsert()
1 parent 4081435 commit 57a1f6e

File tree

1 file changed

+26
-44
lines changed

1 file changed

+26
-44
lines changed

typesense/src/client/collection/documents.rs

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ impl<'a> Documents<'a> {
3131

3232
/// Indexes a document in the collection.
3333
///
34-
/// If the document has an 'id' field, it will be used as the document's ID.
35-
/// Otherwise, Typesense will auto-generate an ID.
34+
3635
///
3736
/// # Arguments
3837
/// * `document` - A `serde_json::Value` representing the document.
39-
/// * `action` - The indexing action to perform (e.g., "create", "upsert", "update").
40-
pub async fn index(
38+
/// * `action` - The indexing action to perform (e.g., "create", "upsert").
39+
async fn index(
4140
&self,
4241
document: serde_json::Value,
4342
action: &str,
@@ -56,67 +55,50 @@ impl<'a> Documents<'a> {
5655
.await
5756
}
5857

59-
/// Fetches an individual document from the collection by its ID.
58+
/// Creates a new document in the collection.
59+
/// Fails if a document with the same id already exists
60+
///
61+
/// If the document has an `id` field of type `string`, it will be used as the document's ID.
62+
/// Otherwise, Typesense will auto-generate an ID.
6063
///
6164
/// # Arguments
62-
/// * `document_id` - The ID of the document to retrieve.
63-
pub async fn retrieve(
65+
/// * `document` - A `serde_json::Value` representing the document to create.
66+
pub async fn create(
6467
&self,
65-
document_id: &str,
66-
) -> Result<serde_json::Value, Error<documents_api::GetDocumentError>> {
67-
let params = documents_api::GetDocumentParams {
68-
collection_name: self.collection_name.to_string(),
69-
document_id: document_id.to_string(),
70-
};
71-
72-
self.client
73-
.execute(|config: Arc<configuration::Configuration>| {
74-
let params_for_move = params.clone();
75-
async move { documents_api::get_document(&config, params_for_move).await }
76-
})
77-
.await
68+
document: serde_json::Value,
69+
) -> Result<serde_json::Value, Error<documents_api::IndexDocumentError>> {
70+
self.index(document, "create").await
7871
}
7972

80-
/// Updates an individual document from the collection by its ID. The update can be partial.
73+
/// Creates a new document or updates an existing document if a document with the same id already exists.
74+
/// Requires the whole document to be sent. For partial updates, use the `update()` action.
8175
///
8276
/// # Arguments
83-
/// * `document_id` - The ID of the document to update.
84-
/// * `document` - A `serde_json::Value` containing the fields to update.
85-
pub async fn update(
77+
/// * `document` - A `serde_json::Value` representing the document to upsert.
78+
pub async fn upsert(
8679
&self,
87-
document_id: &str,
8880
document: serde_json::Value,
89-
) -> Result<serde_json::Value, Error<documents_api::UpdateDocumentError>> {
90-
let params = documents_api::UpdateDocumentParams {
91-
collection_name: self.collection_name.to_string(),
92-
document_id: document_id.to_string(),
93-
body: document,
94-
dirty_values: None,
95-
};
96-
self.client
97-
.execute(|config: Arc<configuration::Configuration>| {
98-
let params_for_move = params.clone();
99-
async move { documents_api::update_document(&config, params_for_move).await }
100-
})
101-
.await
81+
) -> Result<serde_json::Value, Error<documents_api::IndexDocumentError>> {
82+
self.index(document, "upsert").await
10283
}
10384

104-
/// Deletes an individual document from the collection by its ID.
85+
/// Fetches an individual document from the collection by its ID.
10586
///
10687
/// # Arguments
107-
/// * `document_id` - The ID of the document to delete.
108-
pub async fn delete(
88+
/// * `document_id` - The ID of the document to retrieve.
89+
pub async fn retrieve(
10990
&self,
11091
document_id: &str,
111-
) -> Result<serde_json::Value, Error<documents_api::DeleteDocumentError>> {
112-
let params = documents_api::DeleteDocumentParams {
92+
) -> Result<serde_json::Value, Error<documents_api::GetDocumentError>> {
93+
let params = documents_api::GetDocumentParams {
11394
collection_name: self.collection_name.to_string(),
11495
document_id: document_id.to_string(),
11596
};
97+
11698
self.client
11799
.execute(|config: Arc<configuration::Configuration>| {
118100
let params_for_move = params.clone();
119-
async move { documents_api::delete_document(&config, params_for_move).await }
101+
async move { documents_api::get_document(&config, params_for_move).await }
120102
})
121103
.await
122104
}

0 commit comments

Comments
 (0)