[][src]Struct dodo::Repository

pub struct Repository<T, S, R> { /* fields omitted */ }

Repository of entities, backed by provided storage.

See this crate root documentation for more information.

Example

use dodo::prelude::*;

type PersonRepository = Repository<Person, Directory, JsonSerializer>;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let directory = Directory::new(&path)?;
    let mut repository = PersonRepository::new(directory);

    let mut person = Person::new();
    repository.insert(&mut person)?;

    let entities = repository.find_all()?.collect()?;

    println!("{:?}", entities);

    Ok(())
}

Methods

impl<T, S, R> Repository<T, S, R> where
    T: Entity,
    S: Storage,
    R: Serializer
[src]

pub fn new(storage: S) -> Self[src]

Create a new repository, using provided storage.

Examples

use dodo::prelude::*;

type PersonRepository = Repository<Person, Directory, JsonSerializer>;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let directory = Directory::new(&path)?;
    let repository = PersonRepository::new(directory);
    Ok(())
}

pub fn find(&self, id: Uuid) -> Result<T>[src]

Find an specific entity, if it exists.

Returns an error if not found.

Examples

let person = repository.find(id);

match person {
    Ok(person) => println!("Found!"),
    Err(dodo::Error::NotFound(_)) => println!("Not found!"),
    Err(_) => println!("Other error!")
}

pub fn find_all(&self) -> Result<RepositoryCursor<T, S, S::Iterator, R>>[src]

Provide a cursor iterating through the entities in this repository.

Examples

let persons = repository.find_all()?.collect();

println!("{:#?}", persons);

pub fn insert(&mut self, entity: &mut T) -> Result<()>[src]

Insert entity into this repository.

The repository always assigns a new id to the entity, even if it already has one. Thus, the entity need to be mutable when inserted.

Examples

let mut person = Person::new();  //Required to be mutable
repository.insert(&mut person)?; //Assign new id to the entity.

pub fn update(&mut self, entity: &T) -> Result<()>[src]

Update entity in this repository to a new version.

The entity must exist in the repository and have an id.

Examples

let mut person = Person::new();
repository.insert(&mut person)?;

person.age = 1337;
repository.update(&person)?;

pub fn upsert(&mut self, entity: &T) -> Result<()>[src]

Update or insert entity into this repository.

The entity is created if it doesn't exists, using the id currently assigned to it. You will receive an error if the entity doesn't have an id.

Examples

let mut person = Person::new();
person.id = Some(Uuid::parse_str("78190929-3d84-4735-9e40-80e3cd5530e9").unwrap());
repository.upsert(&mut person)?; //Doesn't exist ? No problem here!

let mut person = Person::new();
repository.insert(&mut person)?;
person.age = 1337;
repository.upsert(&mut person)?; //Already exist ? No problem too!

pub fn delete(&mut self, id: Uuid) -> Result<()>[src]

Delete entity with provided id.

This does not fail if the entity doesn't exist.

Examples

let mut person = Person::new();
repository.insert(&mut person)?; //Inserted here.
repository.delete(person.id.unwrap())?; //Deleted here.

let id : Uuid = Uuid::parse_str("78190929-3d84-4735-9e40-80e3cd5530e9").unwrap();
repository.delete(id)?; //Doesn't exist ? No problem!

pub fn clear(&mut self) -> Result<()>[src]

Delete every entity in this repository.

Everything in this repository will be deleted. Use at your own risks.

Examples

repository.insert(&mut Person::new())?;
repository.insert(&mut Person::new())?;

repository.clear();

Trait Implementations

impl<T: Clone, S: Clone, R: Clone> Clone for Repository<T, S, R>[src]

impl<T: Debug, S: Debug, R: Debug> Debug for Repository<T, S, R>[src]

Auto Trait Implementations

impl<T, S, R> RefUnwindSafe for Repository<T, S, R> where
    R: RefUnwindSafe,
    S: RefUnwindSafe,
    T: RefUnwindSafe

impl<T, S, R> Send for Repository<T, S, R> where
    R: Send,
    S: Send,
    T: Send

impl<T, S, R> Sync for Repository<T, S, R> where
    R: Sync,
    S: Sync,
    T: Sync

impl<T, S, R> Unpin for Repository<T, S, R> where
    R: Unpin,
    S: Unpin,
    T: Unpin

impl<T, S, R> UnwindSafe for Repository<T, S, R> where
    R: UnwindSafe,
    S: UnwindSafe,
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,