1use serde::{Deserialize, Serialize};
2
3pub trait ToConnectionUrl {
4 fn to_connection_url(&self) -> String;
5}
6
7#[derive(Debug, Clone, Serialize, Deserialize, Default)]
8pub struct DatabaseConfig {
9 #[serde(default)]
11 pub mongodb: MongoDbConfig,
12 #[serde(default)]
14 pub mysql: MySqlConfig,
15 #[serde(default)]
17 pub postgresql: PostgreSqlConfig,
18 #[serde(default)]
20 pub sqlite: SqliteConfig,
21 #[serde(default)]
23 pub qdrant: QdrantConfig,
24 #[serde(default)]
26 pub seekdb: SeekDbConfig,
27 #[serde(default)]
29 pub surrealdb: SurrealDbConfig,
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct MongoDbConfig {
34 pub host: String,
36 pub port: u16,
38 pub database: String,
40 pub username: String,
42 pub password: String,
44 pub max_pool_size: u32,
46 pub connect_timeout: u64,
48}
49
50impl Default for MongoDbConfig {
51 fn default() -> Self {
52 Self {
53 host: "localhost".to_string(),
54 port: 27017,
55 database: "".to_string(),
56 username: "".to_string(),
57 password: "".to_string(),
58 max_pool_size: 10,
59 connect_timeout: 10,
60 }
61 }
62}
63
64impl ToConnectionUrl for MongoDbConfig {
65 fn to_connection_url(&self) -> String {
66 if self.username.is_empty() {
67 format!("mongodb://{}:{}/{}", self.host, self.port, self.database)
68 } else {
69 format!(
70 "mongodb://{}:{}@{}:{}/{}",
71 self.username, self.password, self.host, self.port, self.database
72 )
73 }
74 }
75}
76
77#[derive(Debug, Clone, Serialize, Deserialize)]
78pub struct MySqlConfig {
79 pub host: String,
81 pub port: u16,
83 pub database: String,
85 pub username: String,
87 pub password: String,
89 pub max_pool_size: u32,
91 pub connect_timeout: u64,
93}
94
95impl Default for MySqlConfig {
96 fn default() -> Self {
97 Self {
98 host: "localhost".to_string(),
99 port: 3306,
100 database: "".to_string(),
101 username: "".to_string(),
102 password: "".to_string(),
103 max_pool_size: 10,
104 connect_timeout: 10,
105 }
106 }
107}
108
109impl ToConnectionUrl for MySqlConfig {
110 fn to_connection_url(&self) -> String {
111 format!(
112 "mysql://{}:{}@{}:{}/{}",
113 self.username, self.password, self.host, self.port, self.database
114 )
115 }
116}
117
118#[derive(Debug, Clone, Serialize, Deserialize)]
119pub struct PostgreSqlConfig {
120 pub host: String,
122 pub port: u16,
124 pub database: String,
126 pub username: String,
128 pub password: String,
130 pub max_pool_size: u32,
132 pub connect_timeout: u64,
134}
135
136impl Default for PostgreSqlConfig {
137 fn default() -> Self {
138 Self {
139 host: "localhost".to_string(),
140 port: 5432,
141 database: "".to_string(),
142 username: "".to_string(),
143 password: "".to_string(),
144 max_pool_size: 10,
145 connect_timeout: 10,
146 }
147 }
148}
149
150impl ToConnectionUrl for PostgreSqlConfig {
151 fn to_connection_url(&self) -> String {
152 format!(
153 "postgresql://{}:{}@{}:{}/{}",
154 self.username, self.password, self.host, self.port, self.database
155 )
156 }
157}
158
159#[derive(Debug, Clone, Serialize, Deserialize)]
160pub struct SqliteConfig {
161 pub path: String,
163 pub max_pool_size: u32,
165}
166
167impl Default for SqliteConfig {
168 fn default() -> Self {
169 Self {
170 path: "./data.db".to_string(),
171 max_pool_size: 10,
172 }
173 }
174}
175
176impl ToConnectionUrl for SqliteConfig {
177 fn to_connection_url(&self) -> String {
178 format!("sqlite://{}", self.path)
179 }
180}
181
182#[derive(Debug, Clone, Serialize, Deserialize)]
183pub struct QdrantConfig {
184 pub host: String,
186 pub port: u16,
188 pub api_key: Option<String>,
190 pub use_https: bool,
192}
193
194impl Default for QdrantConfig {
195 fn default() -> Self {
196 Self {
197 host: "localhost".to_string(),
198 port: 6333,
199 api_key: None,
200 use_https: false,
201 }
202 }
203}
204
205impl ToConnectionUrl for QdrantConfig {
206 fn to_connection_url(&self) -> String {
207 let protocol = if self.use_https { "https" } else { "http" };
208 format!("{}://{}:{}", protocol, self.host, self.port)
209 }
210}
211
212#[derive(Debug, Clone, Serialize, Deserialize)]
213pub struct SeekDbConfig {
214 pub host: String,
216 pub port: u16,
218 pub database: String,
220 pub username: String,
222 pub password: String,
224}
225
226impl Default for SeekDbConfig {
227 fn default() -> Self {
228 Self {
229 host: "localhost".to_string(),
230 port: 2881,
231 database: "".to_string(),
232 username: "".to_string(),
233 password: "".to_string(),
234 }
235 }
236}
237
238impl ToConnectionUrl for SeekDbConfig {
239 fn to_connection_url(&self) -> String {
240 if self.username.is_empty() {
241 format!("seekdb://{}:{}/{}", self.host, self.port, self.database)
242 } else {
243 format!(
244 "seekdb://{}:{}@{}:{}/{}",
245 self.username, self.password, self.host, self.port, self.database
246 )
247 }
248 }
249}
250
251#[derive(Debug, Clone, Serialize, Deserialize)]
252pub struct SurrealDbConfig {
253 pub host: String,
255 pub port: u16,
257 pub namespace: String,
259 pub database: String,
261 pub username: String,
263 pub password: String,
265 pub use_https: bool,
267}
268
269impl Default for SurrealDbConfig {
270 fn default() -> Self {
271 Self {
272 host: "localhost".to_string(),
273 port: 9090,
274 namespace: "".to_string(),
275 database: "".to_string(),
276 username: "".to_string(),
277 password: "".to_string(),
278 use_https: false,
279 }
280 }
281}
282
283impl ToConnectionUrl for SurrealDbConfig {
284 fn to_connection_url(&self) -> String {
285 let protocol = if self.use_https { "wss" } else { "ws" };
286 if self.username.is_empty() {
287 format!("{}://{}:{}", protocol, self.host, self.port)
288 } else {
289 format!(
290 "{}://{}:{}@{}:{}",
291 protocol, self.username, self.password, self.host, self.port
292 )
293 }
294 }
295}