Skip to main content

spg_sqlx/
pool.rs

1//! v7.16.0 — `SpgPool` type alias + convenience constructors.
2
3use sqlx_core::error::Error;
4use sqlx_core::pool::{Pool, PoolOptions};
5
6use crate::database::Spg;
7use crate::options::SpgConnectOptions;
8
9/// Drop-in replacement for `sqlx::PgPool` over an in-process
10/// SPG. Same `Pool<Spg>` shape — every sqlx-core API generic
11/// over `Pool<DB>` works against this alias.
12pub type SpgPool = Pool<Spg>;
13
14/// Pool builder hooks — re-exported for ergonomic
15/// `SpgPoolOptions::new()` calls in mailrs-shape code.
16pub type SpgPoolOptions = PoolOptions<Spg>;
17
18/// Convenience constructors that mirror sqlx's pool-construction
19/// shape (`PgPool::connect(url)` style). Implemented as an
20/// extension trait on [`SpgPool`] so consumers can write
21/// `SpgPool::connect_in_memory().await` directly.
22pub trait SpgPoolExt: Sized {
23    /// In-memory pool — single connection, no persistence.
24    fn connect_in_memory() -> futures_core::future::BoxFuture<'static, Result<SpgPool, Error>>;
25    /// File-backed pool at `path`.
26    fn connect_path(
27        path: std::path::PathBuf,
28    ) -> futures_core::future::BoxFuture<'static, Result<SpgPool, Error>>;
29}
30
31impl SpgPoolExt for SpgPool {
32    fn connect_in_memory() -> futures_core::future::BoxFuture<'static, Result<SpgPool, Error>> {
33        Box::pin(async {
34            SpgPoolOptions::new()
35                .max_connections(1)
36                .connect_with(SpgConnectOptions::in_memory())
37                .await
38        })
39    }
40
41    fn connect_path(
42        path: std::path::PathBuf,
43    ) -> futures_core::future::BoxFuture<'static, Result<SpgPool, Error>> {
44        Box::pin(async move {
45            SpgPoolOptions::new()
46                .max_connections(1)
47                .connect_with(SpgConnectOptions::file(path))
48                .await
49        })
50    }
51}