pub trait TryFromDb<T>: Sized {
type Error;
// Required method
fn try_from_db<'async_trait>(
value: T,
db: Arc<Box<dyn Database>>,
) -> Pin<Box<dyn Future<Output = Result<Self, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait;
}Expand description
Trait for converting types from database representations with database context
Similar to TryFrom but provides access to the database instance during conversion.
This is useful when converting database rows into domain objects that may need to
perform additional queries to load related data.
§Type Parameters
T- The input type to convert from (typically a database row or value)
§Example
ⓘ
use switchy_database::{TryFromDb, Database, DatabaseError, Row};
use std::sync::Arc;
struct User {
id: i64,
name: String,
}
#[async_trait]
impl TryFromDb<Row> for User {
type Error = DatabaseError;
async fn try_from_db(row: Row, db: Arc<Box<dyn Database>>) -> Result<Self, Self::Error> {
Ok(User {
id: row.get("id").unwrap().as_i64().unwrap(),
name: row.get("name").unwrap().as_str().unwrap().to_string(),
})
}
}Required Associated Types§
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".