Function write

Source
pub async fn write<T>(
    auth: &impl FirebaseAuthBearer,
    path: &str,
    document_id: Option<impl AsRef<str>>,
    document: &T,
    options: WriteOptions,
) -> Result<WriteResult>
where T: Serialize,
Expand description

Write a document to a given collection.

If no document_id is given, Firestore will generate an ID. Check the WriteResult return value.

If a document_id is given, the document will be created if it does not yet exist. Except if the “merge” option (see WriteOptions::merge) is set.

Example:

use firestore_db_and_auth::{Credentials, ServiceSession, documents, errors::Result, FirebaseAuthBearer};
use serde::{Serialize,Deserialize};

 #[derive(Serialize, Deserialize)]
 struct DemoDTO {
    a_string: String,
    an_int: u32,
    another_int: u32,
 }
 #[derive(Serialize, Deserialize)]
 struct DemoPartialDTO {
    #[serde(skip_serializing_if = "Option::is_none")]
    a_string: Option<String>,
    an_int: u32,
 }

 async fn write(session: &impl FirebaseAuthBearer) -> Result<()> {
    let obj = DemoDTO { a_string: "abcd".to_owned(), an_int: 14, another_int: 10 };
    let result = documents::write(session, "tests", Some("service_test"), &obj, documents::WriteOptions::default()).await?;
    println!("id: {}, created: {}, updated: {}", result.document_id, result.create_time.unwrap(), result.update_time.unwrap());
    Ok(())
 }
 /// Only write some fields and do not overwrite the entire document.
 /// Either via Option<> or by not having the fields in the structure, see DemoPartialDTO.
 async fn write_partial(session: &impl FirebaseAuthBearer) -> Result<()> {
    let obj = DemoPartialDTO { a_string: None, an_int: 16 };
    let result = documents::write(session, "tests", Some("service_test"), &obj, documents::WriteOptions{merge:true}).await?;
    println!("id: {}, created: {}, updated: {}", result.document_id, result.create_time.unwrap(), result.update_time.unwrap());
    Ok(())
 }

§Arguments

  • ‘auth’ The authentication token
  • ‘path’ The document path / collection; For example “my_collection” or “a/nested/collection”
  • ‘document_id’ The document id. Make sure that you do not include the document id in the path argument.
  • ‘document’ The document
  • ‘options’ Write options