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 - document_
transform_ builder - errors
- insert_
builder - listing_
builder - select_
aggregation_ builder - select_
builder - select_
filter_ builder - serialize_
as_ null - serialize_
as_ null_ timestamp - serialize_
as_ optional_ timestamp - serialize_
as_ reference - serialize_
as_ timestamp - timestamp_
utils - update_
builder
Macros§
Structs§
- Firestore
Aggregated Query Params - Firestore
Aggregated Query Params Init - Firestore
Aggregation - Firestore
Aggregation Init - Firestore
Aggregation Operator Avg - Firestore
Aggregation Operator AvgInit - Firestore
Aggregation Operator Count - Firestore
Aggregation Operator Count Init - Firestore
Aggregation Operator Sum - Firestore
Aggregation Operator SumInit - Firestore
Batch - Firestore
Batch Write Response - Firestore
Batch Write Response Init - Firestore
Collection Documents - Firestore
Collection Documents Init - Firestore
Db - Firestore
DbOptions - Firestore
DbOptions Init - Firestore
DbSession Params - Firestore
DbSession Params Init - Firestore
Document Metadata - Firestore
Document Metadata Init - Firestore
Dynamic Struct - Firestore
Dynamic Struct Init - Firestore
Execution Stats - Firestore
Execution Stats Init - Firestore
Explain Metrics - Firestore
Explain Metrics Init - Firestore
Explain Options - Firestore
Explain Options Init - Firestore
Expr Builder - Firestore
Field Transform - Firestore
Field Transform Init - Firestore
Find Nearest Options - Firestore
Find Nearest Options Init - Firestore
GeoPoint - Firestore
LatLng - Firestore
List Collection IdsParams - Firestore
List Collection IdsParams Init - Firestore
List Collection IdsResult - Firestore
List Collection IdsResult Init - Firestore
List DocParams - Firestore
List DocParams Init - Firestore
List DocResult - Firestore
List DocResult Init - Firestore
Listener - Firestore
Listener Params - Firestore
Listener Params Init - Firestore
Listener Target - Firestore
Listener Target Params - Firestore
Listener Target Params Init - Firestore
Listener Token - Firestore
MemListen State Storage - Firestore
Partition - Firestore
Partition Init - Firestore
Partition Query Params - Firestore
Partition Query Params Init - Firestore
Plan Summary - Firestore
Plan Summary Init - Firestore
Query Filter Composite - Firestore
Query Filter Composite Init - Firestore
Query Order - Firestore
Query Order Init - Firestore
Query Params - Firestore
Query Params Init - Firestore
Reference - Firestore
Simple Batch Write Options - Firestore
Simple Batch Write Options Init - Firestore
Simple Batch Writer - Firestore
Streaming Batch Write Options - Firestore
Streaming Batch Write Options Init - Firestore
Streaming Batch Writer - Firestore
Temp Files Listen State Storage - Firestore
Timestamp - Firestore
Transaction - Firestore
Transaction Options - Firestore
Transaction Options Init - Firestore
Transaction Response - Firestore
Transaction Response Init - Firestore
Value - Firestore
Vector - Firestore
With Metadata - Firestore
Write Result - Firestore
Write Result Init - Parent
Path Builder
Enums§
- Firestore
Aggregation Operator - Firestore
Consistency Selector - Firestore
DbSession Cache Mode - Firestore
Field Transform Type - Firestore
Find Nearest Distance Measure - Firestore
Listener Target Resume Type - Firestore
Query Collection - Firestore
Query Cursor - Firestore
Query Direction - Firestore
Query Filter - Firestore
Query Filter Compare - Firestore
Query Filter Composite Operator - Firestore
Query Filter Unary - Firestore
Target Type - Firestore
Transaction Mode - Firestore
Transform Server Value - Firestore
Write Precondition - A precondition on a document, used for conditional operations.
Constants§
Traits§
- Firestore
Aggregated Query Support - Firestore
Batch Writer - Firestore
Create Support - Firestore
Delete Support - Firestore
GetBy IdSupport - Firestore
Listen Support - Firestore
Listing Support - Firestore
Query Support - Firestore
Resume State Storage - Firestore
Update Support - Value
Struct
Functions§
- firestore_
doc_ get_ field_ by_ path - firestore_
document_ from_ map - firestore_
document_ from_ serializable - firestore_
document_ to_ serializable - serialize_
latlng_ for_ firestore - serialize_
reference_ for_ firestore - serialize_
timestamp_ for_ firestore - serialize_
vector_ for_ firestore