1pub mod grpc {
2 tonic::include_proto!("grpc");
3}
4
5pub mod collection;
6pub mod database;
7use bson::Document;
8use grpc::rus_db_client::RusDbClient;
9use grpc::*;
10use serde::{de::DeserializeOwned, Serialize};
11use tonic::{transport::Channel, Request, Response, Status};
12
13pub use bson;
14pub use collection::{RusCollection, RusDocument};
15pub use database::RusDatabase;
16pub use tonic;
17
18#[derive(Clone)]
19pub struct RusDbConnection {
20 client: RusDbClient<Channel>,
21}
22
23impl RusDbConnection {
24 pub async fn connect(dst: &'static str) -> Self {
25 let client = RusDbClient::connect(dst).await.unwrap();
26 Self { client }
27 }
28 pub fn collection<T>(&self, collection: &str) -> RusCollection<T>
29 where
30 T: Serialize + DeserializeOwned + Clone + std::fmt::Debug,
31 {
32 RusCollection::create(collection.to_string(), self.clone())
33 }
34 pub async fn insert(
35 &mut self,
36 collection: &str,
37 document: Document,
38 return_old: bool,
39 ) -> Result<Response<InsertResponses>, Status> {
40 self.client
41 .insert(Request::new(InsertRequest {
42 collection: collection.to_string(),
43 documents: vec![bson::to_vec(&document).unwrap()],
44 return_old,
45 }))
46 .await
47 }
48 pub async fn insert_many(
49 &mut self,
50 collection: &str,
51 documents: Vec<Document>,
52 return_old: bool,
53 ) -> Result<Response<InsertResponses>, Status> {
54 self.client
55 .insert(Request::new(InsertRequest {
56 collection: collection.to_string(),
57 documents: documents
58 .into_iter()
59 .map(|v| bson::to_vec(&v).unwrap())
60 .collect(),
61 return_old,
62 }))
63 .await
64 }
65 pub async fn update(
66 &mut self,
67 collection: &str,
68 filter: Document,
69 updates: Document,
70 limit: Option<u32>,
71 ) -> Result<Response<UpdateResponses>, Status> {
72 self.client
73 .update(Request::new(UpdateRequest {
74 collection: collection.to_string(),
75 filter: bson::to_vec(&filter).unwrap(),
76 updates: bson::to_vec(&updates).unwrap(),
77 limit,
78 }))
79 .await
80 }
81 pub async fn remove(
82 &mut self,
83 collection: &str,
84 filter: Document,
85 limit: Option<u32>,
86 ) -> Result<Response<RemoveResponse>, Status> {
87 self.client
88 .remove(Request::new(RemoveRequest {
89 collection: collection.to_string(),
90 filter: bson::to_vec(&filter).unwrap(),
91 limit,
92 }))
93 .await
94 }
95 pub async fn get(
96 &mut self,
97 collection: &str,
98 id: &str,
99 ) -> Result<Response<GetResponse>, Status> {
100 self.client
101 .get(Request::new(GetRequest {
102 collection: collection.to_string(),
103 id: id.to_string(),
104 }))
105 .await
106 }
107 pub async fn find(
108 &mut self,
109 collection: &str,
110 filter: Option<Document>,
111 limit: Option<u32>,
112 ) -> Result<Response<FindResponse>, Status> {
113 let filter = {
114 if let Some(doc) = filter {
115 Some(bson::to_vec(&doc).unwrap())
116 } else {
117 None
118 }
119 };
120 self.client
121 .find(Request::new(FindRequest {
122 collection: collection.to_string(),
123 filter,
124 limit,
125 }))
126 .await
127 }
128}
129
130#[cfg(test)]
131mod tests {
132 use crate::RusDbConnection;
133 use bson::{bson, doc};
134 use serde::{Deserialize, Serialize};
135
136 #[derive(Serialize, Deserialize, Clone, Default, Debug)]
137 pub struct TestDoc {
138 hello: String,
139 }
140
141 #[tokio::test]
142 async fn it_works() {
143 let client = RusDbConnection::connect("http://127.0.0.1:8009").await;
144 let mut col = client.collection::<TestDoc>("test");
145 col.truncate().await.unwrap();
146 let doca = col
147 .insert(TestDoc {
148 hello: "world".to_string(),
149 })
150 .await
151 .unwrap();
152 println!("{:?}", doca);
153 let mut res_find = col.find_all(None).await.unwrap();
154 println!("{:?}", res_find);
155 for doc in res_find.drain(..) {
156 doc.delete().await.unwrap();
157 }
158 }
159}