Skip to main content

ModelMut

Trait ModelMut 

Source
pub trait ModelMut: Model
where Self::Model: ModelTrait<Entity = Self> + IntoActiveModel<Self::ActiveModel> + Send + Sync, Self::ActiveModel: ActiveModelTrait<Entity = Self> + ActiveModelBehavior + Send,
{ // Provided methods fn insert_one<'async_trait>( model: Self::ActiveModel, ) -> Pin<Box<dyn Future<Output = Result<Self::Model, FrameworkError>> + Send + 'async_trait>> where Self: Send + 'async_trait { ... } fn update_one<'async_trait>( model: Self::ActiveModel, ) -> Pin<Box<dyn Future<Output = Result<Self::Model, FrameworkError>> + Send + 'async_trait>> where Self: Send + 'async_trait { ... } fn delete_by_pk<'async_trait, K>( id: K, ) -> Pin<Box<dyn Future<Output = Result<u64, FrameworkError>> + Send + 'async_trait>> where K: Into<<Self::PrimaryKey as PrimaryKeyTrait>::ValueType> + Send + 'async_trait, Self: Send + 'async_trait { ... } fn save_one<'async_trait>( model: Self::ActiveModel, ) -> Pin<Box<dyn Future<Output = Result<Self::Model, FrameworkError>> + Send + 'async_trait>> where Self::ActiveModel: TryIntoModel<Self::Model>, Self: Send + 'async_trait { ... } }
Expand description

Trait providing Laravel-like write operations on SeaORM entities

Implement this trait alongside Model to get insert/update/delete methods.

§Example

use ferro_rs::database::{Model, ModelMut};
use sea_orm::Set;

// Implement both traits
impl ferro_rs::database::Model for Entity {}
impl ferro_rs::database::ModelMut for Entity {}

// Insert a new record
let new_user = user::ActiveModel {
    name: Set("John".to_string()),
    email: Set("john@example.com".to_string()),
    ..Default::default()
};
let user = user::Entity::insert_one(new_user).await?;

// Delete by ID
user::Entity::delete_by_pk(user.id).await?;

Provided Methods§

Source

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

Insert a new record

§Example
let new_user = user::ActiveModel {
    name: Set("John".to_string()),
    email: Set("john@example.com".to_string()),
    ..Default::default()
};
let user = user::Entity::insert_one(new_user).await?;
Source

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

Update an existing record

§Example
let mut user: user::ActiveModel = user.into();
user.name = Set("Updated Name".to_string());
let updated = user::Entity::update_one(user).await?;
Source

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

Delete a record by primary key

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

fn save_one<'async_trait>( model: Self::ActiveModel, ) -> Pin<Box<dyn Future<Output = Result<Self::Model, FrameworkError>> + Send + 'async_trait>>
where Self::ActiveModel: TryIntoModel<Self::Model>, Self: Send + 'async_trait,

Save a model (insert or update based on whether primary key is set)

§Example
let user = user::ActiveModel {
    name: Set("John".to_string()),
    ..Default::default()
};
let saved = user::Entity::save_one(user).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§