Skip to main content

axum_admin/
adapter.rs

1use crate::error::AdminError;
2use async_trait::async_trait;
3use serde_json::Value;
4use std::collections::HashMap;
5
6#[derive(Debug, Clone, Default)]
7pub enum SortOrder {
8    #[default]
9    Asc,
10    Desc,
11}
12
13#[derive(Debug, Clone)]
14pub struct ListParams {
15    pub page: u64,
16    pub per_page: u64,
17    pub search: Option<String>,
18    pub search_columns: Vec<String>,
19    pub filters: HashMap<String, Value>,
20    pub order_by: Option<(String, SortOrder)>,
21}
22
23impl Default for ListParams {
24    fn default() -> Self {
25        Self {
26            page: 1,
27            per_page: 20,
28            search: None,
29            search_columns: Vec::new(),
30            filters: HashMap::new(),
31            order_by: None,
32        }
33    }
34}
35
36/// Handles the options list and junction-table read/write for a ManyToMany field.
37#[async_trait]
38pub trait ManyToManyAdapter: Send + Sync {
39    /// All available options as `(value, label)` pairs.
40    async fn fetch_options(&self) -> Result<Vec<(String, String)>, AdminError>;
41    /// The currently selected IDs for a given record.
42    async fn fetch_selected(&self, record_id: &Value) -> Result<Vec<String>, AdminError>;
43    /// Replace the current selection for a record (delete + insert in the junction table).
44    async fn save(&self, record_id: &Value, selected_ids: Vec<String>) -> Result<(), AdminError>;
45}
46
47#[async_trait]
48pub trait DataAdapter: Send + Sync {
49    async fn list(&self, params: ListParams) -> Result<Vec<HashMap<String, Value>>, AdminError>;
50    async fn get(&self, id: &Value) -> Result<HashMap<String, Value>, AdminError>;
51    async fn create(&self, data: HashMap<String, Value>) -> Result<Value, AdminError>;
52    async fn update(&self, id: &Value, data: HashMap<String, Value>) -> Result<(), AdminError>;
53    async fn delete(&self, id: &Value) -> Result<(), AdminError>;
54    async fn count(&self, params: &ListParams) -> Result<u64, AdminError>;
55}