Skip to main content

Crate firestore

Crate firestore 

Source
Expand description

§Firestore for Rust

Library provides a simple API for Google Firestore:

  • Create or update documents using Rust structures and Serde;
  • Support for querying / streaming / listing / listening changes / aggregated queries of documents from Firestore;
  • Fluent high-level and strongly typed API;
  • Full async based on Tokio runtime;
  • Macro that helps you use JSON paths as references to your structure fields;
  • Implements own Serde serializer to Firestore gRPC values;
  • Supports for Firestore timestamp with #[serde(with)];
  • Transactions support;
  • Streaming batch writes with automatic throttling to avoid time limits from Firestore;
  • Aggregated Queries;
  • Google client based on gcloud-sdk library that automatically detects GKE environment or application default accounts for local development;

§Example using the Fluent API:


use firestore::*;
use serde::{Deserialize, Serialize};
use futures::stream::BoxStream;
use futures::StreamExt;

pub fn config_env_var(name: &str) -> Result<String, String> {
   std::env::var(name).map_err(|e| format!("{}: {}", name, e))
}

// Example structure to play with
#[derive(Debug, Clone, Deserialize, Serialize)]
struct MyTestStructure {
   some_id: String,
   some_string: String,
   one_more_string: String,
   some_num: u64,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {//!
  // Create an instance
  let db = FirestoreDb::new(&config_env_var("PROJECT_ID")?).await?;

  const TEST_COLLECTION_NAME: &'static str = "test";

  let my_struct = MyTestStructure {
       some_id: "test-1".to_string(),
       some_string: "Test".to_string(),
       one_more_string: "Test2".to_string(),
       some_num: 42,
  };

  // Create documents
  let object_returned: MyTestStructure = db.fluent()
      .insert()
      .into(TEST_COLLECTION_NAME)
      .document_id(&my_struct.some_id)
      .object(&my_struct)
      .execute()
      .await?;

  // Update documents
  let object_updated: MyTestStructure = db.fluent()
      .update()
      .fields(paths!(MyTestStructure::{some_num, one_more_string})) // Update only specified fields
      .in_col(TEST_COLLECTION_NAME)
      .document_id(&my_struct.some_id)
      .object(&MyTestStructure {
          some_num: my_struct.some_num + 1,
         one_more_string: "updated-value".to_string(),
          ..my_struct.clone()
      })
      .execute()
     .await?;
 
  // Get a document as an object by id
  let find_it_again: Option<MyTestStructure> = db.fluent()
        .select()
        .by_id_in(TEST_COLLECTION_NAME)
        .obj()
        .one(&my_struct.some_id)
        .await?;

  // Query and read stream of objects
  let object_stream: BoxStream<MyTestStructure> = db.fluent()
    .select()
    .fields(paths!(MyTestStructure::{some_id, some_num, some_string, one_more_string})) // Optionally select the fields needed
    .from(TEST_COLLECTION_NAME)
    .filter(|q| { // Fluent filter API example
        q.for_all([
            q.field(path!(MyTestStructure::some_num)).is_not_null(),
            q.field(path!(MyTestStructure::some_string)).eq("Test"),
            // Sometimes you have optional filters
            Some("Test2")
                .and_then(|value| q.field(path!(MyTestStructure::one_more_string)).eq(value)),
        ])
    })
    .order_by([(
        path!(MyTestStructure::some_num),
        FirestoreQueryDirection::Descending,
    )])
    .obj() // Reading documents as structures using Serde gRPC deserializer
    .stream_query()
    .await?;

    let as_vec: Vec<MyTestStructure> = object_stream.collect().await;
    println!("{:?}", as_vec);

    // Delete documents
    db.fluent()
        .delete()
        .from(TEST_COLLECTION_NAME)
        .document_id(&my_struct.some_id)
        .execute()
        .await?;

    Ok(())
}

All examples and more docs available at: github

Re-exports§

pub extern crate struct_path;

Modules§

delete_builder
Re-exports all public items from the fluent_api module.
document_transform_builder
Re-exports all public items from the fluent_api module.
errors
Defines the error types used throughout the firestore-rs crate.
insert_builder
Re-exports all public items from the fluent_api module.
listing_builder
Re-exports all public items from the fluent_api module.
select_aggregation_builder
Re-exports all public items from the fluent_api module.
select_builder
Re-exports all public items from the fluent_api module.
select_filter_builder
Re-exports all public items from the fluent_api module.
serialize_as_null
Re-exports all public items from the firestore_serde module.
serialize_as_null_timestamp
Re-exports all public items from the firestore_serde module.
serialize_as_optional_timestamp
Re-exports all public items from the firestore_serde module.
serialize_as_reference
Re-exports all public items from the firestore_serde module.
serialize_as_timestamp
Re-exports all public items from the firestore_serde module.
timestamp_utils
Provides utility functions for working with Firestore timestamps.
update_builder
Re-exports all public items from the fluent_api module.

Macros§

path
path_camel_case
paths
paths_camel_case

Structs§

FirestoreAggregatedQueryParams
Re-exports all public items from the db module.
FirestoreAggregatedQueryParamsInit
Re-exports all public items from the db module.
FirestoreAggregation
Re-exports all public items from the db module.
FirestoreAggregationInit
Re-exports all public items from the db module.
FirestoreAggregationOperatorAvg
Re-exports all public items from the db module.
FirestoreAggregationOperatorAvgInit
Re-exports all public items from the db module.
FirestoreAggregationOperatorCount
Re-exports all public items from the db module.
FirestoreAggregationOperatorCountInit
Re-exports all public items from the db module.
FirestoreAggregationOperatorSum
Re-exports all public items from the db module.
FirestoreAggregationOperatorSumInit
Re-exports all public items from the db module.
FirestoreBatch
Re-exports all public items from the db module.
FirestoreBatchWriteResponse
Re-exports all public items from the db module.
FirestoreBatchWriteResponseInit
Re-exports all public items from the db module.
FirestoreCollectionDocuments
Re-exports all public items from the db module.
FirestoreCollectionDocumentsInit
Re-exports all public items from the db module.
FirestoreDb
Re-exports all public items from the db module.
FirestoreDbOptions
Re-exports all public items from the db module.
FirestoreDbOptionsInit
Re-exports all public items from the db module.
FirestoreDbSessionParams
Re-exports all public items from the db module.
FirestoreDbSessionParamsInit
Re-exports all public items from the db module.
FirestoreDocumentMetadata
Re-exports all public items from the firestore_meta module.
FirestoreDocumentMetadataInit
Re-exports all public items from the firestore_meta module.
FirestoreDynamicStruct
Re-exports all public items from the firestore_meta module.
FirestoreDynamicStructInit
Re-exports all public items from the firestore_meta module.
FirestoreExecutionStats
Re-exports all public items from the firestore_meta module.
FirestoreExecutionStatsInit
Re-exports all public items from the firestore_meta module.
FirestoreExplainMetrics
Re-exports all public items from the firestore_meta module.
FirestoreExplainMetricsInit
Re-exports all public items from the firestore_meta module.
FirestoreExplainOptions
Re-exports all public items from the db module.
FirestoreExplainOptionsInit
Re-exports all public items from the db module.
FirestoreExprBuilder
Re-exports all public items from the fluent_api module.
FirestoreFieldTransform
Re-exports all public items from the db module.
FirestoreFieldTransformInit
Re-exports all public items from the db module.
FirestoreFindNearestOptions
Re-exports all public items from the db module.
FirestoreFindNearestOptionsInit
Re-exports all public items from the db module.
FirestoreGeoPoint
Re-exports all public items from the firestore_serde module.
FirestoreLatLng
Re-exports all public items from the firestore_serde module.
FirestoreListCollectionIdsParams
Re-exports all public items from the db module.
FirestoreListCollectionIdsParamsInit
Re-exports all public items from the db module.
FirestoreListCollectionIdsResult
Re-exports all public items from the db module.
FirestoreListCollectionIdsResultInit
Re-exports all public items from the db module.
FirestoreListDocParams
Re-exports all public items from the db module.
FirestoreListDocParamsInit
Re-exports all public items from the db module.
FirestoreListDocResult
Re-exports all public items from the db module.
FirestoreListDocResultInit
Re-exports all public items from the db module.
FirestoreListener
Re-exports all public items from the db module.
FirestoreListenerParams
Re-exports all public items from the db module.
FirestoreListenerParamsInit
Re-exports all public items from the db module.
FirestoreListenerTarget
Re-exports all public items from the db module.
FirestoreListenerTargetParams
Re-exports all public items from the db module.
FirestoreListenerTargetParamsInit
Re-exports all public items from the db module.
FirestoreListenerToken
Re-exports all public items from the db module.
FirestoreMemListenStateStorage
Re-exports all public items from the db module.
FirestorePartition
Re-exports all public items from the db module.
FirestorePartitionInit
Re-exports all public items from the db module.
FirestorePartitionQueryParams
Re-exports all public items from the db module.
FirestorePartitionQueryParamsInit
Re-exports all public items from the db module.
FirestorePlanSummary
Re-exports all public items from the firestore_meta module.
FirestorePlanSummaryInit
Re-exports all public items from the firestore_meta module.
FirestoreQueryFilterComposite
Re-exports all public items from the db module.
FirestoreQueryFilterCompositeInit
Re-exports all public items from the db module.
FirestoreQueryOrder
Re-exports all public items from the db module.
FirestoreQueryOrderInit
Re-exports all public items from the db module.
FirestoreQueryParams
Re-exports all public items from the db module.
FirestoreQueryParamsInit
Re-exports all public items from the db module.
FirestoreReference
Re-exports all public items from the firestore_serde module.
FirestoreSimpleBatchWriteOptions
Re-exports all public items from the db module.
FirestoreSimpleBatchWriteOptionsInit
Re-exports all public items from the db module.
FirestoreSimpleBatchWriter
Re-exports all public items from the db module.
FirestoreStreamingBatchWriteOptions
Re-exports all public items from the db module.
FirestoreStreamingBatchWriteOptionsInit
Re-exports all public items from the db module.
FirestoreStreamingBatchWriter
Re-exports all public items from the db module.
FirestoreTempFilesListenStateStorage
Re-exports all public items from the db module.
FirestoreTimestamp
Re-exports all public items from the firestore_serde module.
FirestoreTransaction
Re-exports all public items from the db module.
FirestoreTransactionData
Re-exports all public items from the db module.
FirestoreTransactionOptions
Re-exports all public items from the db module.
FirestoreTransactionOptionsInit
Re-exports all public items from the db module.
FirestoreTransactionResponse
Re-exports all public items from the db module.
FirestoreTransactionResponseInit
Re-exports all public items from the db module.
FirestoreValue
Re-exports all public items from the firestore_value module.
FirestoreVector
Re-exports all public items from the firestore_serde module.
FirestoreWithMetadata
Re-exports all public items from the firestore_meta module.
FirestoreWriteResult
Re-exports all public items from the db module.
FirestoreWriteResultInit
Re-exports all public items from the db module.
ParentPathBuilder
Re-exports all public items from the db module.

Enums§

FirestoreAggregationOperator
Re-exports all public items from the db module.
FirestoreConsistencySelector
Re-exports all public items from the db module.
FirestoreDbSessionCacheMode
Re-exports all public items from the db module.
FirestoreFieldTransformType
Re-exports all public items from the db module.
FirestoreFindNearestDistanceMeasure
Re-exports all public items from the db module.
FirestoreListenerTargetResumeType
Re-exports all public items from the db module.
FirestoreQueryCollection
Re-exports all public items from the db module.
FirestoreQueryCursor
Re-exports all public items from the db module.
FirestoreQueryDirection
Re-exports all public items from the db module.
FirestoreQueryFilter
Re-exports all public items from the db module.
FirestoreQueryFilterCompare
Re-exports all public items from the db module.
FirestoreQueryFilterCompositeOperator
Re-exports all public items from the db module.
FirestoreQueryFilterUnary
Re-exports all public items from the db module.
FirestoreTargetType
Re-exports all public items from the db module.
FirestoreTransactionMode
Re-exports all public items from the db module.
FirestoreTransformServerValue
Re-exports all public items from the db module.
FirestoreWritePrecondition
Re-exports all public items from the db module.

Constants§

FIREBASE_DEFAULT_DATABASE_ID
Re-exports all public items from the db module.

Traits§

FirestoreAggregatedQuerySupport
Re-exports all public items from the db module.
FirestoreBatchWriter
Re-exports all public items from the db module.
FirestoreCreateSupport
Re-exports all public items from the db module.
FirestoreDeleteSupport
Re-exports all public items from the db module.
FirestoreGetByIdSupport
Re-exports all public items from the db module.
FirestoreListenSupport
Re-exports all public items from the db module.
FirestoreListingSupport
Re-exports all public items from the db module.
FirestoreQuerySupport
Re-exports all public items from the db module.
FirestoreResumeStateStorage
Re-exports all public items from the db module.
FirestoreTransactionOps
Re-exports all public items from the db module.
FirestoreUpdateSupport
Re-exports all public items from the db module.
ValueStruct

Functions§

firestore_doc_get_field_by_path
Re-exports helper functions for working with FirestoreDocuments.
firestore_document_from_map
Re-exports all public items from the firestore_serde module.
firestore_document_from_serializable
Re-exports all public items from the firestore_serde module.
firestore_document_to_serializable
Re-exports all public items from the firestore_serde module.
serialize_latlng_for_firestore
Re-exports all public items from the firestore_serde module.
serialize_reference_for_firestore
Re-exports all public items from the firestore_serde module.
serialize_timestamp_for_firestore
Re-exports all public items from the firestore_serde module.
serialize_vector_for_firestore
Re-exports all public items from the firestore_serde module.

Type Aliases§

FirestoreDocument
A type alias for the raw Firestore document representation.
FirestoreListenEvent
Re-exports all public items from the db module.
FirestoreResult
A type alias for std::result::Result<T, FirestoreError>.
FirestoreTransactionId
Re-exports all public items from the db module.
PeekableBoxStream
Re-exports all public items from the db module.

Attribute Macros§

async_trait

Derive Macros§

ValueStruct