sea-orm-soft-delete 0.1.2

Soft deletes for SeaORM: trash, restore, prune, and queries that skip trashed rows
Documentation
use sea_orm::{ColumnTrait, QueryFilter, Select};

use crate::entity::SoftDelete;

/// Trash filters for any query on a [`SoftDelete`] entity.
///
/// ```
/// # mod posts {
/// #     use sea_orm::entity::prelude::*;
/// #     #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
/// #     #[sea_orm(table_name = "posts")]
/// #     pub struct Model {
/// #         #[sea_orm(primary_key)]
/// #         pub id: i32,
/// #         pub category_id: i32,
/// #         pub slug: String,
/// #         pub deleted_at: Option<DateTimeWithTimeZone>,
/// #     }
/// #     #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
/// #     pub enum Relation {}
/// # }
/// # impl sea_orm::ActiveModelBehavior for posts::ActiveModel {}
/// # impl sea_orm_soft_delete::SoftDelete for posts::Entity {
/// #     fn deleted_at_column() -> Self::Column { posts::Column::DeletedAt }
/// # }
/// use sea_orm::{ColumnTrait, DatabaseConnection, DbErr, EntityTrait, QueryFilter};
/// use sea_orm_soft_delete::TrashedQuery;
///
/// async fn example(db: &DatabaseConnection) -> Result<(), DbErr> {
///     let posts = posts::Entity::find()
///         .filter(posts::Column::CategoryId.eq(1))
///         .without_trashed()
///         .all(db)
///         .await?;
///     Ok(())
/// }
/// # fn main() {}
/// ```
///
/// There is no `with_trashed()`: a plain query already includes trashed rows.
pub trait TrashedQuery {
    /// Keeps only rows that are not in the trash.
    #[must_use]
    fn without_trashed(self) -> Self;

    /// Keeps only rows that are in the trash.
    #[must_use]
    fn only_trashed(self) -> Self;
}

impl<E: SoftDelete> TrashedQuery for Select<E> {
    fn without_trashed(self) -> Self {
        self.filter(E::deleted_at_column().is_null())
    }

    fn only_trashed(self) -> Self {
        self.filter(E::deleted_at_column().is_not_null())
    }
}