baichun-framework-db

Crates.iobaichun-framework-db
lib.rsbaichun-framework-db
version0.1.0
created_at2025-06-12 11:39:38.485487+00
updated_at2025-06-12 11:39:38.485487+00
descriptionDatabase module for Baichun-Rust framework
homepage
repositoryhttps://github.com/baichun-framework/baichun-framework
max_upload_size
id1709710
size89,236
(fangbc5)

documentation

https://docs.rs/baichun-framework-db

README

baichun-framework-db

Crates.io Documentation License

baichun-framework-db 是 Baichun Framework 的数据库模块,提供了一个简单而强大的数据库访问层。基于 SQLx,支持 MySQL、PostgreSQL 和 SQLite。

特性

  • 连接池管理
    • 自动配置最佳连接数
    • 连接健康检查
    • 连接超时处理
  • 多数据库支持
    • MySQL
    • PostgreSQL
    • SQLite
  • 异步操作
    • 基于 tokio 的异步运行时
    • 非阻塞数据库操作
  • 事务支持
    • 自动事务管理
    • 嵌套事务支持
  • 错误处理
    • 详细的错误类型
    • 错误追踪和日志记录

安装

将以下内容添加到你的 Cargo.toml 文件中:

[dependencies]
baichun-framework-db = "0.1"

使用示例

use baichun_framework_db::{DbPool, DbConfig};

// 创建数据库配置
let config = DbConfig::new()
    .url("mysql://user:pass@localhost/db_name")
    .max_connections(10)
    .min_connections(5);

// 创建连接池
let pool = DbPool::new(config).await?;

// 执行查询
let rows = pool.execute("SELECT * FROM users WHERE id = ?", &[&1]).await?;

// 使用事务
let mut tx = pool.begin().await?;
tx.execute("INSERT INTO users (name) VALUES (?)", &[&"Alice"]).await?;
tx.execute("UPDATE users SET status = ? WHERE name = ?", &[&"active", &"Alice"]).await?;
tx.commit().await?;

配置示例

use baichun_framework_db::DbConfig;

// MySQL 配置
let mysql_config = DbConfig::new()
    .url("mysql://user:pass@localhost/db_name")
    .max_connections(20)
    .min_connections(5)
    .connect_timeout(Duration::from_secs(5))
    .idle_timeout(Duration::from_secs(300));

// PostgreSQL 配置
let pg_config = DbConfig::new()
    .url("postgresql://user:pass@localhost/db_name")
    .max_connections(20)
    .min_connections(5);

// SQLite 配置
let sqlite_config = DbConfig::new()
    .url("sqlite:///path/to/db.sqlite")
    .max_connections(1);  // SQLite 通常使用单连接

特性标记

  • mysql:启用 MySQL 支持(默认)
  • postgres:启用 PostgreSQL 支持
  • sqlite:启用 SQLite 支持
  • all:启用所有数据库支持

许可证

本项目采用 MIT 许可证。详见 LICENSE 文件。

Commit count: 0

cargo fmt