| Crates.io | seaorm-pool |
| lib.rs | seaorm-pool |
| version | 0.1.1 |
| created_at | 2025-08-09 13:11:55.938336+00 |
| updated_at | 2025-08-09 13:11:55.938336+00 |
| description | A Rust crate that creates a MySQL connection pool for SeaORM using a TOML configuration file. |
| homepage | |
| repository | https://github.com/RustLangLatam/tidb-pool |
| max_upload_size | |
| id | 1787917 |
| size | 103,283 |
seaorm-pool)A robust and easy-to-use utility for creating a sea-orm connection pool from a configuration file. Designed for async Rust applications connecting to MySQL-compatible databases like TiDB.
seaorm-pool eliminates the boilerplate of setting up a database connection. It provides a clean, configuration-driven function that creates a fully configured sea-orm connection pool, ready to use in your application.
It is built on top of industry-standard crates to ensure reliability and performance:
max_connections, min_connections, idle_timeout, max_lifetime, and more.tracing to provide clear insight into the connection lifecycle.Add seaorm-pool and its required peer dependencies to your Cargo.toml:
## ⚙️ Usage Quick Start
### 1. Create a Configuration File
Create a `Settings.toml` file in your project's root directory. **Note that the configuration must be under a `[database]` table** to match the crate's `AppConfig` struct.
```toml
# Settings.toml
# The `[database]` table is required.
[database]
host = "gateway01.eu-central-1.prod.aws.tidbcloud.com"
port = 4000
username = "your_username"
password = "your_password"
databaseName = "your_db"
# Optional: Path to your SSL CA certificate for secure connections.
# sslCa = "/path/to/your/ca.pem"
# Nested table for connection pool options.
[database.poolOptions]
maxConnections = 10
minConnections = 5
acquireTimeout = "30s"
idleTimeout = "10m"
maxLifetime = "30m"
isLazy = true
Use the create_connection_pool function to establish the database connection.
// src/main.rs
use sea_orm::{DatabaseConnection, Statement, DatabaseBackend};
use seaorm_pool::{config::AppConfig, create_connection_pool};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// 1. Load configuration using the `config` crate.
let settings = config::Config::builder()
.add_source(config::File::with_name("Settings"))
.build()?;
let app_config: AppConfig = settings.try_deserialize()?;
// 2. Create the database connection pool from the config.
println!("Establishing connection pool...");
let pool: DatabaseConnection = create_connection_pool(app_config.database).await?;
println!("Connection pool established successfully!");
// 3. Use the pool to execute a query.
let result = pool.execute(Statement::from_string(
DatabaseBackend::MySql,
"SELECT 'Connection successful!' as message;".to_string(),
)).await?;
println!("Query executed. Rows affected: {}", result.rows_affected());
Ok(())
}
The crate is configured through the AppConfig struct, which contains a database field of type DatabaseConfig.
[database])| Field | Type | Required | Description |
|---|---|---|---|
host |
String | Yes | Hostname or IP address of the TiDB/MySQL server. |
port |
u16 |
No | Server port. Defaults to 4000 if connecting to TiDB. |
username |
String | Yes | Username for database authentication. |
password |
String | Yes | Password for database authentication. |
databaseName |
String | Yes | The specific database (schema) to connect to. |
sslCa |
String | No | Path to the SSL Certificate Authority (CA) file for enabling TLS. |
[database.poolOptions])| Field | Type | Default | Description |
|---|---|---|---|
maxConnections |
u32 |
10 |
Maximum number of concurrent connections the pool can open. |
minConnections |
u32 |
1 |
Minimum number of idle connections to maintain in the pool. |
acquireTimeout |
String |
"30s" |
Time to wait for a connection before timing out (e.g., "5s", "1m"). |
idleTimeout |
String |
"5m" |
Time a connection can be idle before it is closed (e.g., "10m", "1h"). |
maxLifetime |
String |
"30m" |
Maximum lifetime of a single connection before it is recycled. |
isLazy |
bool |
true |
If true, connections are established only when first needed. |
statementCacheCapacity |
usize |
100 |
The number of prepared statements to cache per connection. |
This project is licensed under either of:
at your option.
Contributions are welcome! Please feel free to open an issue or submit a pull request for any improvements or bug fixes.