Skip to main content

shaperail_runtime/db/
store.rs

1use std::collections::HashMap;
2use std::sync::Arc;
3
4use async_trait::async_trait;
5use serde_json::{Map, Value};
6
7use super::{FilterSet, PageRequest, SearchParam, SortParam};
8use crate::db::ResourceRow;
9use shaperail_core::{EndpointSpec, ShaperailError};
10
11pub type StoreRegistry = Arc<HashMap<String, Arc<dyn ResourceStore>>>;
12
13/// Typed resource store implemented by generated per-resource query modules.
14#[async_trait]
15pub trait ResourceStore: Send + Sync {
16    fn resource_name(&self) -> &str;
17
18    async fn find_by_id(&self, id: &uuid::Uuid) -> Result<ResourceRow, ShaperailError>;
19
20    async fn find_all(
21        &self,
22        endpoint: &EndpointSpec,
23        filters: &FilterSet,
24        search: Option<&SearchParam>,
25        sort: &SortParam,
26        page: &PageRequest,
27    ) -> Result<(Vec<ResourceRow>, Value), ShaperailError>;
28
29    async fn insert(&self, data: &Map<String, Value>) -> Result<ResourceRow, ShaperailError>;
30
31    async fn update_by_id(
32        &self,
33        id: &uuid::Uuid,
34        data: &Map<String, Value>,
35    ) -> Result<ResourceRow, ShaperailError>;
36
37    async fn soft_delete_by_id(&self, id: &uuid::Uuid) -> Result<ResourceRow, ShaperailError>;
38
39    async fn hard_delete_by_id(&self, id: &uuid::Uuid) -> Result<ResourceRow, ShaperailError>;
40}