tiny_firestore_odm/
database.rs

1use google_authz::TokenSource;
2use serde::{de::DeserializeOwned, Serialize};
3use std::sync::Arc;
4use tokio::sync::Mutex;
5
6use crate::client::get_client;
7use crate::dynamic_firestore_client::SharedFirestoreClient;
8use crate::{Collection, CollectionName};
9
10/// Represents a Firestore database.
11pub struct Database {
12    client: SharedFirestoreClient,
13    project_id: String,
14}
15
16impl Database {
17    pub async fn new(token_source: TokenSource, project_id: &str) -> Self {
18        let client = Arc::new(Mutex::new(get_client(token_source).await.unwrap()));
19        Database {
20            client,
21            project_id: project_id.to_string(),
22        }
23    }
24
25    pub fn new_from_client(client: SharedFirestoreClient, project_id: &str) -> Self {
26        Database {
27            client,
28            project_id: project_id.to_string(),
29        }
30    }
31
32    /// Returns a top-level collection from this database.
33    pub fn collection<T>(&self, name: &str) -> Collection<T>
34    where
35        T: Serialize + DeserializeOwned + 'static + Unpin,
36    {
37        let name = CollectionName::new(&self.project_id, name);
38        Collection::new(self.client.clone(), name)
39    }
40}