From 9374e4ef93685bb848484ae9908ae42f19ec7c0e Mon Sep 17 00:00:00 2001 From: naskya Date: Sat, 27 Jan 2024 07:51:35 +0900 Subject: [PATCH] refactor (backend-rs): move connect_to_database to a separate file --- packages/backend-rs/src/database/connect.rs | 40 +++++++++++++++++++ packages/backend-rs/src/database/mod.rs | 43 ++------------------- 2 files changed, 43 insertions(+), 40 deletions(-) create mode 100644 packages/backend-rs/src/database/connect.rs diff --git a/packages/backend-rs/src/database/connect.rs b/packages/backend-rs/src/database/connect.rs new file mode 100644 index 000000000..004b76b18 --- /dev/null +++ b/packages/backend-rs/src/database/connect.rs @@ -0,0 +1,40 @@ +use crate::config::server::SERVER_CONFIG; +use crate::database::NapiDbErrExt; +use sea_orm::{Database, DbConn}; +use urlencoding::encode; + +#[napi_derive::napi] +pub struct JsDbConn { + inner: DbConn, +} +impl JsDbConn { + pub fn inner(&self) -> &DbConn { + &self.inner + } +} + +#[napi_derive::napi] +pub async fn connect_to_database() -> napi::Result { + let conn_uri = format!( + "postgres://{}:{}@{}:{}/{}", + SERVER_CONFIG.db.user, + encode(&SERVER_CONFIG.db.pass), + SERVER_CONFIG.db.host, + SERVER_CONFIG.db.port, + SERVER_CONFIG.db.db, + ); + let conn = Database::connect(conn_uri) + .await + .map_err(NapiDbErrExt::into)?; + Ok(JsDbConn { inner: conn }) +} + +#[cfg(test)] +mod unit_test { + use super::connect_to_database; + + #[tokio::test] + async fn connect_test() { + assert!(connect_to_database().await.is_ok()); + } +} diff --git a/packages/backend-rs/src/database/mod.rs b/packages/backend-rs/src/database/mod.rs index 155691ff5..e301e4f34 100644 --- a/packages/backend-rs/src/database/mod.rs +++ b/packages/backend-rs/src/database/mod.rs @@ -1,42 +1,5 @@ +pub mod connect; pub mod error; + +pub use connect::JsDbConn; pub use error::NapiDbErrExt; - -use crate::config::server::SERVER_CONFIG; -use sea_orm::{Database, DbConn}; -use urlencoding::encode; - -#[napi_derive::napi] -pub struct JsDbConn { - inner: DbConn, -} -impl JsDbConn { - pub fn inner(&self) -> &DbConn { - &self.inner - } -} - -#[napi_derive::napi] -pub async fn connect_to_database() -> napi::Result { - let conn_uri = format!( - "postgres://{}:{}@{}:{}/{}", - SERVER_CONFIG.db.user, - encode(&SERVER_CONFIG.db.pass), - SERVER_CONFIG.db.host, - SERVER_CONFIG.db.port, - SERVER_CONFIG.db.db, - ); - let conn = Database::connect(conn_uri) - .await - .map_err(NapiDbErrExt::into)?; - Ok(JsDbConn { inner: conn }) -} - -#[cfg(test)] -mod unit_test { - use super::connect_to_database; - - #[tokio::test] - async fn connect_test() { - assert!(connect_to_database().await.is_ok()); - } -}