feat(project): init
This commit is contained in:
18
src/db/mod.rs
Normal file
18
src/db/mod.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
use sqlx::postgres::{PgPool, PgPoolOptions};
|
||||
use std::time::Duration;
|
||||
|
||||
/// 初始化数据库连接池
|
||||
pub async fn init_pool(database_url: &str) -> Result<PgPool, sqlx::Error> {
|
||||
PgPoolOptions::new()
|
||||
.max_connections(20) // 根据服务器规格调整,IAM服务通常并发高
|
||||
.min_connections(5)
|
||||
.acquire_timeout(Duration::from_secs(3)) // 获取连接超时时间
|
||||
.connect(database_url)
|
||||
.await
|
||||
}
|
||||
|
||||
// (可选) 可以在应用启动时自动运行迁移
|
||||
// pub async fn run_migrations(pool: &PgPool) -> Result<(), sqlx::migrate::MigrateError> {
|
||||
// // 这要求你在项目根目录有 `migrations/` 文件夹
|
||||
// sqlx::migrate!("./migrations").run(pool).await
|
||||
// }
|
||||
Reference in New Issue
Block a user