docs.rs failed to build switchy_database_connection-0.2.0
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build:
switchy_database_connection-0.1.4
Database Connection
Database connection initialization and management with support for multiple database backends.
Overview
The Database Connection package provides:
- Multi-backend Support: PostgreSQL, MySQL, SQLite, DuckDB, and Turso 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
- MySQL: SQLx implementation
- SQLite: Rusqlite and SQLx implementations
- DuckDB: Embedded analytical database (file-backed and in-memory)
- Turso: Turso database support (libSQL-compatible)
- 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
MySQL Options
- SQLx Implementation: SQLx-based MySQL connections
- 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
Turso Options
- Local Files: Local Turso/libSQL database files
- In-memory: In-memory Turso databases
DuckDB Options
- File-based: Persistent DuckDB database files
- In-memory: In-memory DuckDB databases
- Read-only: Read-only file-backed connections
- Connection Pool: Pool of 5 connections behind
Arc<Mutex<>>
Installation
Add this to your Cargo.toml:
If you want real database connections through init, disable default features (the default feature set includes simulator).
[]
= { = "../database_connection", = false }
# PostgreSQL with native TLS
= {
path = "../database_connection",
= false,
= ["postgres-raw", "postgres-native-tls"]
}
# SQLite with rusqlite
= {
path = "../database_connection",
= false,
= ["sqlite", "sqlite-rusqlite"]
}
# Multiple backends
= {
path = "../database_connection",
= false,
= [
"postgres-sqlx",
"sqlite-sqlx",
"sqlite"
]
}
# Turso database
= {
path = "../database_connection",
= false,
= ["turso"]
}
# DuckDB with bundled library
= {
path = "../database_connection",
= false,
= ["duckdb-bundled"]
}
Usage
The examples below assume simulator is not enabled.
Basic Database Initialization
use ;
use Path;
async
SQLite Database
use init;
use Path;
async
Credential Management
use Credentials;
// Create credentials manually
let creds = new;
// Or parse from connection string
let creds = from_url?;
// Use with database initialization
let db = init.await?;
Environment Variables
The package supports multiple ways to provide credentials:
# Option 1: Connection string (recommended)
# Option 2: Individual environment variables
# Option 3: AWS SSM Parameters (requires 'creds' feature)
# Optional - defaults shown below
# default
# default
# default
# default
AWS Credentials (Optional)
To use AWS SSM parameter store for credentials, enable the creds feature:
[]
= {
path = "../database_connection",
= ["postgres", "creds"]
}
use get_db_creds;
// Automatically fetches from DATABASE_URL, env vars, or AWS SSM
let creds = get_db_creds.await?;
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?;
MySQL with SQLx
// Feature: mysql-sqlx
use ;
let creds = new;
let db = init_mysql_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?;
Turso Database
// Feature: turso
use init_turso_local;
use Path;
// File-based Turso database
let db_path = new;
let db = init_turso_local.await?;
// In-memory Turso database
let db = init_turso_local.await?;
DuckDB Database
// Feature: duckdb (or duckdb-bundled)
use init_duckdb;
use Path;
// File-based DuckDB database (pool of 5 connections)
let db_path = new;
let db = init_duckdb?;
// In-memory DuckDB database
let db = init_duckdb?;
DuckDB Read-Only
// Feature: duckdb (or duckdb-bundled)
use init_duckdb_read_only;
use Path;
// Read-only file-backed DuckDB database (pool of 5 connections)
let db_path = new;
let db = init_duckdb_read_only?;
DuckDB Configuration
// Feature: duckdb (or duckdb-bundled)
use ;
use ;
use Path;
let config = DuckDbConfig ;
let db = init_duckdb_with_options?;
let read_only_config = DuckDbConfig ;
let db = init_duckdb_read_only_with_options?;
init_duckdb and init_duckdb_read_only also read these environment variables:
SWITCHY_DUCKDB_MODE="deterministic" # or "pooled"
SWITCHY_DUCKDB_CONSISTENCY="strict" # or "relaxed"
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
MySQL Features
mysql: Enable MySQL supportmysql-sqlx: SQLx MySQL implementation
SQLite Features
sqlite: Enable SQLite supportsqlite-rusqlite: Rusqlite implementationsqlite-sqlx: SQLx SQLite implementation
Turso Features
turso: Turso/libSQL database support
DuckDB Features
duckdb: DuckDB database support (requires system libduckdb)duckdb-bundled: DuckDB with bundled library (no system install required)
Other Features
simulator: Mock database for testingcreds: AWS SSM credential management (optional)tls: TLS support via rustls
Connection Strings
Supported URL Formats
The Credentials::from_url() function supports the following URL formats:
# PostgreSQL
# MySQL
# Examples
DATABASE_URL="postgres://myuser:mypass@localhost:5432/mydb"
DATABASE_URL="mysql://root:secret@127.0.0.1:3306/app_db"
Individual Environment Variables
# Individual variables
DB_HOST="localhost"
DB_NAME="mydb"
DB_USER="username"
DB_PASSWORD="password"
Dependencies
- switchy_database: Generic database trait abstraction
- switchy_env: Environment variable utilities
- Tokio Postgres: PostgreSQL async driver (optional)
- deadpool-postgres: Connection pooling for PostgreSQL (optional)
- SQLx: Multi-database async driver (optional)
- Rusqlite: SQLite synchronous driver (optional)
- DuckDB: DuckDB embedded database driver (optional)
- Native TLS: TLS implementation (optional)
- OpenSSL: Alternative TLS implementation (optional)
- AWS SDK: For SSM parameter store credential management (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
- Analytics: DuckDB connections for analytical workloads
- Multi-tenant Applications: Dynamic database connections