pub struct DatabasePool { /* private fields */ }Expand description
Multi-database connection pool with lazy shard opening.
Wraps a default Database (the main database) plus a sharded cache of
lazily-opened shard databases. All shards share the same PRAGMAs and
migrations from the parent Config.
Cloning is cheap (reference count increment via Arc).
§Examples
ⓘ
use modo::db::{self, ConnExt, ConnQueryExt, DatabasePool};
let pool = DatabasePool::new(&config).await?;
// Default database:
let user: User = pool.conn(None).await?
.conn()
.query_one("SELECT id, name FROM users WHERE id = ?1", libsql::params!["u1"])
.await?;
// Tenant shard (lazy open + cache):
let user: User = pool.conn(tenant.db_shard.as_deref()).await?
.conn()
.query_one("SELECT id, name FROM users WHERE id = ?1", libsql::params!["u1"])
.await?;Implementations§
Source§impl DatabasePool
impl DatabasePool
Sourcepub async fn conn(&self, shard: Option<&str>) -> Result<Database>
pub async fn conn(&self, shard: Option<&str>) -> Result<Database>
Get a database connection by shard name.
None— returns the default database (instant, no lock).Some("name")— returns the cached shard database, opening it on first access at{base_path}/{name}.db.
Concurrent first-access to the same shard may open duplicate
connections; the last writer wins and the extra connection is dropped.
This is benign because connect is idempotent (PRAGMAs are
re-applied, migrations use checksum tracking).
§Errors
Returns an error if the shard name is invalid (empty, starts with .,
or contains path separators or a null byte) or if the shard database
fails to open.
Trait Implementations§
Source§impl Clone for DatabasePool
impl Clone for DatabasePool
Source§fn clone(&self) -> DatabasePool
fn clone(&self) -> DatabasePool
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for DatabasePool
impl !RefUnwindSafe for DatabasePool
impl Send for DatabasePool
impl Sync for DatabasePool
impl Unpin for DatabasePool
impl UnsafeUnpin for DatabasePool
impl !UnwindSafe for DatabasePool
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more