Skip to content

Commit 8c3818a

Browse files
committed
chore: relocate Result type and update docs
1 parent 105c736 commit 8c3818a

3 files changed

Lines changed: 97 additions & 19 deletions

File tree

crates/sqlx-sqlite-conn-mgr/README.md

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ management.
1212
Prevents violation of access policies and/or a glut of open file handles and
1313
(mostly) idle threads
1414
* **Connection pooling**:
15-
* Read-only pool for concurrent reads (up to 6 connections)
15+
* Read-only pool for concurrent reads (default: 6 connections, configurable)
1616
* **Lazy write pool**: Single write connection pool (max_connections=1) initialized on
1717
first use
1818
* **Exclusive write access**: WriteGuard ensures serialized writes
19-
* **WAL mode**: Automatically enabled on first `acquire_writer()` call
20-
(idempotent)
19+
* **WAL mode**: Automatically enabled on first `acquire_writer()` call (setting
20+
journal mode to WAL is safe and idempotent)
2121
* See [WAL documentation](https://www.sqlite.org/wal.html) for details
2222
* **30-second idle timeout**: Both read and write connections close after
2323
30 seconds of inactivity
@@ -44,18 +44,19 @@ use std::sync::Arc;
4444
#[tokio::main]
4545
async fn main() -> Result<(), sqlx_sqlite_conn_mgr::Error> {
4646
// Connect to database (creates if missing, returns Arc<SqliteDatabase>)
47+
// (See below for how to customize the configuration)
4748
let db = SqliteDatabase::connect("example.db").await?;
4849

4950
// Multiple connects to the same path return the same instance
5051
let db2 = SqliteDatabase::connect("example.db").await?;
5152
assert!(Arc::ptr_eq(&db, &db2));
5253

53-
// Use read_pool() for SELECT queries (supports concurrent reads)
54+
// Use read_pool() for read queries (supports concurrent reads)
5455
let rows = query("SELECT * FROM users")
5556
.fetch_all(db.read_pool()?)
5657
.await?;
5758

58-
// Optionally acquire writer for INSERT/UPDATE/DELETE (exclusive access)
59+
// Optionally acquire writer for write queries (exclusive access)
5960
// WAL mode is enabled automatically on first call
6061
let mut writer = db.acquire_writer().await?;
6162
query("INSERT INTO users (name) VALUES (?)")
@@ -70,12 +71,39 @@ async fn main() -> Result<(), sqlx_sqlite_conn_mgr::Error> {
7071
}
7172
```
7273

74+
### Custom Configuration
75+
76+
Only customize the configuration when the defaults don't meet your requirements:
77+
78+
```rust
79+
use sqlx_sqlite_conn_mgr::{SqliteDatabase, SqliteDatabaseConfig};
80+
use std::time::Duration;
81+
82+
#[tokio::main]
83+
async fn main() -> Result<(), sqlx_sqlite_conn_mgr::Error> {
84+
// Only create custom configuration when defaults aren't suitable
85+
let custom_config = SqliteDatabaseConfig {
86+
max_read_connections: 10,
87+
idle_timeout: Duration::from_secs(60),
88+
};
89+
90+
// Pass custom configuration to connect()
91+
let db = SqliteDatabase::connect("example.db", Some(custom_config)).await?;
92+
93+
// Use the database as normal...
94+
db.close().await?;
95+
Ok(())
96+
}
97+
```
98+
7399
## API Overview
74100

75101
### `SqliteDatabase`
76102

77-
* `connect(path)` - Connect to a database (creates if missing, returns cached
78-
`Arc<SqliteDatabase>` if already open)
103+
* `connect(path, custom_config)` - Connect to a database (creates if missing,
104+
returns cached `Arc<SqliteDatabase>` if already open). Pass `None` for
105+
`custom_config` to use defaults (recommended for most use cases), or
106+
`Some(SqliteDatabaseConfig)` when you need to customize the configuration
79107
* `read_pool()` - Get reference to the read-only connection pool for read
80108
operations (returns `Result`)
81109
* `acquire_writer()` - Acquire exclusive write access (returns
@@ -85,6 +113,15 @@ async fn main() -> Result<(), sqlx_sqlite_conn_mgr::Error> {
85113
* `close_and_remove()` - Close and delete all database files (.db, .db-wal,
86114
.db-shm)
87115

116+
### `SqliteDatabaseConfig`
117+
118+
Configuration for connection pool behavior:
119+
120+
* `max_read_connections: u32` - Maximum number of concurrent read connections
121+
(default: 6)
122+
* `idle_timeout: Duration` - How long idle connections remain open before
123+
being closed (default: 30 seconds)
124+
88125
### `WriteGuard`
89126

90127
RAII guard that provides exclusive write access. Automatically returns the
@@ -111,12 +148,11 @@ queries.
111148
is released via `WriteGuard` drop.
112149

113150
5. **Connection Management**:
114-
* Read pool: 6 concurrent connections by default, 0 cached
115-
* Can be configured via `SqliteDatabaseConfig`
116-
* Write pool: max 1 connection, 0 cached
117-
* Idle timeout: 30 seconds for both pools
118-
* Can be configured via `SqliteDatabaseConfig`
119-
* No perpetual caching to minimize idle thread overhead
151+
* Read pool: 6 concurrent connections by default (configurable via `custom_config`)
152+
* Write pool: max 1 connection
153+
* Minimum connections: 0 (no perpetual caching)
154+
* Idle timeout: 30 seconds by default (configurable via `custom_config`)
155+
* Only customize `SqliteDatabaseConfig` when defaults don't meet your needs
120156

121157
## Error Handling
122158

crates/sqlx-sqlite-conn-mgr/src/error.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,3 @@ pub enum Error {
1818
#[error("Database has been closed")]
1919
DatabaseClosed,
2020
}
21-
22-
/// A type alias for Results with our Error type
23-
pub type Result<T> = std::result::Result<T, Error>;

crates/sqlx-sqlite-conn-mgr/src/lib.rs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,53 @@
1212
//!
1313
//! ## Architecture
1414
//!
15-
//! - **Dual pools**: Separate read-only pool (max 6 connections) and write pool (max 1 connection)
15+
//! - **Connection pooling**: Separate read-only pool and write pool with a max of 1 connection
1616
//! - **Lazy WAL mode**: Write-Ahead Logging enabled automatically on first write
1717
//! - **Exclusive writes**: Single-connection write pool enforces serialized write access
1818
//! - **Concurrent reads**: Multiple readers can query simultaneously via the read pool
19-
19+
//!
20+
//! ## Usage
21+
//!
22+
//! ```no_run
23+
//! use sqlx_sqlite_conn_mgr::SqliteDatabase;
24+
//! use std::sync::Arc;
25+
//!
26+
//! #[tokio::main]
27+
//! async fn main() -> sqlx_sqlite_conn_mgr::Result<()> {
28+
//! // Connect returns Arc<SqliteDatabase>
29+
//! let db = SqliteDatabase::connect("example.db", None).await?;
30+
//!
31+
//! // Multiple connects to the same path return the same instance
32+
//! let db2 = SqliteDatabase::connect("example.db", None).await?;
33+
//! assert!(Arc::ptr_eq(&db, &db2));
34+
//!
35+
//! // Use read_pool() for read queries (concurrent reads)
36+
//! let rows = sqlx::query("SELECT * FROM users")
37+
//! .fetch_all(db.read_pool()?)
38+
//! .await?;
39+
//!
40+
//! // Optionally acquire writer for write queries (exclusive)
41+
//! // WAL mode is enabled automatically on first call
42+
//! let mut writer = db.acquire_writer().await?;
43+
//! sqlx::query("INSERT INTO users (name) VALUES (?)")
44+
//! .bind("Alice")
45+
//! .execute(&mut *writer)
46+
//! .await?;
47+
//!
48+
//! // Close when done
49+
//! db.close().await?;
50+
//! Ok(())
51+
//! }
52+
//! ```
53+
//!
54+
//! ## Design Principles
55+
//!
56+
//! - Uses sqlx's `SqlitePoolOptions` for all pool configuration
57+
//! - Uses sqlx's `SqliteConnectOptions` for connection flags and configuration
58+
//! - Minimal custom logic - delegates to sqlx wherever possible
59+
//! - Global registry caches new database instances (with their pools) and returns existing ones
60+
//! - WAL mode is enabled lazily only when writes are needed
61+
//!
2062
// TODO: Remove these allows once implementation is complete
2163
#![allow(dead_code)]
2264
#![allow(unused)]
@@ -29,5 +71,8 @@ mod write_guard;
2971
// Re-export public types
3072
pub use config::SqliteDatabaseConfig;
3173
pub use database::SqliteDatabase;
32-
pub use error::{Error, Result};
74+
pub use error::Error;
3375
pub use write_guard::WriteGuard;
76+
77+
/// A type alias for Results with our custom Error type
78+
pub type Result<T> = std::result::Result<T, Error>;

0 commit comments

Comments
 (0)