firebase_rs_sdk/firestore/remote/datastore/
mod.rs

1use std::collections::BTreeMap;
2use std::sync::Arc;
3
4use async_trait::async_trait;
5#[cfg(not(target_arch = "wasm32"))]
6use futures::future::BoxFuture;
7#[cfg(target_arch = "wasm32")]
8use futures::future::LocalBoxFuture;
9
10use crate::firestore::api::snapshot::DocumentSnapshot;
11use crate::firestore::error::FirestoreResult;
12use crate::firestore::model::{DocumentKey, FieldPath};
13use crate::firestore::value::{FirestoreValue, MapValue};
14use crate::firestore::AggregateDefinition;
15use crate::firestore::FieldTransform;
16use crate::firestore::QueryDefinition;
17
18mod http;
19mod in_memory;
20mod streaming;
21
22// Re-export public API
23pub use http::{HttpDatastore, HttpDatastoreBuilder, RetrySettings};
24pub use in_memory::InMemoryDatastore;
25pub(crate) use streaming::box_stream_future;
26pub use streaming::{StreamingDatastoreImpl, StreamingHandleImpl};
27
28#[derive(Clone, Debug)]
29pub enum WriteOperation {
30    Set {
31        key: DocumentKey,
32        data: MapValue,
33        mask: Option<Vec<FieldPath>>,
34        transforms: Vec<FieldTransform>,
35    },
36    Update {
37        key: DocumentKey,
38        data: MapValue,
39        field_paths: Vec<FieldPath>,
40        transforms: Vec<FieldTransform>,
41    },
42    Delete {
43        key: DocumentKey,
44    },
45}
46
47impl WriteOperation {
48    /// Returns the document key targeted by this write.
49    pub fn key(&self) -> &DocumentKey {
50        match self {
51            WriteOperation::Set { key, .. }
52            | WriteOperation::Update { key, .. }
53            | WriteOperation::Delete { key } => key,
54        }
55    }
56}
57
58#[cfg(target_arch = "wasm32")]
59pub type StreamingFuture<'a, T> = LocalBoxFuture<'a, T>;
60#[cfg(not(target_arch = "wasm32"))]
61pub type StreamingFuture<'a, T> = BoxFuture<'a, T>;
62
63pub trait StreamingDatastore: Send + Sync + 'static {
64    fn open_listen_stream(&self) -> StreamingFuture<'_, FirestoreResult<Arc<dyn StreamHandle>>>;
65    fn open_write_stream(&self) -> StreamingFuture<'_, FirestoreResult<Arc<dyn StreamHandle>>>;
66}
67
68pub trait StreamHandle: Send + Sync + 'static {
69    fn send(&self, payload: Vec<u8>) -> StreamingFuture<'_, FirestoreResult<()>>;
70    fn next(&self) -> StreamingFuture<'_, Option<FirestoreResult<Vec<u8>>>>;
71    fn close(&self) -> StreamingFuture<'_, FirestoreResult<()>>;
72}
73
74#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
75#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
76pub trait Datastore: Send + Sync + 'static {
77    async fn get_document(&self, key: &DocumentKey) -> FirestoreResult<DocumentSnapshot>;
78    async fn set_document(
79        &self,
80        key: &DocumentKey,
81        data: MapValue,
82        mask: Option<Vec<FieldPath>>,
83        transforms: Vec<FieldTransform>,
84    ) -> FirestoreResult<()>;
85    async fn run_query(&self, query: &QueryDefinition) -> FirestoreResult<Vec<DocumentSnapshot>>;
86    async fn update_document(
87        &self,
88        key: &DocumentKey,
89        data: MapValue,
90        field_paths: Vec<FieldPath>,
91        transforms: Vec<FieldTransform>,
92    ) -> FirestoreResult<()>;
93    async fn delete_document(&self, key: &DocumentKey) -> FirestoreResult<()>;
94    async fn commit(&self, writes: Vec<WriteOperation>) -> FirestoreResult<()>;
95    async fn run_aggregate(
96        &self,
97        query: &QueryDefinition,
98        aggregations: &[AggregateDefinition],
99    ) -> FirestoreResult<BTreeMap<String, FirestoreValue>>;
100}
101
102#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
103#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
104pub trait TokenProvider: Send + Sync + 'static {
105    async fn get_token(&self) -> FirestoreResult<Option<String>>;
106    fn invalidate_token(&self);
107    async fn heartbeat_header(&self) -> FirestoreResult<Option<String>> {
108        Ok(None)
109    }
110}
111
112#[derive(Default, Clone)]
113pub struct NoopTokenProvider;
114
115#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
116#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
117impl TokenProvider for NoopTokenProvider {
118    async fn get_token(&self) -> FirestoreResult<Option<String>> {
119        Ok(None)
120    }
121
122    fn invalidate_token(&self) {}
123}
124
125pub type TokenProviderArc = Arc<dyn TokenProvider>;