Skip to content

Commit a9cb48e

Browse files
authored
feat: add update collection (#20)
1 parent bbed83f commit a9cb48e

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/client.rs

+45
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,26 @@ impl ChromaClient {
166166
Ok(())
167167
}
168168

169+
/// Update a collection with the given id.
170+
///
171+
/// # Arguments
172+
///
173+
/// * `collection_id` - The id of the collection to update
174+
/// * `new_name` - Optional new name for the collection
175+
/// * `metadata` - Optional new metadata for the collection
176+
///
177+
/// # Errors
178+
///
179+
/// * If the collection name is invalid
180+
/// * If the collection does not exist
181+
pub async fn update_collection(&self, collection_id: &str, new_name: Option<&str>, metadata: Option<Metadata>) -> Result<()> {
182+
self.api.put_database(
183+
&format!("/collections/{}", collection_id),
184+
Some(json!({ "new_name": new_name,"new_metadata": metadata })),
185+
).await?;
186+
Ok(())
187+
}
188+
169189
/// The version of Chroma
170190
pub async fn version(&self) -> Result<String> {
171191
let response = self.api.get_v1("/version").await?;
@@ -265,4 +285,29 @@ mod tests {
265285
let collection = client.delete_collection(DELETE_TEST_COLLECTION).await;
266286
assert!(collection.is_err());
267287
}
288+
289+
#[tokio::test]
290+
async fn test_update_collection() {
291+
let client: ChromaClient = ChromaClient::new(Default::default()).await.unwrap();
292+
293+
let collection = client.get_or_create_collection(TEST_COLLECTION, None).await.unwrap();
294+
let collection_id = collection.id();
295+
296+
let result = client.update_collection(collection_id, None, None).await;
297+
assert!(result.is_ok());
298+
299+
let new_name = "new_name";
300+
let result = client.update_collection(collection_id, Some(new_name), None).await;
301+
assert!(result.is_ok());
302+
303+
let updated_collection = client.get_collection(new_name).await.unwrap();
304+
assert_eq!(collection_id, updated_collection.id());
305+
306+
let new_metadata = Some(json!({"foo": "bar"}).as_object().unwrap().clone());
307+
let result = client.update_collection(collection_id, None, new_metadata.clone()).await;
308+
assert!(result.is_ok());
309+
310+
let updated_collection = client.get_collection(new_name).await.unwrap();
311+
assert_eq!(updated_collection.metadata(), new_metadata.as_ref());
312+
}
268313
}

0 commit comments

Comments
 (0)