1
//! Pure MongoDB model.
2

            
3
use std::{error::Error as StdError, sync::Arc};
4

            
5
use async_trait::async_trait;
6
use mongodb::Database;
7

            
8
use super::{
9
    application_dldata, application_uldata, coremgr_opdata,
10
    mongodb::{
11
        application_dldata::Model as ApplicationDlDataModel,
12
        application_uldata::Model as ApplicationUlDataModel,
13
        conn::{self, Options},
14
        coremgr_opdata::Model as CoremgrOpDataModel,
15
        network_dldata::Model as NetworkDlDataModel,
16
        network_uldata::Model as NetworkUlDataModel,
17
    },
18
    network_dldata, network_uldata,
19
};
20

            
21
/// Pure MongoDB model.
22
#[derive(Clone)]
23
pub struct Model {
24
    conn: Arc<Database>,
25
    application_dldata: Arc<ApplicationDlDataModel>,
26
    application_uldata: Arc<ApplicationUlDataModel>,
27
    coremgr_opdata: Arc<CoremgrOpDataModel>,
28
    network_dldata: Arc<NetworkDlDataModel>,
29
    network_uldata: Arc<NetworkUlDataModel>,
30
}
31

            
32
impl Model {
33
    /// Create an instance.
34
7
    pub async fn new(opts: &Options) -> Result<Self, Box<dyn StdError>> {
35
36
        let conn = Arc::new(conn::connect(opts).await?);
36
        Ok(Model {
37
7
            conn: conn.clone(),
38
14
            application_dldata: Arc::new(ApplicationDlDataModel::new(conn.clone()).await?),
39
14
            application_uldata: Arc::new(ApplicationUlDataModel::new(conn.clone()).await?),
40
14
            coremgr_opdata: Arc::new(CoremgrOpDataModel::new(conn.clone()).await?),
41
14
            network_dldata: Arc::new(NetworkDlDataModel::new(conn.clone()).await?),
42
14
            network_uldata: Arc::new(NetworkUlDataModel::new(conn.clone()).await?),
43
        })
44
7
    }
45

            
46
    /// Get the raw database connection ([`Database`]).
47
154
    pub fn get_connection(&self) -> &Database {
48
154
        &self.conn
49
154
    }
50
}
51

            
52
#[async_trait]
53
impl super::Model for Model {
54
4
    async fn close(&self) -> Result<(), Box<dyn StdError>> {
55
4
        Ok(())
56
8
    }
57

            
58
526
    fn application_dldata(&self) -> &dyn application_dldata::ApplicationDlDataModel {
59
526
        self.application_dldata.as_ref()
60
526
    }
61

            
62
535
    fn application_uldata(&self) -> &dyn application_uldata::ApplicationUlDataModel {
63
535
        self.application_uldata.as_ref()
64
535
    }
65

            
66
485
    fn coremgr_opdata(&self) -> &dyn coremgr_opdata::CoremgrOpDataModel {
67
485
        self.coremgr_opdata.as_ref()
68
485
    }
69

            
70
539
    fn network_dldata(&self) -> &dyn network_dldata::NetworkDlDataModel {
71
539
        self.network_dldata.as_ref()
72
539
    }
73

            
74
521
    fn network_uldata(&self) -> &dyn network_uldata::NetworkUlDataModel {
75
521
        self.network_uldata.as_ref()
76
521
    }
77
}