sylvia_iot_auth/models/
mod.rsuse std::{error::Error as StdError, sync::Arc};
use async_trait::async_trait;
pub mod access_token;
pub mod authorization_code;
pub mod client;
pub mod login_session;
pub mod redis;
pub mod refresh_token;
pub mod user;
mod model_mongodb;
mod model_sqlite;
mod mongodb;
mod sqlite;
pub use self::{
mongodb::conn::{self as mongodb_conn, Options as MongoDbOptions},
redis::conn::{self as redis_conn, Options as RedisOptions},
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 user(&self) -> &dyn user::UserModel;
fn client(&self) -> &dyn client::ClientModel;
fn login_session(&self) -> &dyn login_session::LoginSessionModel;
fn authorization_code(&self) -> &dyn authorization_code::AuthorizationCodeModel;
fn access_token(&self) -> &dyn access_token::AccessTokenModel;
fn refresh_token(&self) -> &dyn refresh_token::RefreshTokenModel;
}
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.user().init().await?;
model.client().init().await?;
model.login_session().init().await?;
model.authorization_code().init().await?;
model.access_token().init().await?;
model.refresh_token().init().await?;
Ok(model)
}