BaseRepository

Trait BaseRepository 

Source
pub trait BaseRepository {
    type Entity: for<'r> FromRow<'r, PgRow> + DeserializeOwned + Serialize;
    type PkType: for<'r> Type<Postgres>;

    // Required methods
    fn get_entities(
        &self,
        page: i32,
        page_size: i32,
        order_by: &'static str,
        direction: PaginationOrder,
    ) -> impl Future<Output = Result<Vec<Self::Entity>>> + Send;
    fn get_entity(
        &self,
        pk: Self::PkType,
    ) -> impl Future<Output = Result<Self::Entity>> + Send;
    fn update_entity(
        &self,
        entity: Self::Entity,
    ) -> impl Future<Output = Result<u64>> + Send;
    fn create_entity(
        &self,
        entity: Self::Entity,
    ) -> impl Future<Output = Result<Self::Entity>> + Send;
    fn delete_entity(
        &self,
        pk: Self::PkType,
    ) -> impl Future<Output = Result<u64>> + Send;
}
Expand description

This trait will be used for everything that wants to play around with your entity on a very basic level.

To get access to filtering the PaginatedSearch should be used as this trait already handles the entity itself most of the time.

Required Associated Types§

Source

type Entity: for<'r> FromRow<'r, PgRow> + DeserializeOwned + Serialize

The entity that this repository will output

Source

type PkType: for<'r> Type<Postgres>

The type of the primary key. It can consist of a tuple of multiple types if a compound key is used

Required Methods§

Source

fn get_entities( &self, page: i32, page_size: i32, order_by: &'static str, direction: PaginationOrder, ) -> impl Future<Output = Result<Vec<Self::Entity>>> + Send

Gets a list of entities respecting the PaginationQuery

To do a filtered list use the PaginatedSearch helper

§Important Note

The order_by is supposed to be only a column name. Whoever implements this trait can rely on that. That said whoever calls this method has to make sure, no SQL data is inserted here.

Source

fn get_entity( &self, pk: Self::PkType, ) -> impl Future<Output = Result<Self::Entity>> + Send

Gets a single entity by it’s primary key(s)

Source

fn update_entity( &self, entity: Self::Entity, ) -> impl Future<Output = Result<u64>> + Send

Updates an entity. Reads the primary key(s) from the input

Source

fn create_entity( &self, entity: Self::Entity, ) -> impl Future<Output = Result<Self::Entity>> + Send

Creates a new entity. The implementation has to make sure, that the primary key(s) are handled properly (eg. by ignoring them)

Source

fn delete_entity( &self, pk: Self::PkType, ) -> impl Future<Output = Result<u64>> + Send

Deletes the given entity. Returns the amount of affected rows (ideally it’s a 0 if the entity does not exist anymore or a 1 if the PK was matched and the entry deleted)

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§