Skip to main content

Model

Trait Model 

Source
pub trait Model: EntityTrait + Sized
where Self::Model: ModelTrait<Entity = Self> + Send + Sync,
{ // Provided methods fn all<'async_trait>( ) -> Pin<Box<dyn Future<Output = Result<Vec<Self::Model>, FrameworkError>> + Send + 'async_trait>> where Self: Send + 'async_trait { ... } fn find_by_pk<'async_trait, K>( id: K, ) -> Pin<Box<dyn Future<Output = Result<Option<Self::Model>, FrameworkError>> + Send + 'async_trait>> where K: Into<<Self::PrimaryKey as PrimaryKeyTrait>::ValueType> + Send + 'async_trait, Self: Send + 'async_trait { ... } fn find_or_fail<'async_trait, K>( id: K, ) -> Pin<Box<dyn Future<Output = Result<Self::Model, FrameworkError>> + Send + 'async_trait>> where K: Into<<Self::PrimaryKey as PrimaryKeyTrait>::ValueType> + Send + Debug + Copy + 'async_trait, Self: Send + 'async_trait { ... } fn count_all<'async_trait>( ) -> Pin<Box<dyn Future<Output = Result<u64, FrameworkError>> + Send + 'async_trait>> where Self: Send + 'async_trait { ... } fn exists_any<'async_trait>( ) -> Pin<Box<dyn Future<Output = Result<bool, FrameworkError>> + Send + 'async_trait>> where Self: Send + 'async_trait { ... } fn first<'async_trait>( ) -> Pin<Box<dyn Future<Output = Result<Option<Self::Model>, FrameworkError>> + Send + 'async_trait>> where Self: Send + 'async_trait { ... } }
Expand description

Trait providing Laravel-like read operations on SeaORM entities

Implement this trait on your SeaORM Entity to get convenient static methods for querying records.

§Example

use ferro_rs::database::Model;
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, DeriveEntityModel)]
#[sea_orm(table_name = "users")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    pub name: String,
    pub email: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

// Add Ferro's Model trait
impl ferro_rs::database::Model for Entity {}

// Now you can use:
let users = Entity::all().await?;
let user = Entity::find_by_pk(1).await?;

Provided Methods§

Source

fn all<'async_trait>() -> Pin<Box<dyn Future<Output = Result<Vec<Self::Model>, FrameworkError>> + Send + 'async_trait>>
where Self: Send + 'async_trait,

Find all records

§Example
let users = user::Entity::all().await?;
Source

fn find_by_pk<'async_trait, K>( id: K, ) -> Pin<Box<dyn Future<Output = Result<Option<Self::Model>, FrameworkError>> + Send + 'async_trait>>
where K: Into<<Self::PrimaryKey as PrimaryKeyTrait>::ValueType> + Send + 'async_trait, Self: Send + 'async_trait,

Find a record by primary key (generic version)

§Example
let user = user::Entity::find_by_pk(1).await?;
Source

fn find_or_fail<'async_trait, K>( id: K, ) -> Pin<Box<dyn Future<Output = Result<Self::Model, FrameworkError>> + Send + 'async_trait>>
where K: Into<<Self::PrimaryKey as PrimaryKeyTrait>::ValueType> + Send + Debug + Copy + 'async_trait, Self: Send + 'async_trait,

Find a record by primary key or return an error

§Example
let user = user::Entity::find_or_fail(1).await?;
Source

fn count_all<'async_trait>() -> Pin<Box<dyn Future<Output = Result<u64, FrameworkError>> + Send + 'async_trait>>
where Self: Send + 'async_trait,

Count all records

§Example
let count = user::Entity::count_all().await?;
Source

fn exists_any<'async_trait>() -> Pin<Box<dyn Future<Output = Result<bool, FrameworkError>> + Send + 'async_trait>>
where Self: Send + 'async_trait,

Check if any records exist

§Example
if user::Entity::exists_any().await? {
    println!("Users exist!");
}
Source

fn first<'async_trait>() -> Pin<Box<dyn Future<Output = Result<Option<Self::Model>, FrameworkError>> + Send + 'async_trait>>
where Self: Send + 'async_trait,

Get the first record

§Example
let first_user = user::Entity::first().await?;

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§