init
This commit is contained in:
57
src/db/context.rs
Normal file
57
src/db/context.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
use std::sync::Arc;
|
||||
use sqlx::SqlitePool;
|
||||
use crate::db::{AttachmentRepository, CategoryRepository, ChannelRepository, ChannelUser, ChannelUserRepository, MessageRepository, ServerRepository, ServerUser, ServerUserRepository, UserRepository};
|
||||
use crate::utils::logger::ContextLogger;
|
||||
|
||||
struct Repositories {
|
||||
server_repository: ServerRepository,
|
||||
category_repository: CategoryRepository,
|
||||
channel_repository: ChannelRepository,
|
||||
user_repository: UserRepository,
|
||||
message_repository: MessageRepository,
|
||||
attachment_repository: AttachmentRepository,
|
||||
|
||||
server_user: ServerUserRepository,
|
||||
channel_user: ChannelUserRepository
|
||||
}
|
||||
|
||||
pub struct DbContext {
|
||||
pool: Arc<SqlitePool>,
|
||||
|
||||
repositories: Arc<Repositories>,
|
||||
|
||||
// logger
|
||||
logger: ContextLogger,
|
||||
}
|
||||
|
||||
impl DbContext {
|
||||
pub async fn new(database_url: &str) -> Result<Self, sqlx::Error> {
|
||||
let logger = ContextLogger::new("DB");
|
||||
|
||||
logger.info(&format!("Creating DB context on {}", database_url));
|
||||
let pool = SqlitePool::connect(database_url).await?;
|
||||
logger.info("DB context created");
|
||||
let pool = Arc::new(pool);
|
||||
|
||||
let repositories = Arc::new(Repositories {
|
||||
server_repository: ServerRepository::new(pool.clone()),
|
||||
category_repository: CategoryRepository::new(pool.clone()),
|
||||
channel_repository: ChannelRepository::new(pool.clone()),
|
||||
user_repository: UserRepository::new(pool.clone()),
|
||||
message_repository: MessageRepository::new(pool.clone()),
|
||||
attachment_repository: AttachmentRepository::new(pool.clone()),
|
||||
server_user: ServerUserRepository::new(pool.clone()),
|
||||
channel_user: ChannelUserRepository::new(pool.clone())
|
||||
});
|
||||
|
||||
Ok(Self {
|
||||
pool,
|
||||
repositories,
|
||||
logger,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn repositories(&self) -> Arc<Repositories> {
|
||||
self.repositories.clone()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user