Crate typesafe_repository

Source
Expand description

typesafe_repository is designed to abstract data persistence, so its implementations can be easily swapped for testing or migration purposes

You can create your own repository by implementing traits like Add, List, Get, and plenty of others (see async_ops for complete list)

Typical way to define abstract repository for some type is:

use typesafe_repository::{Identity, GetIdentity, Repository, Add, List, Remove};
use typesafe_repository::inmemory::InMemoryRepository;
use typesafe_repository::macros::Id;
use std::sync::Arc;

#[derive(Clone, Id)]
#[Id(get_id)]
pub struct Foo {
  id: usize,
}

trait FooRepository:
  Repository<Foo, Error = Box<dyn std::error::Error + Send + Sync>>
  + Add<Foo>
  + List<Foo>
  + Remove<Foo>
{}

impl<T> FooRepository for T
where T: Repository<Foo, Error = Box<dyn std::error::Error + Send + Sync>>
    + Add<Foo>
    + List<Foo>
    + Remove<Foo> {}

struct FooService {
  repo: Arc<dyn FooRepository>,
}

// You can mock any repository with InMemoryRepository since it implements all operations on
// all types
let repo = Arc::new(InMemoryRepository::new());
FooService { repo };

Re-exports§

pub use identity::*;
pub use async_ops::*;

Modules§

async_ops
identity
inmemory
macros
ops
operations for Repository
prelude
transactional
Transactional traits and wrapper for Repository

Traits§

Repository
Marker trait with Error definition for operations