Implement Into<sea_orm::ConnectOptions> for &DatabaseConfig

This commit is contained in:
fluo10 2025-05-03 15:31:13 +09:00
parent aa7f31b396
commit 7bebdf7490
6 changed files with 45 additions and 21 deletions

1
Cargo.lock generated
View file

@ -821,6 +821,7 @@ dependencies = [
"dpts-entity",
"dpts-error",
"iana-time-zone",
"sea-orm",
"serde",
"thiserror 2.0.12",
"tokio",

View file

@ -33,6 +33,17 @@ thiserror = "2.0.12"
tokio = "1.44.2"
toml = "0.8.22"
[workspace.dependencies.sea-orm]
version = "1.1"
features = [
"macros",
"debug-print",
"runtime-tokio-native-tls",
"sqlx-sqlite",
"with-chrono",
]
default-features = false
[workspace.package]
version = "0.1.0"
edition = "2024"

View file

@ -16,6 +16,7 @@ chrono-tz.workspace = true
dpts-entity.workspace = true
dpts-error.workspace = true
iana-time-zone = "0.1.63"
sea-orm.workspace = true
serde.workspace = true
thiserror.workspace = true
tokio.workspace = true

View file

@ -1,5 +1,6 @@
use std::time::Duration;
use sea_orm::ConnectOptions;
use serde::Deserialize;
use crate::Error;
@ -16,6 +17,33 @@ pub struct DatabaseConfig {
pub sqlx_logging: bool,
}
impl Into<ConnectOptions> for &DatabaseConfig {
fn into(self) -> ConnectOptions {
let mut opt = ConnectOptions::new(&self.url);
if let Some(x) = self.max_connections {
opt.max_connections(x);
}
if let Some(x) = self.min_connections {
opt.min_connections(x);
}
if let Some(x) = self.connect_timeout {
opt.connect_timeout(x);
}
if let Some(x) = self.acquire_timeout {
opt.acquire_timeout(x);
}
if let Some(x) = self.idle_timeout {
opt.idle_timeout(x);
}
if let Some(x) = self.max_lifetime {
opt.max_lifetime(x);
}
opt.sqlx_logging(self.sqlx_logging);
opt
}
}
impl TryFrom<PartialDatabaseConfig> for DatabaseConfig{
type Error = Error;
fn try_from(p: PartialDatabaseConfig) -> Result<DatabaseConfig, Self::Error> {
@ -31,6 +59,7 @@ impl TryFrom<PartialDatabaseConfig> for DatabaseConfig{
})
}
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
pub struct PartialDatabaseConfig {
pub url: Option<String>,

View file

@ -20,18 +20,10 @@ axum = "0.8"
chrono = {workspace = true}
dotenv = {workspace = true}
log = "0.4.27"
sea-orm.workspace = true
tokio.workspace = true
[dependencies.sea-orm]
version = "1.1"
features = [
"macros",
"debug-print",
"runtime-tokio-native-tls",
"sqlx-sqlite",
"with-chrono",
]
default-features = false
[dev-dependencies]
dpts-config = { workspace = true, features = ["test"] }

View file

@ -27,17 +27,7 @@ impl OnceDatabaseConnection {
pub async fn get_or_init_with_server_config(&self, c: &ServerConfig) -> &DatabaseConnection {
self.get_or_init( || async {
let mut opt = ConnectOptions::new(&c.database_url);
opt.max_connections(100)
.min_connections(5)
.connect_timeout(Duration::from_secs(8))
.acquire_timeout(Duration::from_secs(8))
.idle_timeout(Duration::from_secs(8))
.max_lifetime(Duration::from_secs(8))
.sqlx_logging(true)
.sqlx_logging_level(log::LevelFilter::Info);
//.set_schema_search_path("my_schema"); // Setting default PostgreSQL schema
let db = Database::connect(opt).await.unwrap();
let db = Database::connect(&c.database).await.unwrap();
Migrator::fresh(&db).await.unwrap();
db
}).await