secra-database 0.1.0

基于 SeaORM 的 Rust 数据库连接和管理库
Documentation
//! 自定义连接选项示例
//!
//! 演示如何配置自定义的连接池选项

use secra_database::{DatabaseConfig, DatabaseService, ConnectionOptions};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = DatabaseConfig {
        database_type: "postgres".to_string(),
        host: "localhost".to_string(),
        port: 5432,
        username: "postgres".to_string(),
        password: "password".to_string(),
        database_name: "mydb".to_string(),
        schema: "public".to_string(),
        logging_level: "info".to_string(),
        use_pgbouncer: false,
    };

    // 自定义连接选项
    let options = ConnectionOptions {
        max_connections: 100,      // 最大连接数
        min_connections: 10,       // 最小连接数
        connect_timeout: 10,      // 连接超时(秒)
        acquire_timeout: 10,       // 获取连接超时(秒)
        idle_timeout: 600,         // 空闲连接超时(秒)
        max_lifetime: 1800,        // 连接最大生命周期(秒)
        sqlx_logging: true,        // 启用 SQL 日志
    };

    println!("使用自定义连接选项创建连接...");
    let db = DatabaseService::init(&config, Some(options)).await?;
    println!("✓ 连接创建成功");

    // 测试连接
    DatabaseService::test_connection(&db).await?;
    println!("✓ 连接测试通过");

    Ok(())
}