Restructure database crate

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/4/head
trivernis 3 years ago
parent 72c5796311
commit 9e5d6c9bdd

@ -1,20 +0,0 @@
use sea_orm::{DatabaseConnection, Database};
use mediarepo_core::error::{RepoDatabaseResult};
pub struct RepoDatabase {
connection: DatabaseConnection,
}
impl RepoDatabase {
/// Creates a new repo database from an existing connection
pub(crate) fn new(connection: DatabaseConnection) -> Self {
Self {connection}
}
/// Creates a new Repo Database Connection
pub(crate) async fn connect<S: AsRef<str>>(uri: S) -> RepoDatabaseResult<Self> {
let connection = Database::connect(uri.as_ref()).await?;
Ok(Self::new(connection))
}
}

@ -1,13 +1,16 @@
use mediarepo_core::error::{RepoDatabaseResult};
use crate::database::RepoDatabase;
use sea_orm::{DatabaseConnection, Database};
pub mod database;
pub mod entities;
/// Connects to the database, runs migrations and returns the RepoDatabase wrapper type
pub async fn get_database<S: AsRef<str>>(uri: S) -> RepoDatabaseResult<RepoDatabase> {
pub async fn get_database<S: AsRef<str>>(uri: S) -> RepoDatabaseResult<DatabaseConnection> {
migrate(uri.as_ref()).await?;
RepoDatabase::connect(uri).await
let conn = Database::connect(uri).await?;
Ok(conn)
}
async fn migrate(uri: &str) -> RepoDatabaseResult<()> {

Loading…
Cancel
Save