Skip to main content

BaseService

Trait BaseService 

Source
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§

Source

fn db(&self) -> &DatabaseConnection

获取数据库连接

Source

fn table_name(&self) -> &str

获取表名

Provided Methods§

Source

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,

新增

Source

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,

删除

Source

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,

软删除

Source

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,

修改

Source

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,

查询单条记录

Source

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] 传入自定义过滤参数使用
Source

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_param
  • field_eq:生成 AND column = ?
  • field_like:生成 AND column LIKE ?
Source

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 之前会做字段名和关键字长度的安全检查
Source

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 查询

Source

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

Source

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,

修改前置钩子

Source

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,

修改后置钩子

Source

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".

Implementors§