Module firestore

Module firestore 

Source
Expand description

§Firebase Firestore module

This module ports core pieces of the Firestore SDK to Rust so applications can discover collections and create, update, retrieve and delete documents.

It provides functionality to interact with Firestore, including retrieving and querying documents, working with collections, and managing real-time updates.

It includes error handling, configuration options, and integration with Firebase apps.

§Features

  • Connect to Firestore emulator
  • Get Firestore instance for a Firebase app
  • Register Firestore component
  • Manage collections and documents
  • Build and execute queries
  • Comprehensive error handling

§References to the Firebase JS SDK - firestore module

§Development status as of 14th October 2025

  • Core functionalities: Mostly implemented (see the module’s README.md for details)
  • Tests: 31 tests (passed)
  • Documentation: Most public functions are documented
  • Examples: None provided

DISCLAIMER: This is not an official Firebase product, nor it is guaranteed that it has no bugs or that it will work as intended.

§Example

use firebase_rs_sdk::app::api::initialize_app;
use firebase_rs_sdk::app::{FirebaseAppSettings, FirebaseOptions};
use firebase_rs_sdk::app_check::api::{custom_provider, initialize_app_check, token_with_ttl};
use firebase_rs_sdk::app_check::{AppCheckOptions, FirebaseAppCheckInternal};
use firebase_rs_sdk::auth::api::auth_for_app;
use firebase_rs_sdk::firestore::*;
use std::collections::BTreeMap;
use std::time::Duration;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // TODO: replace with your project configuration
    let options = FirebaseOptions {
        project_id: Some("your-project".into()),
        // add other Firebase options as needed
        ..Default::default()
    };
    let app = initialize_app(options, Some(FirebaseAppSettings::default()))?;
    let auth = auth_for_app(app.clone())?;

    // Optional: wire App Check tokens into Firestore.
    let app_check_provider = custom_provider(|| token_with_ttl("fake-token", Duration::from_secs(60)));
    let app_check = initialize_app_check(Some(app.clone()), AppCheckOptions::new(app_check_provider))?;
    let app_check_internal = FirebaseAppCheckInternal::new(app_check);

    let firestore = get_firestore(Some(app.clone()))?;
    let client = FirestoreClient::with_http_datastore_authenticated(
        firebase_rs_sdk::firestore::api::Firestore::from_arc(firestore.clone()),
        auth.token_provider(),
        Some(app_check_internal.token_provider()),
    )?;

    let mut ada = BTreeMap::new();
    ada.insert("first".into(), FirestoreValue::from_string("Ada"));
    ada.insert("last".into(), FirestoreValue::from_string("Lovelace"));
    ada.insert("born".into(), FirestoreValue::from_integer(1815));
    let ada_snapshot = client.add_doc("users", ada)?;
    println!("Document written with ID: {}", ada_snapshot.id());

    let mut alan = BTreeMap::new();
    alan.insert("first".into(), FirestoreValue::from_string("Alan"));
    alan.insert("middle".into(), FirestoreValue::from_string("Mathison"));
    alan.insert("last".into(), FirestoreValue::from_string("Turing"));
    alan.insert("born".into(), FirestoreValue::from_integer(1912));
    let alan_snapshot = client.add_doc("users", alan)?;
    println!("Document written with ID: {}", alan_snapshot.id());

    Ok(())
}

If App Check is not enabled for your app, pass None as the third argument to with_http_datastore_authenticated.

Using Converters:

use firebase_rs_sdk::app::*;
use firebase_rs_sdk::firestore::*;
use std::collections::BTreeMap;

#[derive(Clone)]
struct MyUser {
   name: String,
}

#[derive(Clone)]
struct UserConverter;

impl FirestoreDataConverter for UserConverter {
    type Model = MyUser;

    fn to_map(
        &self,
        value: &Self::Model,
    ) -> FirestoreResult<BTreeMap<String, FirestoreValue>> {
        // Encode your model into Firestore fields.
        todo!()
    }

    fn from_map(&self, value: &MapValue) -> FirestoreResult<Self::Model> {
        // Decode Firestore fields into your model.
        todo!()
    }
}

fn example_with_converter(firestore: &Firestore, client: &FirestoreClient) -> FirestoreResult<Option<MyUser>> {
    let users = firestore.collection("typed-users")?.with_converter(UserConverter);
    let doc = users.doc(Some("ada"))?;
    client.set_doc_with_converter(&doc, MyUser { name: "Ada".to_string() }, None)?;
    let typed_snapshot = client.get_doc_with_converter(&doc)?;
    let user: Option<MyUser> = typed_snapshot.data()?;
    Ok(user)
}

Modules§

api
error
model
remote
value

Structs§

ArrayValue
BytesValue
CollectionReference
Connection
ConnectionBuilder
ConvertedCollectionReference
ConvertedDocumentReference
ConvertedQuery
DatabaseId
DocumentKey
DocumentReference
DocumentSnapshot
Snapshot of a document’s contents.
FieldPath
Firestore
FirestoreClient
FirestoreError
FirestoreValue
GeoPoint
HttpDatastore
HttpDatastoreBuilder
InMemoryDatastore
JsonProtoSerializer
MapValue
NoopTokenProvider
PassthroughConverter
Default converter that leaves Firestore maps unchanged (raw JSON-style data).
Query
QueryDefinition
QuerySnapshot
RequestContext
ResourcePath
RetrySettings
SetOptions
Options that configure the behaviour of set_doc writes.
SnapshotMetadata
Metadata about the state of a document snapshot.
Timestamp
TypedDocumentSnapshot
Document snapshot carrying a converter for typed access.
TypedQuerySnapshot

Enums§

FilterOperator
FirestoreErrorCode
LimitType
OrderDirection
ValueKind

Constants§

DEFAULT_DATABASE_ID
FIRESTORE_COMPONENT_NAME

Traits§

Datastore
FirestoreDataConverter
Trait describing how to convert between user models and Firestore maps.
TokenProvider

Functions§

get_firestore
Resolves (or lazily instantiates) the Firestore service for the provided app.
map_http_error
register_firestore_component

Type Aliases§

FirestoreResult
TokenProviderArc