Skip to main content

ModelExt

Trait ModelExt 

Source
pub trait ModelExt: Model {
    // Provided methods
    fn query() -> QueryBuilder { ... }
    fn hydrate(rows: &[Row]) -> Result<Vec<Self>> { ... }
    fn all(db: &Database) -> impl Future<Output = Result<Vec<Self>>> + Send
       where Self: Send { ... }
    fn find(
        db: &Database,
        key: Self::Key,
    ) -> impl Future<Output = Result<Option<Self>>> + Send
       where Self: Send { ... }
    fn find_or_fail(
        db: &Database,
        key: Self::Key,
    ) -> impl Future<Output = Result<Self>> + Send
       where Self: Send { ... }
    fn get(
        db: &Database,
        query: QueryBuilder,
    ) -> impl Future<Output = Result<Vec<Self>>> + Send
       where Self: Send { ... }
    fn first(
        db: &Database,
        query: QueryBuilder,
    ) -> impl Future<Output = Result<Option<Self>>> + Send
       where Self: Send { ... }
    fn count(db: &Database) -> impl Future<Output = Result<i64>> + Send
       where Self: Send { ... }
    fn insert(
        &mut self,
        db: &Database,
    ) -> impl Future<Output = Result<()>> + Send
       where Self: Send { ... }
    fn update(&self, db: &Database) -> impl Future<Output = Result<u64>> + Send
       where Self: Send + Sync { ... }
    fn delete(&self, db: &Database) -> impl Future<Output = Result<u64>> + Send
       where Self: Send + Sync { ... }
    fn to_json(&self) -> Json
       where Self: Sync { ... }
}
Expand description

The query and persistence methods every model gets.

A blanket implementation, so a model author writes no code for any of this.

Provided Methods§

Source

fn query() -> QueryBuilder

Start a query against this model’s table.

Source

fn hydrate(rows: &[Row]) -> Result<Vec<Self>>

Run a builder and map the rows into models.

Source

fn all(db: &Database) -> impl Future<Output = Result<Vec<Self>>> + Send
where Self: Send,

Source

fn find( db: &Database, key: Self::Key, ) -> impl Future<Output = Result<Option<Self>>> + Send
where Self: Send,

Find by primary key.

Source

fn find_or_fail( db: &Database, key: Self::Key, ) -> impl Future<Output = Result<Self>> + Send
where Self: Send,

Find by primary key, or fail with a message naming the model.

Source

fn get( db: &Database, query: QueryBuilder, ) -> impl Future<Output = Result<Vec<Self>>> + Send
where Self: Send,

Run a prepared builder and hydrate the results.

Source

fn first( db: &Database, query: QueryBuilder, ) -> impl Future<Output = Result<Option<Self>>> + Send
where Self: Send,

Source

fn count(db: &Database) -> impl Future<Output = Result<i64>> + Send
where Self: Send,

Source

fn insert(&mut self, db: &Database) -> impl Future<Output = Result<()>> + Send
where Self: Send,

Insert this record and adopt the key the database generated.

Source

fn update(&self, db: &Database) -> impl Future<Output = Result<u64>> + Send
where Self: Send + Sync,

Update the row this record’s key points at.

Source

fn delete(&self, db: &Database) -> impl Future<Output = Result<u64>> + Send
where Self: Send + Sync,

Source

fn to_json(&self) -> Json
where Self: Sync,

This record as JSON, for an API response.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<T: Model> ModelExt for T