Skip to main content

text_document_common/direct_access/use_cases/
traits.rs

1// Generated by Qleany v1.5.0 from common_da_use_cases_traits.tera
2
3use crate::database::{CommandUnitOfWork, QueryUnitOfWork};
4use crate::snapshot::EntityTreeSnapshot;
5use crate::types::{EntityId, HasId};
6use anyhow::Result;
7use std::collections::HashMap;
8
9// ---------------------------------------------------------------------------
10// Read-only UoW traits
11// ---------------------------------------------------------------------------
12
13pub trait ReadUoW: QueryUnitOfWork {
14    type Entity;
15
16    fn get(&self, id: &EntityId) -> Result<Option<Self::Entity>>;
17    fn get_multi(&self, ids: &[EntityId]) -> Result<Vec<Option<Self::Entity>>>;
18    fn get_all(&self) -> Result<Vec<Self::Entity>>;
19}
20
21pub trait ReadUoWFactory {
22    type Entity;
23    fn create(&self) -> Box<dyn ReadUoW<Entity = Self::Entity>>;
24}
25
26// ---------------------------------------------------------------------------
27// Write UoW traits
28// ---------------------------------------------------------------------------
29
30pub trait WriteUoW: CommandUnitOfWork {
31    type Entity: Clone + HasId + Send;
32
33    fn get(&self, id: &EntityId) -> Result<Option<Self::Entity>>;
34    fn get_multi(&self, ids: &[EntityId]) -> Result<Vec<Option<Self::Entity>>>;
35    fn get_all(&self) -> Result<Vec<Self::Entity>>;
36    fn create_orphan_multi(&self, entities: &[Self::Entity]) -> Result<Vec<Self::Entity>>;
37    fn update_multi(&self, entities: &[Self::Entity]) -> Result<Vec<Self::Entity>>;
38    fn update_with_relationships_multi(
39        &self,
40        entities: &[Self::Entity],
41    ) -> Result<Vec<Self::Entity>>;
42    fn remove(&self, id: &EntityId) -> Result<()>;
43    fn remove_multi(&self, ids: &[EntityId]) -> Result<()>;
44    fn snapshot(&self, ids: &[EntityId]) -> Result<EntityTreeSnapshot>;
45    fn restore(&self, snap: &EntityTreeSnapshot) -> Result<()>;
46}
47
48pub trait WriteUoWFactory: Send + Sync {
49    type Entity: Clone + HasId + Send;
50    fn create(&self) -> Box<dyn WriteUoW<Entity = Self::Entity>>;
51}
52
53// ---------------------------------------------------------------------------
54// Owned-entity write UoW traits (entities with an owner)
55// ---------------------------------------------------------------------------
56
57pub trait OwnedWriteUoW: WriteUoW {
58    fn create_multi(
59        &self,
60        entities: &[Self::Entity],
61        owner_id: EntityId,
62        index: i32,
63    ) -> Result<Vec<Self::Entity>>;
64    fn get_relationships_from_owner(&self, owner_id: &EntityId) -> Result<Vec<EntityId>>;
65    fn set_relationships_in_owner(&self, owner_id: &EntityId, ids: &[EntityId]) -> Result<()>;
66}
67
68pub trait OwnedWriteUoWFactory: Send + Sync {
69    type Entity: Clone + HasId + Send;
70    fn create(&self) -> Box<dyn OwnedWriteUoW<Entity = Self::Entity>>;
71}
72
73// ---------------------------------------------------------------------------
74// Relationship UoW traits
75// ---------------------------------------------------------------------------
76
77pub trait ReadRelUoW<RF>: QueryUnitOfWork {
78    fn get_relationship(&self, id: &EntityId, field: &RF) -> Result<Vec<EntityId>>;
79    fn get_relationship_many(
80        &self,
81        ids: &[EntityId],
82        field: &RF,
83    ) -> Result<HashMap<EntityId, Vec<EntityId>>>;
84    fn get_relationship_count(&self, id: &EntityId, field: &RF) -> Result<usize>;
85    fn get_relationship_in_range(
86        &self,
87        id: &EntityId,
88        field: &RF,
89        offset: usize,
90        limit: usize,
91    ) -> Result<Vec<EntityId>>;
92}
93
94pub trait ReadRelUoWFactory<RF> {
95    fn create(&self) -> Box<dyn ReadRelUoW<RF>>;
96}
97
98pub trait WriteRelUoW<RF>: CommandUnitOfWork {
99    fn set_relationship(&self, id: &EntityId, field: &RF, right_ids: &[EntityId]) -> Result<()>;
100    fn move_relationship(
101        &self,
102        id: &EntityId,
103        field: &RF,
104        ids_to_move: &[EntityId],
105        new_index: i32,
106    ) -> Result<Vec<EntityId>>;
107}
108
109pub trait WriteRelUoWFactory<RF>: Send + Sync {
110    fn create(&self) -> Box<dyn WriteRelUoW<RF>>;
111}