predawn_sea_orm/
data_sources.rs1use std::{collections::HashMap, sync::Arc};
2
3use rudi::SingleOwner;
4use sea_orm::DatabaseConnection;
5
6use crate::{inner::Inner, DataSource, Error, DEFAULT_DATA_SOURCE};
7
8#[derive(Debug)]
9pub struct DataSources(HashMap<Arc<str>, DataSource>);
10
11impl DataSources {
12 pub fn with_default(conn: DatabaseConnection) -> Self {
13 let name = Arc::<str>::from(DEFAULT_DATA_SOURCE);
14
15 let mut map = HashMap::new();
16 map.insert(name.clone(), DataSource::new(name, conn));
17
18 Self(map)
19 }
20
21 pub fn new(map: HashMap<Arc<str>, DatabaseConnection>) -> Self {
22 let map = map
23 .into_iter()
24 .map(|(name, conn)| (name.clone(), DataSource::new(name, conn)))
25 .collect();
26
27 Self(map)
28 }
29
30 pub fn get(&self, name: &str) -> Option<&DataSource> {
31 self.0.get(name)
32 }
33
34 pub fn standalone(&self) -> Self {
35 let map = self
36 .0
37 .iter()
38 .map(|(name, conn)| (name.clone(), conn.standalone()))
39 .collect();
40
41 Self(map)
42 }
43
44 pub async fn commit_all(&self) -> Result<(), Error> {
45 for source in self.0.values() {
46 source.commit_all().await?;
47 }
48
49 Ok(())
50 }
51
52 pub async fn rollback_all(&self) -> Result<(), Error> {
53 for source in self.0.values() {
54 source.rollback_all().await?;
55 }
56
57 Ok(())
58 }
59}
60
61#[SingleOwner]
62impl DataSources {
63 #[di]
64 async fn inject(Inner(map): Inner) -> Self {
65 Self::new(map)
66 }
67}