-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathnested_collections.rs
120 lines (101 loc) · 3.21 KB
/
nested_collections.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use firestore::*;
use futures::stream::BoxStream;
use serde::{Deserialize, Serialize};
use tokio_stream::StreamExt;
pub fn config_env_var(name: &str) -> Result<String, String> {
std::env::var(name).map_err(|e| format!("{}: {}", name, e))
}
#[derive(Debug, Clone, Deserialize, Serialize)]
struct MyParentStructure {
some_id: String,
some_string: String,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
struct MyChildStructure {
some_id: String,
another_string: String,
}
#[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_PARENT_COLLECTION_NAME: &'static str = "nested-test";
const TEST_CHILD_COLLECTION_NAME: &'static str = "test-childs";
println!("Creating a parent doc/collection");
let parent_struct = MyParentStructure {
some_id: "test-parent".to_string(),
some_string: "Test".to_string(),
};
// Remove if it already exist
db.fluent()
.delete()
.from(TEST_PARENT_COLLECTION_NAME)
.document_id(&parent_struct.some_id)
.execute()
.await?;
// Creating a parent doc
db.fluent()
.insert()
.into(TEST_PARENT_COLLECTION_NAME)
.document_id(&parent_struct.some_id)
.object(&parent_struct)
.execute::<()>()
.await?;
// Creating a child doc
let child_struct = MyChildStructure {
some_id: "test-child".to_string(),
another_string: "TestChild".to_string(),
};
// The doc path where we store our childs
let parent_path = db.parent_path(TEST_PARENT_COLLECTION_NAME, parent_struct.some_id)?;
// Remove child doc if exists
db.fluent()
.delete()
.from(TEST_CHILD_COLLECTION_NAME)
.parent(&parent_path)
.document_id(&child_struct.some_id)
.execute()
.await?;
// Create a child doc
db.fluent()
.insert()
.into(TEST_CHILD_COLLECTION_NAME)
.document_id(&child_struct.some_id)
.parent(&parent_path)
.object(&child_struct)
.execute::<()>()
.await?;
println!("Listing all children");
let list_stream: BoxStream<MyChildStructure> = db
.fluent()
.list()
.from(TEST_CHILD_COLLECTION_NAME)
.parent(&parent_path)
.obj()
.stream_all()
.await?;
let as_vec: Vec<MyChildStructure> = list_stream.collect().await;
println!("{:?}", as_vec);
println!("Querying in children");
let query_stream: BoxStream<MyChildStructure> = db
.fluent()
.select()
.from(TEST_CHILD_COLLECTION_NAME)
.parent(&parent_path)
.filter(|q| {
q.for_all([q
.field(path!(MyChildStructure::another_string))
.eq("TestChild")])
})
.obj()
.stream_query()
.await?;
let as_vec: Vec<MyChildStructure> = query_stream.collect().await;
println!("{:?}", as_vec);
Ok(())
}