From c783323b69e4874a1f55c4cd77df9a570d06386e Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Fri, 2 Jan 2026 13:30:28 +0100 Subject: [PATCH] Bump version to 0.1.3 and enhance DatabaseConfig with min_connections and new methods --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/db.rs | 63 +++++++++++++++++++++++++++++++++++++++++++----------- 3 files changed, 53 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 82305e3..fc151be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -747,7 +747,7 @@ dependencies = [ [[package]] name = "k-core" -version = "0.1.2" +version = "0.1.3" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index cbd935a..f868d37 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "k-core" -version = "0.1.2" +version = "0.1.3" edition = "2024" [features] diff --git a/src/db.rs b/src/db.rs index 2ddc7a3..ee7d81d 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,5 +1,5 @@ +use sqlx::Pool; use std::time::Duration; -use sqlx::{Pool}; #[cfg(feature = "sqlite")] use sqlx::Sqlite; @@ -12,15 +12,53 @@ use sqlx::Postgres; pub struct DatabaseConfig { pub url: String, pub max_connections: u32, + pub min_connections: u32, pub acquire_timeout: Duration, } impl Default for DatabaseConfig { fn default() -> Self { + #[cfg(feature = "sqlite")] Self { url: "sqlite::memory:".to_string(), max_connections: 5, + min_connections: 1, acquire_timeout: Duration::from_secs(30), + }; + + #[cfg(all(not(feature = "sqlite"), feature = "postgres"))] + Self { + url: "postgres://localhost:5432/mydb".to_string(), + max_connections: 5, + min_connections: 1, + acquire_timeout: Duration::from_secs(30), + }; + + #[cfg(not(any(feature = "sqlite", feature = "postgres")))] + Self { + url: "".to_string(), + max_connections: 5, + min_connections: 1, + acquire_timeout: Duration::from_secs(30), + } + } +} + +impl DatabaseConfig { + pub fn new(url: impl Into) -> Self { + Self { + url: url.into(), + ..Default::default() + } + } + + #[cfg(feature = "sqlite")] + pub fn in_memory() -> Self { + Self { + url: "sqlite::memory:".to_string(), + max_connections: 1, // SQLite in-memory is single-connection + min_connections: 1, + ..Default::default() } } } @@ -50,14 +88,15 @@ pub async fn connect(config: &DatabaseConfig) -> Result Result Result, sqlx::Error> { - sqlx::sqlite::SqlitePoolOptions::new().connect(url).await -} \ No newline at end of file + sqlx::sqlite::SqlitePoolOptions::new().connect(url).await +}