br_addon/
module.rs

1use crate::action::Action;
2use crate::{Tools, GLOBAL_DATA};
3use crate::PLUGIN_TOOLS;
4use json::{array, object, JsonValue};
5use std::any::type_name;
6#[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
7use br_db::Db;
8
9/// 模型配置
10pub trait Module: Send + Sync + 'static {
11    /// =================基础配置=============================
12    /// 插件名称
13    fn _name(&self) -> &'static str {
14        type_name::<Self>().split("::").collect::<Vec<&str>>()[3].to_lowercase().leak()
15    }
16    /// 模型名称
17    /// xxx.xxx
18    fn module_name(&self) -> &'static str {
19        let t = type_name::<Self>().split("::").collect::<Vec<&str>>();
20        format!("{}.{}", t[2], t[3]).leak()
21    }
22    /// 模型标题
23    fn title(&self) -> &'static str;
24    /// 模型描述
25    fn description(&self) -> &'static str {
26        ""
27    }
28    /// 可用范围
29    fn tags(&self) -> &'static [&'static str] {
30        &["admin", "org"]
31    }
32    /// =================视图配置=============================
33    /// 模型icon
34    fn icon(&self) -> &'static str {
35        ""
36    }
37    /// =================数据库配置=============================
38    /// 是否数据表
39    fn table(&self) -> bool {
40        false
41    }
42    /// 数据库表名称
43    fn _table_name(&self) -> &'static str {
44        let mut it = type_name::<Self>().rsplitn(4, "::");
45        let _ = it.next().unwrap_or(""); // 最末段
46        let t2 = it.next().unwrap_or("");
47        let t3 = it.next().unwrap_or("");
48        let s = format!("{t3}_{t2}").to_lowercase();
49        Box::leak(s.into_boxed_str())
50    }
51    /// 主键
52    fn table_key(&self) -> &'static str {
53        "id"
54    }
55    /// 唯一索引
56    fn table_unique(&self) -> &'static [&'static str] {
57        &[]
58    }
59    /// 查询索引
60    fn table_index(&self) -> &'static [&'static [&'static str]] {
61        &[]
62    }
63    /// 是否分区
64    fn table_partition(&self) -> bool {
65        false
66    }
67    /// 分区配置
68    /// *列名
69    /// *分区key
70    /// *实例 array![str, array![range1, range2, range3...]]
71    /// *如果在已有分区的情况下想要修改分区的参照列,先要将当前分区代码删除后刷库,然后写新的分区规则再刷库
72    fn table_partition_columns(&self) -> JsonValue {
73        array![]
74    }
75    /// =================功能配置=============================
76    /// 加载功能
77    /// * name 功能名称
78    fn action(&mut self, name: &str) -> Result<Box<dyn Action>, String>;
79    /// 模型字段
80    fn fields(&mut self) -> JsonValue {
81        object! {}
82    }
83    /// 初始化数据
84    fn init_data(&mut self) -> JsonValue {
85        array![]
86    }
87    /// 使用工具
88    fn tools(&mut self) -> Tools {
89        let tools = PLUGIN_TOOLS.lock().unwrap();
90        let tools = tools.get("tools").unwrap().clone();
91        tools
92    }
93    /// 设置全局变量
94    fn set_global_data(&mut self, key: &str, value: JsonValue) {
95        GLOBAL_DATA.with(|data| {
96            data.borrow_mut()[key] = value;
97        });
98    }
99    /// 获取全局变量数据
100    fn get_global_data(&mut self) -> JsonValue {
101        GLOBAL_DATA.with(|data| {
102            data.borrow().clone()
103        })
104    }
105    /// 获取全局变量指定字段数据
106    fn get_global_data_key(&mut self, key: &str) -> JsonValue {
107        GLOBAL_DATA.with(|data| {
108            data.borrow()[key].clone()
109        })
110    }
111    #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
112    fn db_table(&mut self) -> Db {
113        let table = self._table_name();
114        self.tools().db.table(table).clone()
115    }
116    #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
117    fn db_find(&mut self, id: &str) -> JsonValue {
118        self.db_table().where_and("id", "=", id.into()).find()
119    }
120    #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
121    fn db_select(&mut self, ids: &str) -> JsonValue {
122        self.db_table().where_and("id", "in", ids.into()).select()
123    }
124    #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
125    fn db_insert(&mut self, data: JsonValue) -> String {
126        let res = self.db_table().insert(data);
127        res.to_string()
128    }
129    #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
130    fn db_delete(&mut self, id: &str) -> usize {
131        let delete = self.db_table().where_and("id", "=", id.into()).delete();
132        delete.as_usize().unwrap_or(0)
133    }
134    #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
135    fn db_update(&mut self, id: &str, data: JsonValue) -> usize {
136        let update = self.db_table().where_and("id", "=", id.into()).update(data);
137        update.as_usize().unwrap_or(0)
138    }
139    /// 监听
140    fn handle(&mut self) {}
141}