mobc_sqlx/
lib.rs

1use std::marker::PhantomData;
2
3pub use mobc;
4pub use sqlx;
5
6use mobc::{Manager, async_trait};
7use sqlx::{Connection as _, Database};
8
9pub struct SqlxConnectionManager<DB>
10where
11    DB: Database + Sync,
12{
13    url: &'static str,
14    _phantom: PhantomData<DB>,
15}
16
17impl<DB> SqlxConnectionManager<DB>
18where
19    DB: Database + Sync,
20{
21    #[must_use]
22    pub const fn new(url: &'static str) -> Self {
23        Self {
24            url,
25            _phantom: PhantomData,
26        }
27    }
28}
29
30#[async_trait]
31impl<DB> Manager for SqlxConnectionManager<DB>
32where
33    DB: Database + Sync,
34{
35    type Connection = DB::Connection;
36    type Error = sqlx::Error;
37
38    async fn connect(&self) -> Result<Self::Connection, Self::Error> {
39        Self::Connection::connect(self.url).await
40    }
41
42    async fn check(
43        &self,
44        mut conn: Self::Connection,
45    ) -> Result<Self::Connection, Self::Error> {
46        conn.ping().await.map(|()| conn)
47    }
48}