Skip to main content

modo_db/
pool.rs

1use sea_orm::DatabaseConnection;
2use std::future::Future;
3use std::ops::Deref;
4use std::pin::Pin;
5
6/// Newtype around `sea_orm::DatabaseConnection`.
7///
8/// Registered as a managed service via `app.managed_service(db)` (which also
9/// handles graceful pool shutdown) and extracted in handlers via the [`Db`]
10/// extractor.
11///
12/// [`Db`]: crate::extractor::Db
13#[derive(Debug, Clone)]
14pub struct DbPool(pub(crate) DatabaseConnection);
15
16impl DbPool {
17    /// Access the underlying SeaORM connection.
18    pub fn connection(&self) -> &DatabaseConnection {
19        &self.0
20    }
21}
22
23impl Deref for DbPool {
24    type Target = DatabaseConnection;
25
26    fn deref(&self) -> &Self::Target {
27        &self.0
28    }
29}
30
31impl modo::GracefulShutdown for DbPool {
32    fn graceful_shutdown(&self) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
33        Box::pin(async {
34            if let Err(e) = self.0.close_by_ref().await {
35                tracing::warn!("Error closing database pool: {e}");
36            }
37        })
38    }
39}