pub trait Link<T: Record + Send> {
// Required method
fn link_query(&self) -> Query;
// Provided method
fn linked_models<'life0, 'life1, 'async_trait, D>(
&'life0 self,
db_access: &'life1 D
) -> Pin<Box<dyn Future<Output = Result<QueryResult<T>, Error>> + Send + 'async_trait>>
where Self: Sized + Sync + 'async_trait,
D: DatabaseAccess + ?Sized + 'async_trait,
T: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
}
Expand description
The Link
trait of the Aragog library.
It allows to define a query relation between different models.
Example
#[derive(Clone, Serialize, Deserialize, Record, Validate)]
pub struct Order {
pub content: String,
pub user_id: String,
}
#[derive(Clone, Serialize, Deserialize, Record, Validate)]
pub struct User {}
impl Link<Order> for DatabaseRecord<User> {
fn link_query(&self) -> Query {
Order::query().filter(Comparison::field("user_id").equals_str(self.key()).into())
}
}
let user = DatabaseRecord::create(User {}, &database_connection).await.unwrap();
let order = DatabaseRecord::create(
Order {
content: "content".to_string(),
user_id: user.key().clone()
},
&database_connection).await.unwrap();
let orders = user.linked_models(&database_connection).await.unwrap();
assert_eq!(user.key(), &orders.first().unwrap().user_id);
Required Methods§
sourcefn link_query(&self) -> Query
fn link_query(&self) -> Query
Defines the query to execute to find the T
models linked to Self
Example
#[derive(Clone, Serialize, Deserialize, Record, Validate)]
pub struct Order {
pub content: String,
pub user_id: String,
}
#[derive(Clone, Serialize, Deserialize, Record, Validate)]
pub struct User {}
impl Link<Order> for DatabaseRecord<User> {
fn link_query(&self) -> Query {
Order::query().filter(Comparison::field("user_id").equals_str(self.key()).into())
}
}
Provided Methods§
sourcefn linked_models<'life0, 'life1, 'async_trait, D>(
&'life0 self,
db_access: &'life1 D
) -> Pin<Box<dyn Future<Output = Result<QueryResult<T>, Error>> + Send + 'async_trait>>where
Self: Sized + Sync + 'async_trait,
D: DatabaseAccess + ?Sized + 'async_trait,
T: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn linked_models<'life0, 'life1, 'async_trait, D>( &'life0 self, db_access: &'life1 D ) -> Pin<Box<dyn Future<Output = Result<QueryResult<T>, Error>> + Send + 'async_trait>>where Self: Sized + Sync + 'async_trait, D: DatabaseAccess + ?Sized + 'async_trait, T: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,
Retrieves the records matching the defined link_query
. Type inference may be required.