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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use std::collections::HashMap;
use arangors::client::reqwest::ReqwestClient;
use arangors::{Collection, Connection, Database};
use serde_json::Value;
use crate::db::database_collection::DatabaseCollection;
use crate::db::database_connection_pool_builder::{
DatabaseConnectionPoolBuilder, DatabaseSchemaOption, PoolCredentialsOption,
};
use crate::query::JsonQueryResult;
use crate::schema::{DatabaseSchema, SchemaDatabaseOperation};
use crate::ServiceError;
#[derive(Clone)]
pub struct DatabaseConnectionPool {
pub collections: HashMap<String, DatabaseCollection>,
pub database: Database<ReqwestClient>,
}
#[derive(Debug, Clone)]
pub enum AuthMode {
Basic,
Jwt,
}
impl Default for AuthMode {
fn default() -> Self {
Self::Basic
}
}
impl DatabaseConnectionPool {
pub fn builder() -> DatabaseConnectionPoolBuilder {
DatabaseConnectionPoolBuilder {
apply_schema: false,
auth_mode: AuthMode::default(),
credentials: PoolCredentialsOption::Auto,
schema: DatabaseSchemaOption::Auto,
}
}
#[maybe_async::maybe_async]
pub(crate) async fn new(
db_host: &str,
db_name: &str,
db_user: &str,
db_password: &str,
auth_mode: AuthMode,
mut schema: DatabaseSchema,
apply_schema: bool,
) -> Result<Self, ServiceError> {
log::info!("Connecting to database server...");
let db_connection = match auth_mode {
AuthMode::Basic => {
Connection::establish_basic_auth(db_host, db_user, db_password).await?
}
AuthMode::Jwt => Connection::establish_jwt(db_host, db_user, db_password).await?,
};
log::info!("Connected to database server.");
let database = db_connection.db(&db_name).await.unwrap();
if apply_schema {
schema.apply_to_database(&database, true).await?
}
DatabaseConnectionPool::load_schema(database, schema).await
}
pub fn get_collection(&self, collection: &str) -> &Collection<ReqwestClient> {
if !self.collections.contains_key(collection) {
panic!(
"Undefined collection {}, check your schema.yaml file",
collection
)
}
&self.collections[collection].collection
}
#[maybe_async::maybe_async]
pub async fn truncate(&self) {
for collection in self.collections.iter() {
collection.1.collection.truncate().await.unwrap();
}
}
#[maybe_async::maybe_async]
pub async fn aql_get(&self, aql: &str) -> Result<JsonQueryResult, ServiceError> {
let query_result: Vec<Value> = match self.database.aql_str(aql).await {
Ok(value) => value,
Err(error) => {
log::error!("{}", error);
return Err(ServiceError::from(error));
}
};
Ok(JsonQueryResult::new(query_result))
}
#[maybe_async::maybe_async]
async fn load_schema(
database: Database<ReqwestClient>,
schema: DatabaseSchema,
) -> Result<DatabaseConnectionPool, ServiceError> {
let mut collections = HashMap::new();
for collection in schema.collections.iter() {
collections.insert(
collection.name.clone(),
DatabaseCollection {
collection_name: collection.name.clone(),
collection: collection.get(&database).await?,
},
);
}
Ok(DatabaseConnectionPool {
collections,
database,
})
}
}