| Crates.io | switchy_database_connection |
| lib.rs | switchy_database_connection |
| version | 0.1.4 |
| created_at | 2025-05-07 21:08:44.026315+00 |
| updated_at | 2025-07-21 19:27:59.38276+00 |
| description | Switchy database connection package |
| homepage | |
| repository | https://github.com/MoosicBox/MoosicBox |
| max_upload_size | |
| id | 1664524 |
| size | 132,676 |
Database connection initialization and management with support for multiple database backends.
The Database Connection package provides:
Add this to your Cargo.toml:
[dependencies]
database_connection = { path = "../database_connection" }
# PostgreSQL with native TLS
database_connection = {
path = "../database_connection",
features = ["postgres-raw", "postgres-native-tls"]
}
# SQLite with rusqlite
database_connection = {
path = "../database_connection",
features = ["sqlite", "sqlite-rusqlite"]
}
# Multiple backends
database_connection = {
path = "../database_connection",
features = [
"postgres-sqlx",
"sqlite-sqlx",
"sqlite"
]
}
use database_connection::{init, Credentials};
use std::path::Path;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// PostgreSQL connection
let pg_creds = Some(Credentials::new(
"localhost".to_string(),
"mydb".to_string(),
"user".to_string(),
Some("password".to_string()),
));
let db = init(None, pg_creds).await?;
// Use database...
Ok(())
}
use database_connection::init;
use std::path::Path;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// File-based SQLite
let db_path = Path::new("./database.db");
let db = init(Some(db_path), None).await?;
// In-memory SQLite
let db = init(None, None).await?;
// Use database...
Ok(())
}
use database_connection::Credentials;
// Create credentials
let creds = Credentials::new(
"database.example.com".to_string(), // host
"production_db".to_string(), // database name
"app_user".to_string(), // username
Some("secure_password".to_string()), // password (optional)
);
// Use with database initialization
let db = database_connection::init(None, Some(creds)).await?;
// Feature: postgres-raw + postgres-native-tls
use database_connection::{init_postgres_raw_native_tls, Credentials};
let creds = Credentials::new(
"secure-db.example.com".to_string(),
"mydb".to_string(),
"user".to_string(),
Some("password".to_string()),
);
let db = init_postgres_raw_native_tls(creds).await?;
// Feature: postgres-raw + postgres-openssl
use database_connection::{init_postgres_raw_openssl, Credentials};
let creds = Credentials::new(
"ssl-db.example.com".to_string(),
"mydb".to_string(),
"user".to_string(),
Some("password".to_string()),
);
let db = init_postgres_raw_openssl(creds).await?;
// Feature: postgres-raw
use database_connection::{init_postgres_raw_no_tls, Credentials};
let creds = Credentials::new(
"local-db".to_string(),
"mydb".to_string(),
"user".to_string(),
Some("password".to_string()),
);
let db = init_postgres_raw_no_tls(creds).await?;
// Feature: postgres-sqlx
use database_connection::{init_postgres_sqlx, Credentials};
let creds = Credentials::new(
"localhost".to_string(),
"mydb".to_string(),
"user".to_string(),
Some("password".to_string()),
);
let db = init_postgres_sqlx(creds).await?;
// Feature: sqlite-rusqlite
use database_connection::init_sqlite_rusqlite;
use std::path::Path;
// File-based database
let db_path = Path::new("./app.db");
let db = init_sqlite_rusqlite(Some(db_path))?;
// In-memory database
let db = init_sqlite_rusqlite(None)?;
// Feature: sqlite-sqlx
use database_connection::init_sqlite_sqlx;
use std::path::Path;
// File-based database
let db_path = Path::new("./app.db");
let db = init_sqlite_sqlx(Some(db_path)).await?;
// In-memory database
let db = init_sqlite_sqlx(None).await?;
use database_connection::{init_default_non_sqlite, Credentials};
// Initialize any non-SQLite database based on features
let creds = Some(Credentials::new(
"localhost".to_string(),
"mydb".to_string(),
"user".to_string(),
Some("password".to_string()),
));
let db = init_default_non_sqlite(creds).await?;
// Feature: simulator
use database_connection::init;
// Always returns a mock database for testing
let db = init(None, None).await?;
// Use mock database in tests
#[tokio::test]
async fn test_database_operations() {
let db = init(None, None).await.unwrap();
// Test database operations without real database
}
use database_connection::{init, InitDbError, Credentials};
match init(None, None).await {
Ok(db) => {
// Use database
}
Err(InitDbError::CredentialsRequired) => {
eprintln!("Database credentials are required");
}
Err(InitDbError::InitSqlite(e)) => {
eprintln!("SQLite initialization error: {}", e);
}
Err(InitDbError::InitPostgres(e)) => {
eprintln!("PostgreSQL initialization error: {}", e);
}
Err(InitDbError::Database(e)) => {
eprintln!("Database error: {}", e);
}
}
postgres-raw: Raw tokio-postgres implementationpostgres-sqlx: SQLx PostgreSQL implementationpostgres-native-tls: Native TLS support for PostgreSQLpostgres-openssl: OpenSSL support for PostgreSQLsqlite: Enable SQLite supportsqlite-rusqlite: Rusqlite implementationsqlite-sqlx: SQLx SQLite implementationsimulator: Mock database for testingcreds: Credential management utilitieshost=localhost dbname=mydb user=username password=password
./path/to/database.db (file-based)
:memory: (in-memory)