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_apimodule. - document_
transform_ builder - Re-exports all public items from the
fluent_apimodule. - errors
- Defines the error types used throughout the
firestore-rscrate. - insert_
builder - Re-exports all public items from the
fluent_apimodule. - listing_
builder - Re-exports all public items from the
fluent_apimodule. - select_
aggregation_ builder - Re-exports all public items from the
fluent_apimodule. - select_
builder - Re-exports all public items from the
fluent_apimodule. - select_
filter_ builder - Re-exports all public items from the
fluent_apimodule. - serialize_
as_ null - Re-exports all public items from the
firestore_serdemodule. - serialize_
as_ null_ timestamp - Re-exports all public items from the
firestore_serdemodule. - serialize_
as_ optional_ timestamp - Re-exports all public items from the
firestore_serdemodule. - serialize_
as_ reference - Re-exports all public items from the
firestore_serdemodule. - serialize_
as_ timestamp - Re-exports all public items from the
firestore_serdemodule. - timestamp_
utils - Provides utility functions for working with Firestore timestamps.
- update_
builder - Re-exports all public items from the
fluent_apimodule.
Macros§
Structs§
- Firestore
Aggregated Query Params - Re-exports all public items from the
dbmodule. - Firestore
Aggregated Query Params Init - Re-exports all public items from the
dbmodule. - Firestore
Aggregation - Re-exports all public items from the
dbmodule. - Firestore
Aggregation Init - Re-exports all public items from the
dbmodule. - Firestore
Aggregation Operator Avg - Re-exports all public items from the
dbmodule. - Firestore
Aggregation Operator AvgInit - Re-exports all public items from the
dbmodule. - Firestore
Aggregation Operator Count - Re-exports all public items from the
dbmodule. - Firestore
Aggregation Operator Count Init - Re-exports all public items from the
dbmodule. - Firestore
Aggregation Operator Sum - Re-exports all public items from the
dbmodule. - Firestore
Aggregation Operator SumInit - Re-exports all public items from the
dbmodule. - Firestore
Batch - Re-exports all public items from the
dbmodule. - Firestore
Batch Write Response - Re-exports all public items from the
dbmodule. - Firestore
Batch Write Response Init - Re-exports all public items from the
dbmodule. - Firestore
Collection Documents - Re-exports all public items from the
dbmodule. - Firestore
Collection Documents Init - Re-exports all public items from the
dbmodule. - Firestore
Db - Re-exports all public items from the
dbmodule. - Firestore
DbOptions - Re-exports all public items from the
dbmodule. - Firestore
DbOptions Init - Re-exports all public items from the
dbmodule. - Firestore
DbSession Params - Re-exports all public items from the
dbmodule. - Firestore
DbSession Params Init - Re-exports all public items from the
dbmodule. - Firestore
Document Metadata - Re-exports all public items from the
firestore_metamodule. - Firestore
Document Metadata Init - Re-exports all public items from the
firestore_metamodule. - Firestore
Dynamic Struct - Re-exports all public items from the
firestore_metamodule. - Firestore
Dynamic Struct Init - Re-exports all public items from the
firestore_metamodule. - Firestore
Execution Stats - Re-exports all public items from the
firestore_metamodule. - Firestore
Execution Stats Init - Re-exports all public items from the
firestore_metamodule. - Firestore
Explain Metrics - Re-exports all public items from the
firestore_metamodule. - Firestore
Explain Metrics Init - Re-exports all public items from the
firestore_metamodule. - Firestore
Explain Options - Re-exports all public items from the
dbmodule. - Firestore
Explain Options Init - Re-exports all public items from the
dbmodule. - Firestore
Expr Builder - Re-exports all public items from the
fluent_apimodule. - Firestore
Field Transform - Re-exports all public items from the
dbmodule. - Firestore
Field Transform Init - Re-exports all public items from the
dbmodule. - Firestore
Find Nearest Options - Re-exports all public items from the
dbmodule. - Firestore
Find Nearest Options Init - Re-exports all public items from the
dbmodule. - Firestore
GeoPoint - Re-exports all public items from the
firestore_serdemodule. - Firestore
LatLng - Re-exports all public items from the
firestore_serdemodule. - Firestore
List Collection IdsParams - Re-exports all public items from the
dbmodule. - Firestore
List Collection IdsParams Init - Re-exports all public items from the
dbmodule. - Firestore
List Collection IdsResult - Re-exports all public items from the
dbmodule. - Firestore
List Collection IdsResult Init - Re-exports all public items from the
dbmodule. - Firestore
List DocParams - Re-exports all public items from the
dbmodule. - Firestore
List DocParams Init - Re-exports all public items from the
dbmodule. - Firestore
List DocResult - Re-exports all public items from the
dbmodule. - Firestore
List DocResult Init - Re-exports all public items from the
dbmodule. - Firestore
Listener - Re-exports all public items from the
dbmodule. - Firestore
Listener Params - Re-exports all public items from the
dbmodule. - Firestore
Listener Params Init - Re-exports all public items from the
dbmodule. - Firestore
Listener Target - Re-exports all public items from the
dbmodule. - Firestore
Listener Target Params - Re-exports all public items from the
dbmodule. - Firestore
Listener Target Params Init - Re-exports all public items from the
dbmodule. - Firestore
Listener Token - Re-exports all public items from the
dbmodule. - Firestore
MemListen State Storage - Re-exports all public items from the
dbmodule. - Firestore
Partition - Re-exports all public items from the
dbmodule. - Firestore
Partition Init - Re-exports all public items from the
dbmodule. - Firestore
Partition Query Params - Re-exports all public items from the
dbmodule. - Firestore
Partition Query Params Init - Re-exports all public items from the
dbmodule. - Firestore
Plan Summary - Re-exports all public items from the
firestore_metamodule. - Firestore
Plan Summary Init - Re-exports all public items from the
firestore_metamodule. - Firestore
Query Filter Composite - Re-exports all public items from the
dbmodule. - Firestore
Query Filter Composite Init - Re-exports all public items from the
dbmodule. - Firestore
Query Order - Re-exports all public items from the
dbmodule. - Firestore
Query Order Init - Re-exports all public items from the
dbmodule. - Firestore
Query Params - Re-exports all public items from the
dbmodule. - Firestore
Query Params Init - Re-exports all public items from the
dbmodule. - Firestore
Reference - Re-exports all public items from the
firestore_serdemodule. - Firestore
Simple Batch Write Options - Re-exports all public items from the
dbmodule. - Firestore
Simple Batch Write Options Init - Re-exports all public items from the
dbmodule. - Firestore
Simple Batch Writer - Re-exports all public items from the
dbmodule. - Firestore
Streaming Batch Write Options - Re-exports all public items from the
dbmodule. - Firestore
Streaming Batch Write Options Init - Re-exports all public items from the
dbmodule. - Firestore
Streaming Batch Writer - Re-exports all public items from the
dbmodule. - Firestore
Temp Files Listen State Storage - Re-exports all public items from the
dbmodule. - Firestore
Timestamp - Re-exports all public items from the
firestore_serdemodule. - Firestore
Transaction - Re-exports all public items from the
dbmodule. - Firestore
Transaction Data - Re-exports all public items from the
dbmodule. - Firestore
Transaction Options - Re-exports all public items from the
dbmodule. - Firestore
Transaction Options Init - Re-exports all public items from the
dbmodule. - Firestore
Transaction Response - Re-exports all public items from the
dbmodule. - Firestore
Transaction Response Init - Re-exports all public items from the
dbmodule. - Firestore
Value - Re-exports all public items from the
firestore_valuemodule. - Firestore
Vector - Re-exports all public items from the
firestore_serdemodule. - Firestore
With Metadata - Re-exports all public items from the
firestore_metamodule. - Firestore
Write Result - Re-exports all public items from the
dbmodule. - Firestore
Write Result Init - Re-exports all public items from the
dbmodule. - Parent
Path Builder - Re-exports all public items from the
dbmodule.
Enums§
- Firestore
Aggregation Operator - Re-exports all public items from the
dbmodule. - Firestore
Consistency Selector - Re-exports all public items from the
dbmodule. - Firestore
DbSession Cache Mode - Re-exports all public items from the
dbmodule. - Firestore
Field Transform Type - Re-exports all public items from the
dbmodule. - Firestore
Find Nearest Distance Measure - Re-exports all public items from the
dbmodule. - Firestore
Listener Target Resume Type - Re-exports all public items from the
dbmodule. - Firestore
Query Collection - Re-exports all public items from the
dbmodule. - Firestore
Query Cursor - Re-exports all public items from the
dbmodule. - Firestore
Query Direction - Re-exports all public items from the
dbmodule. - Firestore
Query Filter - Re-exports all public items from the
dbmodule. - Firestore
Query Filter Compare - Re-exports all public items from the
dbmodule. - Firestore
Query Filter Composite Operator - Re-exports all public items from the
dbmodule. - Firestore
Query Filter Unary - Re-exports all public items from the
dbmodule. - Firestore
Target Type - Re-exports all public items from the
dbmodule. - Firestore
Transaction Mode - Re-exports all public items from the
dbmodule. - Firestore
Transform Server Value - Re-exports all public items from the
dbmodule. - Firestore
Write Precondition - Re-exports all public items from the
dbmodule.
Constants§
- FIREBASE_
DEFAULT_ DATABASE_ ID - Re-exports all public items from the
dbmodule.
Traits§
- Firestore
Aggregated Query Support - Re-exports all public items from the
dbmodule. - Firestore
Batch Writer - Re-exports all public items from the
dbmodule. - Firestore
Create Support - Re-exports all public items from the
dbmodule. - Firestore
Delete Support - Re-exports all public items from the
dbmodule. - Firestore
GetBy IdSupport - Re-exports all public items from the
dbmodule. - Firestore
Listen Support - Re-exports all public items from the
dbmodule. - Firestore
Listing Support - Re-exports all public items from the
dbmodule. - Firestore
Query Support - Re-exports all public items from the
dbmodule. - Firestore
Resume State Storage - Re-exports all public items from the
dbmodule. - Firestore
Transaction Ops - Re-exports all public items from the
dbmodule. - Firestore
Update Support - Re-exports all public items from the
dbmodule. - Value
Struct
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_serdemodule. - firestore_
document_ from_ serializable - Re-exports all public items from the
firestore_serdemodule. - firestore_
document_ to_ serializable - Re-exports all public items from the
firestore_serdemodule. - serialize_
latlng_ for_ firestore - Re-exports all public items from the
firestore_serdemodule. - serialize_
reference_ for_ firestore - Re-exports all public items from the
firestore_serdemodule. - serialize_
timestamp_ for_ firestore - Re-exports all public items from the
firestore_serdemodule. - serialize_
vector_ for_ firestore - Re-exports all public items from the
firestore_serdemodule.
Type Aliases§
- Firestore
Document - A type alias for the raw Firestore document representation.
- Firestore
Listen Event - Re-exports all public items from the
dbmodule. - Firestore
Result - A type alias for
std::result::Result<T, FirestoreError>. - Firestore
Transaction Id - Re-exports all public items from the
dbmodule. - Peekable
BoxStream - Re-exports all public items from the
dbmodule.