Bump version to 0.1.3 and enhance DatabaseConfig with min_connections and new methods
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -747,7 +747,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "k-core"
|
name = "k-core"
|
||||||
version = "0.1.2"
|
version = "0.1.3"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"chrono",
|
"chrono",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "k-core"
|
name = "k-core"
|
||||||
version = "0.1.2"
|
version = "0.1.3"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
|||||||
47
src/db.rs
47
src/db.rs
@@ -1,5 +1,5 @@
|
|||||||
|
use sqlx::Pool;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use sqlx::{Pool};
|
|
||||||
|
|
||||||
#[cfg(feature = "sqlite")]
|
#[cfg(feature = "sqlite")]
|
||||||
use sqlx::Sqlite;
|
use sqlx::Sqlite;
|
||||||
@@ -12,15 +12,53 @@ use sqlx::Postgres;
|
|||||||
pub struct DatabaseConfig {
|
pub struct DatabaseConfig {
|
||||||
pub url: String,
|
pub url: String,
|
||||||
pub max_connections: u32,
|
pub max_connections: u32,
|
||||||
|
pub min_connections: u32,
|
||||||
pub acquire_timeout: Duration,
|
pub acquire_timeout: Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for DatabaseConfig {
|
impl Default for DatabaseConfig {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
|
#[cfg(feature = "sqlite")]
|
||||||
Self {
|
Self {
|
||||||
url: "sqlite::memory:".to_string(),
|
url: "sqlite::memory:".to_string(),
|
||||||
max_connections: 5,
|
max_connections: 5,
|
||||||
|
min_connections: 1,
|
||||||
acquire_timeout: Duration::from_secs(30),
|
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<String>) -> 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<DatabasePool, sqlx::Erro
|
|||||||
|
|
||||||
// 2. Fallback to Sqlite if the feature is enabled
|
// 2. Fallback to Sqlite if the feature is enabled
|
||||||
#[cfg(feature = "sqlite")]
|
#[cfg(feature = "sqlite")]
|
||||||
|
{
|
||||||
let pool = sqlx::sqlite::SqlitePoolOptions::new()
|
let pool = sqlx::sqlite::SqlitePoolOptions::new()
|
||||||
.max_connections(config.max_connections)
|
.max_connections(config.max_connections)
|
||||||
.acquire_timeout(config.acquire_timeout)
|
.acquire_timeout(config.acquire_timeout)
|
||||||
.connect(&config.url)
|
.connect(&config.url)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
#[cfg(feature = "sqlite")]
|
Ok(DatabasePool::Sqlite(pool))
|
||||||
Ok(DatabasePool::Sqlite(pool));
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "sqlite"))]
|
#[cfg(not(feature = "sqlite"))]
|
||||||
{
|
{
|
||||||
@@ -68,7 +107,7 @@ pub async fn connect(config: &DatabaseConfig) -> Result<DatabasePool, sqlx::Erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Re-export specific connectors if you still need manual control
|
// Re-export specific connectors if you still need manual control
|
||||||
#[cfg(feature = "postgres")]
|
#[cfg(feature = "sqlite")]
|
||||||
pub async fn connect_sqlite(url: &str) -> Result<Pool<Sqlite>, sqlx::Error> {
|
pub async fn connect_sqlite(url: &str) -> Result<Pool<Sqlite>, sqlx::Error> {
|
||||||
sqlx::sqlite::SqlitePoolOptions::new().connect(url).await
|
sqlx::sqlite::SqlitePoolOptions::new().connect(url).await
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user