#[cfg(feature = "database")]
use async_trait::async_trait;
#[cfg(feature = "database")]
use std::sync::Arc;
#[cfg(feature = "database")]
use tideway::{DatabaseConnection, DatabasePool, Result};
#[cfg(not(feature = "database"))]
fn main() {
println!("This example requires the 'database' feature");
println!("Run with: cargo run --example custom_database --features database");
}
#[cfg(feature = "database")]
struct MockConnection {
id: u64,
}
impl DatabaseConnection for MockConnection {
fn is_valid(&self) -> bool {
self.id > 0
}
}
struct CustomDatabasePool {
url: String,
}
impl CustomDatabasePool {
fn new(url: String) -> Self {
Self { url }
}
}
#[async_trait]
impl DatabasePool for CustomDatabasePool {
async fn connection(&self) -> Result<Box<dyn DatabaseConnection>> {
Ok(Box::new(MockConnection { id: 1 }))
}
fn is_healthy(&self) -> bool {
true
}
async fn close(self: Box<Self>) -> Result<()> {
Ok(())
}
fn connection_url(&self) -> Option<&str> {
Some(&self.url)
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
#[tokio::main]
async fn main() -> Result<()> {
tideway::init_tracing();
let pool: Arc<dyn DatabasePool> =
Arc::new(CustomDatabasePool::new("custom://database/url".to_string()));
let _context = tideway::AppContext::builder()
.with_database(pool.clone())
.build();
let conn = pool.connection().await?;
println!("Got connection, valid: {}", conn.is_valid());
println!("Custom database pool example completed!");
Ok(())
}