switchy_database_connection 0.3.0

Switchy database connection package
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# 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`).

```toml
[dependencies]
switchy_database_connection = { path = "../database_connection", default-features = false }

# PostgreSQL with native TLS
switchy_database_connection = {
    path = "../database_connection",
    default-features = false,
    features = ["postgres-raw", "postgres-native-tls"]
}

# SQLite with rusqlite
switchy_database_connection = {
    path = "../database_connection",
    default-features = false,
    features = ["sqlite", "sqlite-rusqlite"]
}

# Multiple backends
switchy_database_connection = {
    path = "../database_connection",
    default-features = false,
    features = [
        "postgres-sqlx",
        "sqlite-sqlx",
        "sqlite"
    ]
}

# Turso database
switchy_database_connection = {
    path = "../database_connection",
    default-features = false,
    features = ["turso"]
}

# DuckDB with bundled library
switchy_database_connection = {
    path = "../database_connection",
    default-features = false,
    features = ["duckdb-bundled"]
}
```

## Usage

The examples below assume `simulator` is not enabled.

### Basic Database Initialization

```rust
use switchy_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(),
        None,
        "mydb".to_string(),
        "user".to_string(),
        Some("password".to_string()),
    ));

    let db = init(None, pg_creds).await?;

    // Use database...
    Ok(())
}
```

### SQLite Database

```rust
use switchy_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(())
}
```

### Credential Management

```rust
use switchy_database_connection::Credentials;

// Create credentials manually
let creds = Credentials::new(
    "database.example.com".to_string(),  // host
    None,
    "production_db".to_string(),         // database name
    "app_user".to_string(),              // username
    Some("secure_password".to_string()), // password (optional)
);

// Or parse from connection string
let creds = Credentials::from_url("postgres://user:pass@localhost:5432/mydb")?;

// Use with database initialization
let db = switchy_database_connection::init(None, Some(creds)).await?;
```

### Environment Variables

The package supports multiple ways to provide credentials:

```bash
# Option 1: Connection string (recommended)
export DATABASE_URL="postgres://user:password@localhost:5432/mydb"

# Option 2: Individual environment variables
export DB_HOST="localhost"
export DB_NAME="mydb"
export DB_USER="user"
export DB_PASSWORD="password"
export DB_PORT="5432"

# Option 3: AWS SSM Parameters (requires 'creds' feature)
# Optional - defaults shown below
export SSM_DB_HOST_PARAM_NAME="moosicbox_db_hostname"    # default
export SSM_DB_NAME_PARAM_NAME="moosicbox_db_name"        # default
export SSM_DB_USER_PARAM_NAME="moosicbox_db_user"        # default
export SSM_DB_PASSWORD_PARAM_NAME="moosicbox_db_password" # default
```

### AWS Credentials (Optional)

To use AWS SSM parameter store for credentials, enable the `creds` feature:

```toml
[dependencies]
switchy_database_connection = {
    path = "../database_connection",
    features = ["postgres", "creds"]
}
```

```rust
use switchy_database_connection::creds::get_db_creds;

// Automatically fetches from DATABASE_URL, env vars, or AWS SSM
let creds = get_db_creds().await?;
let db = switchy_database_connection::init(None, Some(creds)).await?;
```

### PostgreSQL with TLS

```rust
// Feature: postgres-raw + postgres-native-tls
use switchy_database_connection::{init_postgres_raw_native_tls, Credentials};

let creds = Credentials::new(
    "secure-db.example.com".to_string(),
    None,
    "mydb".to_string(),
    "user".to_string(),
    Some("password".to_string()),
);

let db = init_postgres_raw_native_tls(creds).await?;
```

### PostgreSQL with OpenSSL

```rust
// Feature: postgres-raw + postgres-openssl
use switchy_database_connection::{init_postgres_raw_openssl, Credentials};

let creds = Credentials::new(
    "ssl-db.example.com".to_string(),
    None,
    "mydb".to_string(),
    "user".to_string(),
    Some("password".to_string()),
);

let db = init_postgres_raw_openssl(creds).await?;
```

### PostgreSQL without TLS

```rust
// Feature: postgres-raw
use switchy_database_connection::{init_postgres_raw_no_tls, Credentials};

let creds = Credentials::new(
    "local-db".to_string(),
    None,
    "mydb".to_string(),
    "user".to_string(),
    Some("password".to_string()),
);

let db = init_postgres_raw_no_tls(creds).await?;
```

### PostgreSQL with SQLx

```rust
// Feature: postgres-sqlx
use switchy_database_connection::{init_postgres_sqlx, Credentials};

let creds = Credentials::new(
    "localhost".to_string(),
    None,
    "mydb".to_string(),
    "user".to_string(),
    Some("password".to_string()),
);

let db = init_postgres_sqlx(creds).await?;
```

### MySQL with SQLx

```rust
// Feature: mysql-sqlx
use switchy_database_connection::{init_mysql_sqlx, Credentials};

let creds = Credentials::new(
    "localhost".to_string(),
    None,
    "mydb".to_string(),
    "user".to_string(),
    Some("password".to_string()),
);

let db = init_mysql_sqlx(creds).await?;
```

### SQLite with Rusqlite

