Database Connection
Database connection initialization and management with support for multiple database backends.
Overview
The Database Connection package provides:
- Multi-backend Support: PostgreSQL and SQLite database connections
- Feature-gated Backends: Choose specific database implementations
- TLS Support: Native TLS and OpenSSL options for PostgreSQL
- Connection Management: Unified connection initialization interface
- Credential Handling: Secure credential management
- Simulator Mode: Mock database connections for testing
Features
Database Backends
- PostgreSQL: Raw tokio-postgres and SQLx implementations
- SQLite: Rusqlite and SQLx implementations
- Simulator: Mock database for testing and development
PostgreSQL Options
- Raw Implementation: Direct tokio-postgres with connection pooling
- SQLx Implementation: SQLx-based PostgreSQL connections
- TLS Support: Native TLS, OpenSSL, or no TLS options
- Connection Pooling: Managed connection pools
SQLite Options
- Rusqlite: Synchronous SQLite with async wrapper
- SQLx: Async SQLite via SQLx
- In-memory: Support for in-memory databases
- File-based: Persistent SQLite database files
Installation
Add this to your Cargo.toml
:
[]
= { = "../database_connection" }
# PostgreSQL with native TLS
= {
path = "../database_connection",
= ["postgres-raw", "postgres-native-tls"]
}
# SQLite with rusqlite
= {
path = "../database_connection",
= ["sqlite", "sqlite-rusqlite"]
}
# Multiple backends
= {
path = "../database_connection",
= [
"postgres-sqlx",
"sqlite-sqlx",
"sqlite"
]
}
Usage
Basic Database Initialization
use ;
use Path;
async
SQLite Database
use init;
use Path;
async
Credential Management
use Credentials;
// Create credentials
let creds = new;
// Use with database initialization
let db = init.await?;
PostgreSQL with TLS
// Feature: postgres-raw + postgres-native-tls
use ;
let creds = new;
let db = init_postgres_raw_native_tls.await?;
PostgreSQL with OpenSSL
// Feature: postgres-raw + postgres-openssl
use ;
let creds = new;
let db = init_postgres_raw_openssl.await?;
PostgreSQL without TLS
// Feature: postgres-raw
use ;
let creds = new;
let db = init_postgres_raw_no_tls.await?;
PostgreSQL with SQLx
// Feature: postgres-sqlx
use ;
let creds = new;
let db = init_postgres_sqlx.await?;
SQLite with Rusqlite
// Feature: sqlite-rusqlite
use init_sqlite_rusqlite;
use Path;
// File-based database
let db_path = new;
let db = init_sqlite_rusqlite?;
// In-memory database
let db = init_sqlite_rusqlite?;
SQLite with SQLx
// Feature: sqlite-sqlx
use init_sqlite_sqlx;
use Path;
// File-based database
let db_path = new;
let db = init_sqlite_sqlx.await?;
// In-memory database
let db = init_sqlite_sqlx.await?;
Non-SQLite Initialization
use ;
// Initialize any non-SQLite database based on features
let creds = Some;
let db = init_default_non_sqlite.await?;
Simulator Mode
// Feature: simulator
use init;
// Always returns a mock database for testing
let db = init.await?;
// Use mock database in tests
async
Error Handling
use ;
match init.await
Feature Flags
PostgreSQL Features
postgres-raw
: Raw tokio-postgres implementationpostgres-sqlx
: SQLx PostgreSQL implementationpostgres-native-tls
: Native TLS support for PostgreSQLpostgres-openssl
: OpenSSL support for PostgreSQL
SQLite Features
sqlite
: Enable SQLite supportsqlite-rusqlite
: Rusqlite implementationsqlite-sqlx
: SQLx SQLite implementation
Other Features
simulator
: Mock database for testingcreds
: Credential management utilities
Connection Strings
PostgreSQL Connection Format
host=localhost dbname=mydb user=username password=password
SQLite Connection Format
./path/to/database.db (file-based)
:memory: (in-memory)
Dependencies
- Switchy Database: Generic database trait abstraction
- Tokio Postgres: PostgreSQL async driver (optional)
- SQLx: Multi-database async driver (optional)
- Rusqlite: SQLite synchronous driver (optional)
- Native TLS: TLS implementation (optional)
- OpenSSL: Alternative TLS implementation (optional)
- Thiserror: Error handling
Use Cases
- Web Applications: Database connections for web servers
- Microservices: Service-specific database connections
- CLI Tools: Command-line database utilities
- Testing: Mock database connections for unit tests
- Data Migration: Database migration and setup tools
- Multi-tenant Applications: Dynamic database connections