sylvia_iot_data/models/
mod.rsuse std::{error::Error as StdError, sync::Arc};
use async_trait::async_trait;
pub mod application_dldata;
pub mod application_uldata;
pub mod coremgr_opdata;
pub mod network_dldata;
pub mod network_uldata;
mod model_mongodb;
mod model_sqlite;
mod mongodb;
mod sqlite;
pub use self::{
mongodb::conn::{self as mongodb_conn, Options as MongoDbOptions},
sqlite::conn::{self as sqlite_conn, Options as SqliteOptions},
};
pub use model_mongodb::Model as MongoDbModel;
pub use model_sqlite::Model as SqliteModel;
pub enum ConnOptions {
MongoDB(MongoDbOptions),
Sqlite(SqliteOptions),
}
#[async_trait]
pub trait Model: Send + Sync {
async fn close(&self) -> Result<(), Box<dyn StdError>>;
fn application_dldata(&self) -> &dyn application_dldata::ApplicationDlDataModel;
fn application_uldata(&self) -> &dyn application_uldata::ApplicationUlDataModel;
fn coremgr_opdata(&self) -> &dyn coremgr_opdata::CoremgrOpDataModel;
fn network_dldata(&self) -> &dyn network_dldata::NetworkDlDataModel;
fn network_uldata(&self) -> &dyn network_uldata::NetworkUlDataModel;
}
pub async fn new(opts: &ConnOptions) -> Result<Arc<dyn Model>, Box<dyn StdError>> {
let model: Arc<dyn Model> = match opts {
ConnOptions::MongoDB(opts) => Arc::new(MongoDbModel::new(opts).await?),
ConnOptions::Sqlite(opts) => Arc::new(SqliteModel::new(opts).await?),
};
model.application_dldata().init().await?;
model.application_uldata().init().await?;
model.coremgr_opdata().init().await?;
model.network_dldata().init().await?;
model.network_uldata().init().await?;
Ok(model)
}