use crate::database::{CommandUnitOfWork, QueryUnitOfWork};
use crate::snapshot::EntityTreeSnapshot;
use crate::types::{EntityId, HasId};
use anyhow::Result;
use std::collections::HashMap;
pub trait ReadUoW: QueryUnitOfWork {
type Entity;
fn get(&self, id: &EntityId) -> Result<Option<Self::Entity>>;
fn get_multi(&self, ids: &[EntityId]) -> Result<Vec<Option<Self::Entity>>>;
fn get_all(&self) -> Result<Vec<Self::Entity>>;
}
pub trait ReadUoWFactory {
type Entity;
fn create(&self) -> Box<dyn ReadUoW<Entity = Self::Entity>>;
}
pub trait WriteUoW: CommandUnitOfWork {
type Entity: Clone + HasId + Send;
fn get(&self, id: &EntityId) -> Result<Option<Self::Entity>>;
fn get_multi(&self, ids: &[EntityId]) -> Result<Vec<Option<Self::Entity>>>;
fn get_all(&self) -> Result<Vec<Self::Entity>>;
fn create_orphan_multi(&self, entities: &[Self::Entity]) -> Result<Vec<Self::Entity>>;
fn update_multi(&self, entities: &[Self::Entity]) -> Result<Vec<Self::Entity>>;
fn update_with_relationships_multi(
&self,
entities: &[Self::Entity],
) -> Result<Vec<Self::Entity>>;
fn remove(&self, id: &EntityId) -> Result<()>;
fn remove_multi(&self, ids: &[EntityId]) -> Result<()>;
fn snapshot(&self, ids: &[EntityId]) -> Result<EntityTreeSnapshot>;
fn restore(&self, snap: &EntityTreeSnapshot) -> Result<()>;
}
pub trait WriteUoWFactory: Send + Sync {
type Entity: Clone + HasId + Send;
fn create(&self) -> Box<dyn WriteUoW<Entity = Self::Entity>>;
}
pub trait OwnedWriteUoW: WriteUoW {
fn create_multi(
&self,
entities: &[Self::Entity],
owner_id: EntityId,
index: i32,
) -> Result<Vec<Self::Entity>>;
fn get_relationships_from_owner(&self, owner_id: &EntityId) -> Result<Vec<EntityId>>;
fn set_relationships_in_owner(&self, owner_id: &EntityId, ids: &[EntityId]) -> Result<()>;
}
pub trait OwnedWriteUoWFactory: Send + Sync {
type Entity: Clone + HasId + Send;
fn create(&self) -> Box<dyn OwnedWriteUoW<Entity = Self::Entity>>;
}
pub trait ReadRelUoW<RF>: QueryUnitOfWork {
fn get_relationship(&self, id: &EntityId, field: &RF) -> Result<Vec<EntityId>>;
fn get_relationship_many(
&self,
ids: &[EntityId],
field: &RF,
) -> Result<HashMap<EntityId, Vec<EntityId>>>;
fn get_relationship_count(&self, id: &EntityId, field: &RF) -> Result<usize>;
fn get_relationship_in_range(
&self,
id: &EntityId,
field: &RF,
offset: usize,
limit: usize,
) -> Result<Vec<EntityId>>;
}
pub trait ReadRelUoWFactory<RF> {
fn create(&self) -> Box<dyn ReadRelUoW<RF>>;
}
pub trait WriteRelUoW<RF>: CommandUnitOfWork {
fn set_relationship(&self, id: &EntityId, field: &RF, right_ids: &[EntityId]) -> Result<()>;
fn move_relationship(
&self,
id: &EntityId,
field: &RF,
ids_to_move: &[EntityId],
new_index: i32,
) -> Result<Vec<EntityId>>;
}
pub trait WriteRelUoWFactory<RF>: Send + Sync {
fn create(&self) -> Box<dyn WriteRelUoW<RF>>;
}