Skip to main content

Repository

Trait Repository 

Source
pub trait Repository<E>: Send + Sync {
    type Key: Clone + Debug + PartialEq + Send + Sync;

Show 16 methods // Required methods fn key_of(&self, entity: &E) -> Self::Key; fn find_by_id(&self, key: &Self::Key) -> Result<Option<E>, RepositoryError>; fn find_all(&self) -> Result<Vec<E>, RepositoryError>; fn find_by( &self, conditions: &[WhereCondition], ) -> Result<Vec<E>, RepositoryError>; fn save(&self, entity: E) -> Result<E, RepositoryError>; fn delete(&self, key: &Self::Key) -> Result<usize, RepositoryError>; fn count(&self) -> Result<u64, RepositoryError>; // Provided methods fn find_one_by( &self, conditions: &[WhereCondition], ) -> Result<Option<E>, RepositoryError> { ... } fn save_many(&self, entities: Vec<E>) -> Result<Vec<E>, RepositoryError> { ... } fn batch_update( &self, entities: Vec<E>, ) -> Result<BatchUpdateResult<E>, RepositoryError> { ... } fn delete_by( &self, conditions: &[WhereCondition], ) -> Result<usize, RepositoryError> { ... } fn count_by( &self, conditions: &[WhereCondition], ) -> Result<u64, RepositoryError> { ... } fn exists(&self, key: &Self::Key) -> Result<bool, RepositoryError> { ... } fn paginate( &self, page: u64, page_size: u64, ) -> Result<PageResult<E>, RepositoryError> where E: Clone { ... } fn paginate_by( &self, conditions: &[WhereCondition], page: u64, page_size: u64, ) -> Result<PageResult<E>, RepositoryError> where E: Clone { ... } fn find_by_with_or_filter( &self, and: &[WhereCondition], or_filter: &[WhereCondition], ) -> Result<Vec<E>, RepositoryError> where E: Clone + EntityAttributes { ... }
}
Expand description

仓储接口(generic over Model-like entity E, primary key K)

E 不必是 Model trait 实现,仅需满足存储基本要求。 这样设计允许存储任意结构(DTO、聚合、领域实体等)。

Required Associated Types§

Source

type Key: Clone + Debug + PartialEq + Send + Sync

主键类型

Required Methods§

Source

fn key_of(&self, entity: &E) -> Self::Key

提取实体的主键

Source

fn find_by_id(&self, key: &Self::Key) -> Result<Option<E>, RepositoryError>

按主键查找

Source

fn find_all(&self) -> Result<Vec<E>, RepositoryError>

查询所有

Source

fn find_by( &self, conditions: &[WhereCondition], ) -> Result<Vec<E>, RepositoryError>

按条件查询

Source

fn save(&self, entity: E) -> Result<E, RepositoryError>

保存(INSERT 或 UPDATE)

返回保存后的实体(可能是克隆,主键可能被填充)

Source

fn delete(&self, key: &Self::Key) -> Result<usize, RepositoryError>

按主键删除

Source

fn count(&self) -> Result<u64, RepositoryError>

总数

Provided Methods§

Source

fn find_one_by( &self, conditions: &[WhereCondition], ) -> Result<Option<E>, RepositoryError>

按条件查询单条

Source

fn save_many(&self, entities: Vec<E>) -> Result<Vec<E>, RepositoryError>

批量保存

Source

fn batch_update( &self, entities: Vec<E>, ) -> Result<BatchUpdateResult<E>, RepositoryError>

批量更新(S-1:SeaORM 对标短板补全)

仅更新已存在的实体(按主键匹配),不存在的实体跳过并计入 skipped。 与 save_many 的区别:save_many 是 upsert(存在则更新,不存在则插入), batch_update 是纯更新(不存在则跳过)。

默认实现为逐条调用 find_by_id + save, 具体实现可重写为真正的批量 UPDATE SQL(如 UPDATE ... SET ... WHERE id IN (...))。

§参数
  • entities:待更新的实体列表(主键必须已填充)
§返回
  • updated:成功更新的实体列表
  • skipped:主键不存在而被跳过的实体数量
Source

fn delete_by( &self, conditions: &[WhereCondition], ) -> Result<usize, RepositoryError>

按条件删除

Source

fn count_by( &self, conditions: &[WhereCondition], ) -> Result<u64, RepositoryError>

按条件计数

Source

fn exists(&self, key: &Self::Key) -> Result<bool, RepositoryError>

主键是否存在

Source

fn paginate( &self, page: u64, page_size: u64, ) -> Result<PageResult<E>, RepositoryError>
where E: Clone,

分页查询

Source

fn paginate_by( &self, conditions: &[WhereCondition], page: u64, page_size: u64, ) -> Result<PageResult<E>, RepositoryError>
where E: Clone,

按条件分页查询

Source

fn find_by_with_or_filter( &self, and: &[WhereCondition], or_filter: &[WhereCondition], ) -> Result<Vec<E>, RepositoryError>

按 AND 条件查询,并对结果应用额外的 OR 过滤组

语义:WHERE (and1 AND and2 AND ...) AND (or1_1 OR or1_2 OR ...)

用于多字段 keyword LIKE 搜索等场景(对齐 PHP ThinkPHP where('field1|field2|field3','like','%kw%') 多字段 OR LIKE 语法)。

§默认实现

基于 find_by + 内存 OR 过滤;SQL 后端实现可重写以将 OR 下推到 SQL, 避免 SELECT ... WHERE app_id=? 全表拉取后再内存过滤的性能问题。

§参数
  • and:AND 关系的条件列表(同 find_by
  • or_filter:OR 关系的条件列表,对 and 结果做二次过滤
§示例
use sz_orm_core::repository::{Repository, WhereCondition, WhereOp};
use sz_orm_core::Value;
let and = vec![
    WhereCondition::new("is_delete", WhereOp::Eq, Value::I64(0)),
    WhereCondition::new("app_id", WhereOp::Eq, Value::I64(1)),
];
let or = vec![
    WhereCondition::new("name", WhereOp::Like, Value::String("%kw%".into())),
    WhereCondition::new("addr", WhereOp::Like, Value::String("%kw%".into())),
];
let items = repo.find_by_with_or_filter(&and, &or).unwrap();

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<E, K> Repository<E> for GenericKeyRepository<E, K>
where E: Clone + Send + Sync + 'static + EntityKey<K> + EntityAttributes, K: Clone + Debug + PartialEq + Send + Sync + 'static,

Source§

type Key = K

Source§

impl<E> Repository<E> for InMemoryRepository<E>
where E: Clone + Send + Sync + 'static + EntityAttributes,

InMemoryRepository 的 EntityAttributes-based 实现