forked from abdolence/firestore-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_doc_level_crud.rs
73 lines (63 loc) · 2.11 KB
/
dynamic_doc_level_crud.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use chrono::prelude::*;
use firestore::*;
pub fn config_env_var(name: &str) -> Result<String, String> {
std::env::var(name).map_err(|e| format!("{}: {}", name, e))
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
// Logging with debug enabled
let subscriber = tracing_subscriber::fmt()
.with_env_filter("firestore=debug")
.finish();
tracing::subscriber::set_global_default(subscriber)?;
// Create an instance
let db = FirestoreDb::new(&config_env_var("PROJECT_ID")?).await?;
const TEST_COLLECTION_NAME: &'static str = "test";
db.fluent()
.delete()
.from(TEST_COLLECTION_NAME)
.document_id("test-1")
.execute()
.await?;
let object_returned = db
.fluent()
.insert()
.into(TEST_COLLECTION_NAME)
.document_id("test-1")
.document(FirestoreDb::serialize_map_to_doc(
"",
[
("some_id", "some-id-value".into()),
("some_string", "some-string-value".into()),
("one_more_string", "another-string-value".into()),
("some_num", 41.into()),
(
"embedded_obj",
FirestoreValue::from_map([
("inner_some_id", "inner-value".into()),
("inner_some_string", "inner-value".into()),
]),
),
("created_at", FirestoreTimestamp(Utc::now()).into()),
],
)?)
.execute()
.await?;
println!("Created {:?}", object_returned);
let object_updated = db
.fluent()
.update()
.fields(["some_num", "one_more_string"])
.in_col(TEST_COLLECTION_NAME)
.document(FirestoreDb::serialize_map_to_doc(
db.parent_path(TEST_COLLECTION_NAME, "test-1")?,
[
("one_more_string", "update-string".into()),
("some_num", 42.into()),
],
)?)
.execute()
.await?;
println!("Updated {:?}", object_updated);
Ok(())
}