| Crates.io | tin-sea-conn |
| lib.rs | tin-sea-conn |
| version | 0.1.1 |
| created_at | 2025-10-13 00:29:09.428785+00 |
| updated_at | 2025-12-25 02:19:28.729275+00 |
| description | A simple and flexible database connection library for Rust based on SeaORM, supporting PostgreSQL, MySQL, and SQLite with configurable connection pooling |
| homepage | https://github.com/JiabinTang/tin-sea-conn |
| repository | https://github.com/JiabinTang/tin-sea-conn |
| max_upload_size | |
| id | 1879868 |
| size | 66,771 |
A simple and flexible database connection library for Rust based on SeaORM, supporting PostgreSQL, MySQL, and SQLite with configurable connection pooling.
Add this to your Cargo.toml:
[dependencies]
tin-sea-conn = { version = "0.1.0", features = ["postgres"] }
use tin_sea_conn::DbConnector;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let connector = DbConnector::new()
.postgres()
.host("localhost")
.port(5432)
.username("user")
.password("password")
.database("mydb")
.max_connections(20)
.min_connections(5);
let db = connector.connect().await?;
// Use the database connection...
Ok(())
}
use tin_sea_conn::DbConnector;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let connector = DbConnector::new()
.mysql()
.host("localhost")
.port(3306)
.username("user")
.password("password")
.database("mydb");
let db = connector.connect().await?;
// Use the database connection...
Ok(())
}
use tin_sea_conn::DbConnector;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let connector = DbConnector::new()
.sqlite()
.database("./mydb.sqlite");
let db = connector.connect().await?;
// Use the database connection...
Ok(())
}
The DbConnector supports the following configuration options:
| Method | Description | Default |
|---|---|---|
host(host) |
Database host | Required for PostgreSQL/MySQL |
port(port) |
Database port | 5432 (PostgreSQL), 3306 (MySQL) |
username(user) |
Database username | Required for PostgreSQL/MySQL |
password(pass) |
Database password | Required for PostgreSQL/MySQL |
database(db) |
Database name or file path | Required |
max_connections(max) |
Maximum connections in pool | 10 |
min_connections(min) |
Minimum connections in pool | 1 |
connect_timeout(seconds) |
Connection timeout in seconds | 30 |
idle_timeout(seconds) |
Idle connection timeout in seconds | 60 |
test_before_acquire(bool) |
Test connections before use | true |
sqlx_logging(bool) |
Enable SQLx logging | Auto-detected from log level |
This crate uses Cargo features to enable database drivers:
postgres - Enable PostgreSQL supportmysql - Enable MySQL supportsqlite - Enable SQLite supportYou can enable multiple features to support multiple database types in the same application.
To build with a specific database feature:
# PostgreSQL
cargo build --features postgres
# MySQL
cargo build --features mysql
# SQLite
cargo build --features sqlite
# Multiple databases
cargo build --features "postgres,mysql,sqlite"
The library provides a comprehensive ConnectionError enum for error handling:
use tin_sea_conn::{DbConnector, ConnectionError};
match connector.connect().await {
Ok(db) => {
// Use database connection
}
Err(ConnectionError::InvalidConfig(msg)) => {
eprintln!("Configuration error: {}", msg);
}
Err(ConnectionError::ConnectionFailed(msg)) => {
eprintln!("Failed to connect: {}", msg);
}
Err(ConnectionError::DatabaseError(msg)) => {
eprintln!("Database error: {}", msg);
}
}
This library is built on top of:
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.