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()); - } -}