baichun-framework-db
baichun-framework-db 是 Baichun Framework 的数据库模块,提供了一个简单而强大的数据库访问层。基于 SQLx,支持 MySQL、PostgreSQL 和 SQLite。
特性
- 连接池管理
- 自动配置最佳连接数
- 连接健康检查
- 连接超时处理
- 多数据库支持
- MySQL
- PostgreSQL
- SQLite
- 异步操作
- 基于 tokio 的异步运行时
- 非阻塞数据库操作
- 事务支持
- 自动事务管理
- 嵌套事务支持
- 错误处理
- 详细的错误类型
- 错误追踪和日志记录
安装
将以下内容添加到你的 Cargo.toml 文件中:
[]
= "0.1"
使用示例
use ;
// 创建数据库配置
let config = new
.url
.max_connections
.min_connections;
// 创建连接池
let pool = new.await?;
// 执行查询
let rows = pool.execute.await?;
// 使用事务
let mut tx = pool.begin.await?;
tx.execute.await?;
tx.execute.await?;
tx.commit.await?;
配置示例
use DbConfig;
// MySQL 配置
let mysql_config = new
.url
.max_connections
.min_connections
.connect_timeout
.idle_timeout;
// PostgreSQL 配置
let pg_config = new
.url
.max_connections
.min_connections;
// SQLite 配置
let sqlite_config = new
.url
.max_connections; // SQLite 通常使用单连接
特性标记
mysql:启用 MySQL 支持(默认)postgres:启用 PostgreSQL 支持sqlite:启用 SQLite 支持all:启用所有数据库支持
许可证
本项目采用 MIT 许可证。详见 LICENSE 文件。