pub trait BaseService: Send + Sync {
Show 15 methods
// Required methods
fn db(&self) -> &DatabaseConnection;
fn table_name(&self) -> &str;
// Provided methods
fn add<'life0, 'async_trait>(
&'life0 self,
data: Value,
) -> Pin<Box<dyn Future<Output = CoolResult<Value>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn delete<'life0, 'async_trait>(
&'life0 self,
param: DeleteParam,
) -> Pin<Box<dyn Future<Output = CoolResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn soft_delete<'life0, 'async_trait>(
&'life0 self,
ids: Vec<Id>,
) -> Pin<Box<dyn Future<Output = CoolResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn update<'life0, 'async_trait>(
&'life0 self,
data: Value,
) -> Pin<Box<dyn Future<Output = CoolResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn info<'life0, 'async_trait>(
&'life0 self,
id: Id,
_ignore_fields: Option<Vec<String>>,
) -> Pin<Box<dyn Future<Output = CoolResult<Option<Value>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn page<'life0, 'async_trait>(
&'life0 self,
query: PageQuery,
option: QueryOption,
) -> Pin<Box<dyn Future<Output = CoolResult<PageResult<Value>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn page_with_filters<'life0, 'life1, 'async_trait>(
&'life0 self,
query: PageQuery,
filters: &'life1 Value,
option: QueryOption,
) -> Pin<Box<dyn Future<Output = CoolResult<PageResult<Value>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn list<'life0, 'async_trait>(
&'life0 self,
query: ListQuery,
option: QueryOption,
) -> Pin<Box<dyn Future<Output = CoolResult<Vec<Value>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn native_query<'life0, 'life1, 'async_trait>(
&'life0 self,
sql: &'life1 str,
params: Vec<Value>,
) -> Pin<Box<dyn Future<Output = CoolResult<Vec<Value>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn execute<'life0, 'life1, 'async_trait>(
&'life0 self,
sql: &'life1 str,
) -> Pin<Box<dyn Future<Output = CoolResult<u64>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn modify_before<'life0, 'async_trait>(
&'life0 self,
data: Value,
_modify_type: ModifyType,
) -> Pin<Box<dyn Future<Output = CoolResult<Value>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn modify_after<'life0, 'async_trait>(
&'life0 self,
_data: Value,
_modify_type: ModifyType,
) -> Pin<Box<dyn Future<Output = CoolResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn map_row(&self, row: QueryResult) -> Value { ... }
}Expand description
服务基类 trait
提供通用的 CRUD 操作
Required Methods§
Sourcefn db(&self) -> &DatabaseConnection
fn db(&self) -> &DatabaseConnection
获取数据库连接
Sourcefn table_name(&self) -> &str
fn table_name(&self) -> &str
获取表名
Provided Methods§
Sourcefn add<'life0, 'async_trait>(
&'life0 self,
data: Value,
) -> Pin<Box<dyn Future<Output = CoolResult<Value>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn add<'life0, 'async_trait>(
&'life0 self,
data: Value,
) -> Pin<Box<dyn Future<Output = CoolResult<Value>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
新增
Sourcefn delete<'life0, 'async_trait>(
&'life0 self,
param: DeleteParam,
) -> Pin<Box<dyn Future<Output = CoolResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn delete<'life0, 'async_trait>(
&'life0 self,
param: DeleteParam,
) -> Pin<Box<dyn Future<Output = CoolResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
删除
Sourcefn soft_delete<'life0, 'async_trait>(
&'life0 self,
ids: Vec<Id>,
) -> Pin<Box<dyn Future<Output = CoolResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn soft_delete<'life0, 'async_trait>(
&'life0 self,
ids: Vec<Id>,
) -> Pin<Box<dyn Future<Output = CoolResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
软删除
Sourcefn update<'life0, 'async_trait>(
&'life0 self,
data: Value,
) -> Pin<Box<dyn Future<Output = CoolResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn update<'life0, 'async_trait>(
&'life0 self,
data: Value,
) -> Pin<Box<dyn Future<Output = CoolResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
修改
Sourcefn info<'life0, 'async_trait>(
&'life0 self,
id: Id,
_ignore_fields: Option<Vec<String>>,
) -> Pin<Box<dyn Future<Output = CoolResult<Option<Value>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn info<'life0, 'async_trait>(
&'life0 self,
id: Id,
_ignore_fields: Option<Vec<String>>,
) -> Pin<Box<dyn Future<Output = CoolResult<Option<Value>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
查询单条记录
Sourcefn page<'life0, 'async_trait>(
&'life0 self,
query: PageQuery,
option: QueryOption,
) -> Pin<Box<dyn Future<Output = CoolResult<PageResult<Value>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn page<'life0, 'async_trait>(
&'life0 self,
query: PageQuery,
option: QueryOption,
) -> Pin<Box<dyn Future<Output = CoolResult<PageResult<Value>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
分页查询
说明:
- 支持基础的关键字模糊查询与排序(基于
QueryOption) - 在生成 SQL 之前会做字段名和关键字长度的安全检查,避免明显的 SQL 注入
- 更复杂的关联和条件可以通过
native_query或上层服务自定义
与 TS 版本的 QueryOp 对齐:
key_word_like_fields:关键字模糊查询order_by:排序配置field_eq/field_like:可通过 [page_with_filters] 传入自定义过滤参数使用
Sourcefn page_with_filters<'life0, 'life1, 'async_trait>(
&'life0 self,
query: PageQuery,
filters: &'life1 Value,
option: QueryOption,
) -> Pin<Box<dyn Future<Output = CoolResult<PageResult<Value>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn page_with_filters<'life0, 'life1, 'async_trait>(
&'life0 self,
query: PageQuery,
filters: &'life1 Value,
option: QueryOption,
) -> Pin<Box<dyn Future<Output = CoolResult<PageResult<Value>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
分页查询(带过滤参数)
对齐 TS 版本中 fieldEq / fieldLike 的能力:
filters为一个 JSON 对象,key 为FieldCondition.request_paramfield_eq:生成AND column = ?field_like:生成AND column LIKE ?
Sourcefn list<'life0, 'async_trait>(
&'life0 self,
query: ListQuery,
option: QueryOption,
) -> Pin<Box<dyn Future<Output = CoolResult<Vec<Value>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn list<'life0, 'async_trait>(
&'life0 self,
query: ListQuery,
option: QueryOption,
) -> Pin<Box<dyn Future<Output = CoolResult<Vec<Value>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
列表查询(不分页)
说明:
- 支持基础的关键字模糊查询与排序
- 在生成 SQL 之前会做字段名和关键字长度的安全检查
Sourcefn native_query<'life0, 'life1, 'async_trait>(
&'life0 self,
sql: &'life1 str,
params: Vec<Value>,
) -> Pin<Box<dyn Future<Output = CoolResult<Vec<Value>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn native_query<'life0, 'life1, 'async_trait>(
&'life0 self,
sql: &'life1 str,
params: Vec<Value>,
) -> Pin<Box<dyn Future<Output = CoolResult<Vec<Value>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
原生 SQL 查询
Sourcefn execute<'life0, 'life1, 'async_trait>(
&'life0 self,
sql: &'life1 str,
) -> Pin<Box<dyn Future<Output = CoolResult<u64>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn execute<'life0, 'life1, 'async_trait>(
&'life0 self,
sql: &'life1 str,
) -> Pin<Box<dyn Future<Output = CoolResult<u64>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
执行 SQL
Sourcefn modify_before<'life0, 'async_trait>(
&'life0 self,
data: Value,
_modify_type: ModifyType,
) -> Pin<Box<dyn Future<Output = CoolResult<Value>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn modify_before<'life0, 'async_trait>(
&'life0 self,
data: Value,
_modify_type: ModifyType,
) -> Pin<Box<dyn Future<Output = CoolResult<Value>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
修改前置钩子
Sourcefn modify_after<'life0, 'async_trait>(
&'life0 self,
_data: Value,
_modify_type: ModifyType,
) -> Pin<Box<dyn Future<Output = CoolResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn modify_after<'life0, 'async_trait>(
&'life0 self,
_data: Value,
_modify_type: ModifyType,
) -> Pin<Box<dyn Future<Output = CoolResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
修改后置钩子
Sourcefn map_row(&self, row: QueryResult) -> Value
fn map_row(&self, row: QueryResult) -> Value
行数据映射
说明:
- 默认实现返回调试字符串,保证兼容所有数据库类型
- 具体业务 Service 可以重写此方法,根据表结构把
QueryResult精准映射为字段级 JSON
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".