firestore_db_and_auth/documents/
delete.rs

1use super::*;
2use crate::errors::extract_google_api_error_async;
3
4///
5/// Deletes the document at the given path.
6///
7/// You cannot use this directly with paths from [`list`] and [`query`] document metadata objects.
8/// Those contain an absolute document path. Use [`abs_to_rel`] to convert to a relative path.
9///
10/// ## Arguments
11/// * 'auth' The authentication token
12/// * 'path' The relative collection path and document id, for example "my_collection/document_id"
13/// * 'fail_if_not_existing' If true this method will return an error if the document does not exist.
14pub async fn delete(auth: &impl FirebaseAuthBearer, path: &str, fail_if_not_existing: bool) -> Result<()> {
15    let url = firebase_url(auth.project_id(), path);
16
17    let query_request = dto::Write {
18        current_document: Some(dto::Precondition {
19            exists: match fail_if_not_existing {
20                true => Some(true),
21                false => None,
22            },
23            ..Default::default()
24        }),
25        ..Default::default()
26    };
27
28    let resp = auth
29        .client()
30        .delete(&url)
31        .bearer_auth(auth.access_token().await.to_owned())
32        .json(&query_request)
33        .send()
34        .await?;
35
36    extract_google_api_error_async(resp, || path.to_owned()).await?;
37
38    Ok({})
39}