Skip to main content

Model

Trait Model 

Source
pub trait Model: Sized + Clone {
    type Id: ?Sized + PartialEq + Debug + ToOwned;

    // Required methods
    fn all(conn: &mut SqliteConnection) -> QueryResult<Vec<Self>>;
    fn find(
        conn: &mut SqliteConnection,
        id: &Self::Id,
    ) -> QueryResult<Option<Self>>;
    fn id_exists(
        conn: &mut SqliteConnection,
        id: &Self::Id,
    ) -> QueryResult<bool>;
}
Expand description

A trait representing a generic model with basic database operations.

This trait defines the structure for models that can be queried from a database. It includes methods for retrieving all records, finding a specific record by its ID, and checking if a record with a given ID exists.

§Type Parameters

  • Id: The type of the ID of the model. Must implement PartialEq and Debug.

§Methods

  • all: Queries all records in the database table.
  • find: Queries a record by its primary key.
  • id_exists: Checks if a record with the given primary key exists in the database table.

Required Associated Types§

Source

type Id: ?Sized + PartialEq + Debug + ToOwned

The type of the ID of the model.

Required Methods§

Source

fn all(conn: &mut SqliteConnection) -> QueryResult<Vec<Self>>

Queries all records in the database table.

§Arguments
  • conn: A mutable reference to the database connection.
§Returns

A QueryResult containing a vector of all records.

Source

fn find(conn: &mut SqliteConnection, id: &Self::Id) -> QueryResult<Option<Self>>

Queries a record by its primary key.

§Arguments
  • conn: A mutable reference to the database connection.
  • id: A reference to the ID of the record to find.
§Returns

A QueryResult containing an Option with the record if found.

Source

fn id_exists(conn: &mut SqliteConnection, id: &Self::Id) -> QueryResult<bool>

Checks if a record with the given primary key exists.

§Arguments
  • conn: A mutable reference to the database connection.
  • id: A reference to the ID of the record to check.
§Returns

A QueryResult containing a boolean indicating whether the record exists.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§