Trait ensemble::Model

source ·
pub trait Model: DeserializeOwned + Serialize + Sized + Send + Sync + Debug + Default {
    type PrimaryKey: Display + DeserializeOwned + Serialize + PartialEq + Default + Clone + Send + Sync;

    const NAME: &'static str;
    const TABLE_NAME: &'static str;
    const PRIMARY_KEY: &'static str;

    // Required methods
    fn primary_key(&self) -> &Self::PrimaryKey;
    fn find(
        key: Self::PrimaryKey
    ) -> impl Future<Output = Result<Self, Error>> + Send;
    fn create(self) -> impl Future<Output = Result<Self, Error>> + Send;
    fn save(&mut self) -> impl Future<Output = Result<(), Error>> + Send;
    fn fresh(&self) -> impl Future<Output = Result<Self, Error>> + Send;

    // Provided methods
    fn all() -> impl Future<Output = Result<Vec<Self>, Error>> + Send { ... }
    fn delete(self) -> impl Future<Output = Result<(), Error>> + Send { ... }
    fn query() -> Builder { ... }
    fn with<T: Into<EagerLoad>>(eager_load: T) -> Builder { ... }
    fn load<T: Into<EagerLoad> + Send>(
        &mut self,
        relation: T
    ) -> impl Future<Output = Result<(), Error>> + Send { ... }
    fn increment(
        &mut self,
        column: &str,
        amount: u64
    ) -> impl Future<Output = Result<(), Error>> + Send { ... }
    fn json(&self) -> Value { ... }
}

Required Associated Types§

source

type PrimaryKey: Display + DeserializeOwned + Serialize + PartialEq + Default + Clone + Send + Sync

The type of the primary key for the model.

Required Associated Constants§

source

const NAME: &'static str

The name of the model.

source

const TABLE_NAME: &'static str

The name of the table for the model

source

const PRIMARY_KEY: &'static str

The name of the primary key field for the model.

Required Methods§

source

fn primary_key(&self) -> &Self::PrimaryKey

Returns the value of the model’s primary key.

source

fn find( key: Self::PrimaryKey ) -> impl Future<Output = Result<Self, Error>> + Send

Find a model by its primary key.

Errors

Returns an error if the model cannot be found, or if a connection to the database cannot be established.

source

fn create(self) -> impl Future<Output = Result<Self, Error>> + Send

Insert a new model into the database.

Errors

Returns an error if the model cannot be inserted, or if a connection to the database cannot be established.

source

fn save(&mut self) -> impl Future<Output = Result<(), Error>> + Send

Update the model in the database.

Errors

Returns an error if the model cannot be updated, or if a connection to the database cannot be established.

source

fn fresh(&self) -> impl Future<Output = Result<Self, Error>> + Send

Reload a fresh model instance from the database.

Errors

Returns an error if the model cannot be retrieved, or if a connection to the database cannot be established.

Provided Methods§

source

fn all() -> impl Future<Output = Result<Vec<Self>, Error>> + Send

Get all of the models from the database.

Errors

Returns an error if the query fails, or if a connection to the database cannot be established.

source

fn delete(self) -> impl Future<Output = Result<(), Error>> + Send

Delete the model from the database.

Errors

Returns an error if the model cannot be deleted, or if a connection to the database cannot be established.

source

fn query() -> Builder

Begin querying the model.

source

fn with<T: Into<EagerLoad>>(eager_load: T) -> Builder

Begin querying a model with eager loading.

source

fn load<T: Into<EagerLoad> + Send>( &mut self, relation: T ) -> impl Future<Output = Result<(), Error>> + Send

Load a relationship for the model.

source

fn increment( &mut self, column: &str, amount: u64 ) -> impl Future<Output = Result<(), Error>> + Send

source

fn json(&self) -> Value

Convert the model to a JSON value.

Panics

Panics if the model cannot be converted to JSON. Since Ensemble manually implement Serialize, this should never happen.

Object Safety§

This trait is not object safe.

Implementors§