Skip to main content

Repository

Trait Repository 

Source
pub trait Repository<T>: Send + Sync {
    type Filter: Send;
    type Id: Send + Sync + Clone;

    // Required methods
    fn find(
        &self,
        id: Self::Id,
    ) -> impl Future<Output = Result<Option<T>, AppError>> + Send;
    fn query(
        &self,
        filter: Self::Filter,
    ) -> impl Future<Output = Result<Vec<T>, AppError>> + Send;
    fn create(
        &self,
        value: T,
    ) -> impl Future<Output = Result<T, AppError>> + Send;
    fn update(
        &self,
        id: Self::Id,
        value: T,
    ) -> impl Future<Output = Result<T, AppError>> + Send;
    fn delete(
        &self,
        id: Self::Id,
    ) -> impl Future<Output = Result<(), AppError>> + Send;
}
Expand description

Storage-agnostic CRUD contract. Implemented automatically for any #[derive(Model)] SQL-family struct via larust_orm::AnyRepository<T> (a thin wrapper over the existing QueryBuilder/Model machinery - see that type’s own doc comment), and by hand for a non-SQL backend such as Firestore, DynamoDB, or MongoDB.

-> impl Future<...> + Send (rather than a plain async fn) matches larust_auth::Authenticatable’s own established reasoning: native async-fn-in-traits doesn’t propagate the Send bound on its returned future by default, and this trait needs to be usable from axum handlers that require Send futures. An implementation can still be written as an ordinary async fn - its desugared return type already satisfies -> impl Future<...>; only the trait’s own declaration needs the explicit spelling.

Required Associated Types§

Source

type Filter: Send

Opaque to this trait - see the module doc comment.

Source

type Id: Send + Sync + Clone

The value used to look a single record up and to target update/delete - typically a primary key or document id.

Required Methods§

Source

fn find( &self, id: Self::Id, ) -> impl Future<Output = Result<Option<T>, AppError>> + Send

Looks a single record up by id. Returns Ok(None) (not an error) when the id doesn’t resolve to a record.

Source

fn query( &self, filter: Self::Filter, ) -> impl Future<Output = Result<Vec<T>, AppError>> + Send

Returns every record matching filter.

Source

fn create(&self, value: T) -> impl Future<Output = Result<T, AppError>> + Send

Creates a new record, returning it as actually stored (e.g. with a generated id populated).

Source

fn update( &self, id: Self::Id, value: T, ) -> impl Future<Output = Result<T, AppError>> + Send

Replaces the record at id with value in full, returning the stored result. Not a partial/dirty-tracking update - the same “replace every field” contract #[derive(Model)]’s create() already has for inserts.

Source

fn delete( &self, id: Self::Id, ) -> impl Future<Output = Result<(), AppError>> + Send

Deletes the record at id. Not an error if it didn’t exist.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§