Bump version to 0.1.2 and refactor database connection handling
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.1"
|
version = "0.1.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"chrono",
|
"chrono",
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "k-core"
|
name = "k-core"
|
||||||
version = "0.1.1"
|
version = "0.1.2"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["logging", "db-sqlx", "auth"]
|
default = ["logging", "db-sqlx", "auth"]
|
||||||
logging = ["dep:tracing", "dep:tracing-subscriber"]
|
logging = ["dep:tracing", "dep:tracing-subscriber"]
|
||||||
db-sqlx = ["dep:sqlx"]
|
db-sqlx = ["dep:sqlx"]
|
||||||
postgres = ["sqlx/postgres"]
|
postgres = ["db-sqlx", "sqlx/postgres"]
|
||||||
|
sqlite = ["db-sqlx", "sqlx/sqlite"]
|
||||||
auth = ["dep:tower-sessions"]
|
auth = ["dep:tower-sessions"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
@@ -22,8 +23,6 @@ tokio = { version = "1.48.0", features = ["full"], optional = true }
|
|||||||
sqlx = { version = "0.8.6", features = [
|
sqlx = { version = "0.8.6", features = [
|
||||||
"runtime-tokio",
|
"runtime-tokio",
|
||||||
"macros",
|
"macros",
|
||||||
"sqlite",
|
|
||||||
"postgres",
|
|
||||||
"chrono",
|
"chrono",
|
||||||
"uuid",
|
"uuid",
|
||||||
], optional = true }
|
], optional = true }
|
||||||
|
|||||||
20
src/db.rs
20
src/db.rs
@@ -1,5 +1,8 @@
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use sqlx::{Pool, Sqlite};
|
use sqlx::{Pool};
|
||||||
|
|
||||||
|
#[cfg(feature = "sqlite")]
|
||||||
|
use sqlx::Sqlite;
|
||||||
|
|
||||||
#[cfg(feature = "postgres")]
|
#[cfg(feature = "postgres")]
|
||||||
use sqlx::Postgres;
|
use sqlx::Postgres;
|
||||||
@@ -26,6 +29,7 @@ impl Default for DatabaseConfig {
|
|||||||
/// The Template uses this type so it doesn't care if it's Sqlite or Postgres.
|
/// The Template uses this type so it doesn't care if it's Sqlite or Postgres.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum DatabasePool {
|
pub enum DatabasePool {
|
||||||
|
#[cfg(feature = "sqlite")]
|
||||||
Sqlite(Pool<Sqlite>),
|
Sqlite(Pool<Sqlite>),
|
||||||
#[cfg(feature = "postgres")]
|
#[cfg(feature = "postgres")]
|
||||||
Postgres(Pool<Postgres>),
|
Postgres(Pool<Postgres>),
|
||||||
@@ -44,17 +48,27 @@ pub async fn connect(config: &DatabaseConfig) -> Result<DatabasePool, sqlx::Erro
|
|||||||
return Ok(DatabasePool::Postgres(pool));
|
return Ok(DatabasePool::Postgres(pool));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Default to Sqlite
|
// 2. Fallback to Sqlite if the feature is enabled
|
||||||
|
#[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?;
|
||||||
|
|
||||||
Ok(DatabasePool::Sqlite(pool))
|
#[cfg(feature = "sqlite")]
|
||||||
|
Ok(DatabasePool::Sqlite(pool));
|
||||||
|
|
||||||
|
#[cfg(not(feature = "sqlite"))]
|
||||||
|
{
|
||||||
|
Err(sqlx::Error::Configuration(
|
||||||
|
"No supported database features enabled".into(),
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Re-export specific connectors if you still need manual control
|
// Re-export specific connectors if you still need manual control
|
||||||
|
#[cfg(feature = "postgres")]
|
||||||
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