feat: Implement database factory and abstract session store for multi-database support, centralizing service creation in main.rs.
This commit is contained in:
46
notes-infra/src/session_store.rs
Normal file
46
notes-infra/src/session_store.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
use async_trait::async_trait;
|
||||
use sqlx;
|
||||
use tower_sessions::{
|
||||
SessionStore,
|
||||
session::{Id, Record},
|
||||
};
|
||||
use tower_sessions_sqlx_store::{PostgresStore, SqliteStore};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum InfraSessionStore {
|
||||
Sqlite(SqliteStore),
|
||||
Postgres(PostgresStore),
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl SessionStore for InfraSessionStore {
|
||||
async fn save(&self, session_record: &Record) -> tower_sessions::session_store::Result<()> {
|
||||
match self {
|
||||
Self::Sqlite(store) => store.save(session_record).await,
|
||||
Self::Postgres(store) => store.save(session_record).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn load(&self, session_id: &Id) -> tower_sessions::session_store::Result<Option<Record>> {
|
||||
match self {
|
||||
Self::Sqlite(store) => store.load(session_id).await,
|
||||
Self::Postgres(store) => store.load(session_id).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn delete(&self, session_id: &Id) -> tower_sessions::session_store::Result<()> {
|
||||
match self {
|
||||
Self::Sqlite(store) => store.delete(session_id).await,
|
||||
Self::Postgres(store) => store.delete(session_id).await,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl InfraSessionStore {
|
||||
pub async fn migrate(&self) -> Result<(), sqlx::Error> {
|
||||
match self {
|
||||
Self::Sqlite(store) => store.migrate().await,
|
||||
Self::Postgres(store) => store.migrate().await,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user