atomr_persistence_mongodb/
config.rs1use std::env;
4
5#[derive(Debug, Clone)]
6pub struct MongoConfig {
7 pub url: String,
8 pub database: String,
9 pub journal_collection: String,
10 pub snapshot_collection: String,
11}
12
13impl MongoConfig {
14 pub fn new(url: impl Into<String>, database: impl Into<String>) -> Self {
15 Self {
16 url: url.into(),
17 database: database.into(),
18 journal_collection: "event_journal".into(),
19 snapshot_collection: "snapshot_store".into(),
20 }
21 }
22
23 pub fn with_collections(mut self, journal: impl Into<String>, snapshot: impl Into<String>) -> Self {
24 self.journal_collection = journal.into();
25 self.snapshot_collection = snapshot.into();
26 self
27 }
28
29 pub fn from_env() -> Self {
32 let url = env::var("ATOMR_PERSISTENCE_MONGO_URL")
33 .or_else(|_| env::var("ATOMR_IT_MONGO_URL"))
34 .or_else(|_| env::var("MONGODB_URL"))
35 .unwrap_or_else(|_| "mongodb://127.0.0.1:27017".to_string());
36 let db = env::var("ATOMR_PERSISTENCE_MONGO_DB").unwrap_or_else(|_| "atomr".into());
37 Self::new(url, db)
38 }
39}
40
41#[cfg(test)]
42mod tests {
43 use super::*;
44
45 #[test]
46 fn defaults() {
47 let cfg = MongoConfig::new("mongodb://x", "atomr");
48 assert_eq!(cfg.journal_collection, "event_journal");
49 assert_eq!(cfg.snapshot_collection, "snapshot_store");
50 }
51
52 #[test]
53 fn custom_collections() {
54 let cfg = MongoConfig::new("mongodb://x", "db").with_collections("j", "s");
55 assert_eq!(cfg.journal_collection, "j");
56 assert_eq!(cfg.snapshot_collection, "s");
57 }
58}