Skip to main content

DatabaseFactory

Trait DatabaseFactory 

Source
pub trait DatabaseFactory: Factory + IntoActiveModel<Self::ActiveModel> {
    type Entity: EntityTrait<Model = Self>;
    type ActiveModel: ActiveModelTrait<Entity = Self::Entity> + ActiveModelBehavior + Send;

    // Provided method
    fn insert<'async_trait>(
        model: Self,
    ) -> Pin<Box<dyn Future<Output = Result<Self, FrameworkError>> + Send + 'async_trait>>
       where Self: Sized + Send + 'async_trait { ... }
}
Expand description

Trait for models that can be persisted to database via factories

Implement this trait for SeaORM entities to enable create() and create_many().

§Example

use ferro_rs::testing::{Factory, DatabaseFactory, Fake};
use sea_orm::ActiveValue::Set;

impl Factory for user::Model {
    fn definition() -> Self {
        Self {
            id: 0,
            name: Fake::name(),
            email: Fake::email(),
            created_at: chrono::Utc::now().naive_utc(),
        }
    }
}

impl DatabaseFactory for user::Model {
    type Entity = user::Entity;
    type ActiveModel = user::ActiveModel;

    fn to_active_model(model: Self) -> Self::ActiveModel {
        user::ActiveModel {
            name: Set(model.name),
            email: Set(model.email),
            ..Default::default()
        }
    }
}

Trait for models that can be persisted to database via factories

Implement this trait for SeaORM entities to enable create() and create_many().

Required Associated Types§

Source

type Entity: EntityTrait<Model = Self>

The SeaORM entity type

Source

type ActiveModel: ActiveModelTrait<Entity = Self::Entity> + ActiveModelBehavior + Send

The SeaORM active model type

Provided Methods§

Source

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

Insert a model into the database

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§