predawn_sea_orm/
inner.rs

1use std::{collections::HashMap, sync::Arc};
2
3use rudi::Singleton;
4use sea_orm::{Database, DatabaseConnection};
5
6use crate::DataSourcesConfig;
7
8#[derive(Debug, Clone)]
9pub(crate) struct Inner(pub(crate) HashMap<Arc<str>, DatabaseConnection>);
10
11#[Singleton]
12impl Inner {
13    #[di]
14    async fn new(cfg: DataSourcesConfig) -> Self {
15        let DataSourcesConfig { data_sources } = cfg;
16
17        let mut map = HashMap::with_capacity(data_sources.len());
18
19        for (name, options) in data_sources {
20            let conn = Database::connect(options)
21                .await
22                .expect("failed to create data sources");
23
24            let _ = map.insert(name.into(), conn);
25        }
26
27        Self(map)
28    }
29}