Skip to content

Commit a7a3e3c

Browse files
committed
add comment about axum
1 parent f0a1673 commit a7a3e3c

1 file changed

Lines changed: 39 additions & 1 deletion

File tree

chuchi-postgres/src/db.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
1+
//! Database module
2+
//!
3+
//! ## Axum
4+
//! If you wan't to use axum the best way is to make a small wrapper around
5+
//! ConnOwned.
6+
//!
7+
//! ```rust
8+
//! pub struct ConnOwned(pub pg::db::ConnOwned);
9+
//!
10+
//! impl ConnOwned {
11+
//! pub fn conn(&self) -> Conn {
12+
//! self.0.conn()
13+
//! }
14+
//!
15+
//! pub async fn trans(&mut self) -> Result<Trans, pg::Error> {
16+
//! self.0.trans().await
17+
//! }
18+
//! }
19+
//!
20+
//! impl<S> FromRequestParts<S> for ConnOwned
21+
//! where
22+
//! S: Send + Sync,
23+
//! Db: FromRef<S>,
24+
//! {
25+
//! type Rejection = Error;
26+
//!
27+
//! async fn from_request_parts(
28+
//! _parts: &mut Parts,
29+
//! state: &S,
30+
//! ) -> Result<Self, Self::Rejection> {
31+
//! let db = Db::from_ref(state);
32+
//! db.get()
33+
//! .await
34+
//! .map(Self)
35+
//! .map_err(|e| Error::Internal(e.to_string()))
36+
//! }
37+
//! }
38+
//! ```
39+
140
use crate::{
241
Connection, Database, Error,
342
connection::{ConnectionOwned, Transaction},
@@ -49,7 +88,6 @@ impl ConnOwned {
4988
}
5089

5190
// or transaction
52-
#[allow(dead_code)]
5391
pub async fn trans(&mut self) -> Result<Trans, Error> {
5492
match &mut self.pg {
5593
Some(pg) => Ok(Trans {

0 commit comments

Comments
 (0)