```rust
// Feature: sqlite-rusqlite
use switchy_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)?;
```

### SQLite with SQLx

```rust
// Feature: sqlite-sqlx
use switchy_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?;
```

### Turso Database

```rust
// Feature: turso
use switchy_database_connection::init_turso_local;
use std::path::Path;

// File-based Turso database
let db_path = Path::new("./app.db");
let db = init_turso_local(Some(db_path)).await?;

// In-memory Turso database
let db = init_turso_local(None).await?;
```

### DuckDB Database

```rust
// Feature: duckdb (or duckdb-bundled)
use switchy_database_connection::init_duckdb;
use std::path::Path;

// File-based DuckDB database (pool of 5 connections)
let db_path = Path::new("./analytics.duckdb");
let db = init_duckdb(Some(db_path))?;

// In-memory DuckDB database
let db = init_duckdb(None)?;
```

### DuckDB Read-Only

```rust
// Feature: duckdb (or duckdb-bundled)
use switchy_database_connection::init_duckdb_read_only;
use std::path::Path;

// Read-only file-backed DuckDB database (pool of 5 connections)
let db_path = Path::new("./analytics.duckdb");
let db = init_duckdb_read_only(db_path)?;
```

### DuckDB Configuration

```rust
// Feature: duckdb (or duckdb-bundled)
use switchy_database::duckdb::{DuckDbConfig, DuckDbConsistency, DuckDbMode};
use switchy_database_connection::{init_duckdb_read_only_with_options, init_duckdb_with_options};
use std::path::Path;

let config = DuckDbConfig {
    mode: DuckDbMode::Pooled,
    consistency: DuckDbConsistency::Strict,
};

let db = init_duckdb_with_options(Some(Path::new("./analytics.duckdb")), config)?;

let read_only_config = DuckDbConfig {
    mode: DuckDbMode::Pooled,
    consistency: DuckDbConsistency::Strict,
};

let db = init_duckdb_read_only_with_options(Path::new("./analytics.duckdb"), read_only_config)?;
```

`init_duckdb` and `init_duckdb_read_only` also read these environment variables:

```bash
SWITCHY_DUCKDB_MODE="deterministic" # or "pooled"
SWITCHY_DUCKDB_CONSISTENCY="strict" # or "relaxed"
```

### Non-SQLite Initialization

```rust
use switchy_database_connection::{init_default_non_sqlite, Credentials};

// Initialize any non-SQLite database based on features
let creds = Some(Credentials::new(
    "localhost".to_string(),
    None,
    "mydb".to_string(),
    "user".to_string(),
    Some("password".to_string()),
));

let db = init_default_non_sqlite(creds).await?;
```

### Simulator Mode

```rust
// Feature: simulator
use switchy_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
}
```

## Error Handling

```rust
use switchy_database_connection::{init, InitDbError, Credentials};

match init(None, None).await {
    Ok(db) => {
        // Use database
    }
    Err(InitDbError::CredentialsRequired) => {
        eprintln!("Database credentials are required");
    }
    #[cfg(feature = "sqlite-rusqlite")]
    Err(InitDbError::InitSqlite(e)) => {
        eprintln!("SQLite initialization error: {}", e);
    }
    #[cfg(feature = "postgres")]
    Err(InitDbError::InitPostgres(e)) => {
        eprintln!("PostgreSQL initialization error: {}", e);
    }
    #[cfg(feature = "postgres")]
    Err(InitDbError::InitDatabase(e)) => {
        eprintln!("Database initialization error: {}", e);
    }
    #[cfg(feature = "sqlite-sqlx")]
    Err(InitDbError::InitSqliteSqlxDatabase(e)) => {
        eprintln!("SQLite SQLx initialization error: {}", e);
    }
    #[cfg(feature = "turso")]
    Err(InitDbError::InitTurso(e)) => {
        eprintln!("Turso initialization error: {}", e);
    }
    #[cfg(feature = "duckdb")]
    Err(InitDbError::InitDuckDb(e)) => {
        eprintln!("DuckDB initialization error: {}", e);
    }
    Err(InitDbError::Database(e)) => {
        eprintln!("Database error: {}", e);
    }
}
```

## Feature Flags

### PostgreSQL Features

- **`postgres-raw`**: Raw tokio-postgres implementation
- **`postgres-sqlx`**: SQLx PostgreSQL implementation
- **`postgres-native-tls`**: Native TLS support for PostgreSQL
- **`postgres-openssl`**: OpenSSL support for PostgreSQL

### MySQL Features

- **`mysql`**: Enable MySQL support
- **`mysql-sqlx`**: SQLx MySQL implementation

### SQLite Features

- **`sqlite`**: Enable SQLite support
- **`sqlite-rusqlite`**: Rusqlite implementation
- **`sqlite-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 testing
- **`creds`**: 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:

```bash
# PostgreSQL
postgres://user:password@host:port/database
postgresql://user:password@host:port/database

# MySQL
mysql://user:password@host:port/database

# Examples
DATABASE_URL="postgres://myuser:mypass@localhost:5432/mydb"
DATABASE_URL="mysql://root:secret@127.0.0.1:3306/app_db"
```

### Individual Environment Variables

```bash
# 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