Skip to main content

wae_database/connection/
config.rs

1//! 数据库配置模块
2
3use serde::{Deserialize, Serialize};
4#[cfg(feature = "turso")]
5use std::path::PathBuf;
6use wae_types::{WaeError, WaeResult};
7
8/// 数据库操作结果类型
9pub type DatabaseResult<T> = WaeResult<T>;
10
11/// 数据库错误类型
12pub type DatabaseError = WaeError;
13
14/// 数据库配置枚举
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub enum DatabaseConfig {
17    /// Turso 配置
18    #[cfg(feature = "turso")]
19    Turso {
20        /// 数据库文件路径,为 None 时使用内存数据库
21        path: Option<PathBuf>,
22    },
23    /// PostgreSQL 配置
24    #[cfg(feature = "postgres")]
25    Postgres {
26        /// 连接字符串
27        connection_string: String,
28        /// 最大连接数
29        max_connections: Option<usize>,
30    },
31    /// MySQL 配置
32    #[cfg(feature = "mysql")]
33    MySql {
34        /// 连接字符串
35        connection_string: String,
36        /// 最大连接数
37        max_connections: Option<usize>,
38    },
39}
40
41impl DatabaseConfig {
42    /// 创建 Turso 内存数据库配置
43    #[cfg(feature = "turso")]
44    pub fn turso_in_memory() -> Self {
45        Self::Turso { path: None }
46    }
47
48    /// 创建 Turso 文件数据库配置
49    #[cfg(feature = "turso")]
50    pub fn turso_file<P: Into<PathBuf>>(path: P) -> Self {
51        Self::Turso { path: Some(path.into()) }
52    }
53
54    /// 创建 PostgreSQL 配置
55    #[cfg(feature = "postgres")]
56    pub fn postgres<S: Into<String>>(connection_string: S) -> Self {
57        Self::Postgres { connection_string: connection_string.into(), max_connections: None }
58    }
59
60    /// 创建 PostgreSQL 配置,带最大连接数
61    #[cfg(feature = "postgres")]
62    pub fn postgres_with_max_connections<S: Into<String>>(connection_string: S, max_connections: usize) -> Self {
63        Self::Postgres { connection_string: connection_string.into(), max_connections: Some(max_connections) }
64    }
65
66    /// 创建 MySQL 配置
67    #[cfg(feature = "mysql")]
68    pub fn mysql<S: Into<String>>(connection_string: S) -> Self {
69        Self::MySql { connection_string: connection_string.into(), max_connections: None }
70    }
71
72    /// 创建 MySQL 配置,带最大连接数
73    #[cfg(feature = "mysql")]
74    pub fn mysql_with_max_connections<S: Into<String>>(connection_string: S, max_connections: usize) -> Self {
75        Self::MySql { connection_string: connection_string.into(), max_connections: Some(max_connections) }
76    }
77}