Model

Trait Model 

Source
pub trait Model{
    type Id: IdGuard;

    // Required methods
    fn id(&self) -> &Id<Self, Self::Id>;
    fn set_id(&mut self, id: Id<Self, Self::Id>);

    // Provided methods
    fn get_by_id<'life0, 'async_trait, B, T>(
        db: &'life0 Db<B>,
        id: T,
    ) -> Pin<Box<dyn Future<Output = Result<Option<Self>>> + Send + 'async_trait>>
       where Self: Context<Self::Id, B> + 'static + Send + 'async_trait,
             T: Into<Id<Self, Self::Id>> + Send + Sync + 'async_trait,
             B: Backend + 'async_trait,
             'life0: 'async_trait { ... }
    fn save<'life0, 'life1, 'async_trait, B>(
        &'life0 mut self,
        db: &'life1 Db<B>,
    ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
       where Self: Context<Self::Id, B> + 'static + Send + 'async_trait,
             B: Backend + 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn delete<'life0, 'life1, 'async_trait, B>(
        &'life0 mut self,
        db: &'life1 Db<B>,
    ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
       where Self: Context<Self::Id, B> + 'static + Send + 'async_trait,
             B: Backend + 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn find_one<'life0, 'async_trait, B>(
        db: &'life0 Db<B>,
        filter: B::Filter,
    ) -> Pin<Box<dyn Future<Output = Result<Option<Self>>> + Send + 'async_trait>>
       where Self: Context<Self::Id, B> + 'static + Send + 'async_trait,
             B: Backend + 'async_trait,
             'life0: 'async_trait { ... }
}
Expand description

Exposes basic database operations for a model.

This trait is database-agnostic and only exposes basic database operations. More complex database-specific operations are implemented in other traits, such as MongoModel.

Most of the time you should not need to implement this trait yourself. Instead, use the model macro. Ideally, you should try to use only the generic operations provided by this trait, and avoid using database-specific operations unless absolutely necessary.

§The #[model] macro

The #[model] macro makes it very straight-forward to derive Model for your structs.

Required Associated Types§

Required Methods§

Source

fn id(&self) -> &Id<Self, Self::Id>

Get the ID of this model.

Source

fn set_id(&mut self, id: Id<Self, Self::Id>)

Set the ID of this model.

Provided Methods§

Source

fn get_by_id<'life0, 'async_trait, B, T>( db: &'life0 Db<B>, id: T, ) -> Pin<Box<dyn Future<Output = Result<Option<Self>>> + Send + 'async_trait>>
where Self: Context<Self::Id, B> + 'static + Send + 'async_trait, T: Into<Id<Self, Self::Id>> + Send + Sync + 'async_trait, B: Backend + 'async_trait, 'life0: 'async_trait,

Get a model by its ID from a database.

Source

fn save<'life0, 'life1, 'async_trait, B>( &'life0 mut self, db: &'life1 Db<B>, ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: Context<Self::Id, B> + 'static + Send + 'async_trait, B: Backend + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Save this model to a database.

Source

fn delete<'life0, 'life1, 'async_trait, B>( &'life0 mut self, db: &'life1 Db<B>, ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: Context<Self::Id, B> + 'static + Send + 'async_trait, B: Backend + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Delete this model from a database.

Source

fn find_one<'life0, 'async_trait, B>( db: &'life0 Db<B>, filter: B::Filter, ) -> Pin<Box<dyn Future<Output = Result<Option<Self>>> + Send + 'async_trait>>
where Self: Context<Self::Id, B> + 'static + Send + 'async_trait, B: Backend + 'async_trait, 'life0: 'async_trait,

Find a single model from a database by a filter.

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§