sea-orm-soft-delete
Soft deletes for SeaORM: trash, restore, prune, and queries that skip the trash.
Mark an entity, and its rows go to the trash instead of disappearing: post.soft_delete(db)
sets deleted_at, post.restore(db) brings the row back, old trash is pruned for good.
Think Laravel's SoftDeletes, for SeaORM.
Features
- Trash and restore —
soft_delete,restore,force_delete,is_trashedon every model of the entity - Hooks — before/after each operation, like Laravel's model events; a
before_*hook can cancel - Quiet variants —
*_quietlymethods that skip the hooks - Queries —
find_without_trashed(),find_only_trashed(),.without_trashed()on any query - Bulk and pruning —
soft_delete_many,restore_many,Prunable- andMassPrunable-style pruning - Guard — turns SeaORM's hard
delete()into an error, so data is never erased by accident - Migrations — the
deleted_atcolumn and a unique index that ignores trashed rows - Tested — against a real database (in-memory SQLite), every doc example compiled
Installation
[]
= "0.1"
Quick start
Add a nullable deleted_at column (see Migrations) and mark the entity:
use SoftDelete;
Every posts::Model now has trash operations:
use SoftDeleteModel;
let post = post.soft_delete.await?; // deleted_at = now
assert!;
let post = post.restore.await?; // deleted_at = NULL
post.force_delete.await?; // DELETE, for good
Trashing a row twice keeps its original deleted_at. If the table has an updated_at
column, set it together with deleted_at, as Laravel does:
Queries
use ;
let live = find_without_trashed.all.await?;
let trash = find_only_trashed.all.await?;
let in_category = find
.filter
.without_trashed
.all
.await?;
let by_id = find_by_id.without_trashed.one.await?;
let comments = post.find_related.without_trashed.all.await?;
Read this first. SeaORM has no global query scopes, so a plain
posts::Entity::find()still returns trashed rows. Laravel hides them automatically; here you ask for it withfind_without_trashed()or.without_trashed(). Keeping all queries for an entity in one place (a repository module) makes that easy to get right.
There is no with_trashed(): a plain query already includes trashed rows.
Hooks
Override any of them in the same impl SoftDelete block. An error from a before_* hook
cancels the operation.
| Hook | Laravel event | Runs |
|---|---|---|
before_soft_delete |
deleting |
before a row goes to the trash |
after_soft_delete |
trashed |
after it went there |
before_restore |
restoring |
before a row leaves the trash |
after_restore |
restored |
after it left |
before_force_delete |
forceDeleting |
before a row is deleted for good |
after_force_delete |
forceDeleted |
after that |
soft_delete_quietly, restore_quietly and force_delete_quietly skip the hooks. Bulk
operations and mass_prune_trashed never run them, as Laravel's mass deletes fire no events.
Pass a DatabaseTransaction as db to make a hook and its change atomic.
Guarding against hard deletes
In Laravel delete() on a soft-deleting model is soft. In SeaORM post.delete(db) removes the
row for good, and a hook cannot turn that into an update. So make it an error instead:
force_delete keeps working: it does not go through ActiveModelBehavior.
Many rows at once
let condition = all.add;
let trashed = soft_delete_many.await?; // count
let restored = restore_many.await?; // count
Pruning
Delete for good everything that has been in the trash longer than a given time, for example from a scheduled task:
use Duration;
// How many rows would go, like `model:prune --pretend`
let pending = count_prunable.await?;
// Like `Prunable`: one row at a time, with the force-delete hooks (to remove files, ...)
let removed = prune_trashed.await?;
// Like `MassPrunable`: one DELETE query, no hooks
let removed = mass_prune_trashed.await?;
Migrations
The column, a nullable timestamp with time zone:
manager
.alter_table
.await?;
A unique index that only covers rows outside the trash, so a trashed hello no longer blocks
a new hello:
manager
.create_index
.await?;
// CREATE UNIQUE INDEX "posts_slug_kept" ON "posts" ("slug") WHERE "deleted_at" IS NULL
This is cleaner than Laravel's usual unique(slug, deleted_at), which lets two live rows share
a slug because NULL never equals NULL. Partial indexes work in Postgres and SQLite; MySQL
does not support them.
Coming from Laravel
Laravel SoftDeletes |
sea-orm-soft-delete |
|---|---|
use SoftDeletes |
impl SoftDelete for Entity |
$table->softDeletesTz() |
deleted_at_column("deleted_at") |
$post->delete() |
post.soft_delete(db) |
$post->restore() |
post.restore(db) |
$post->forceDelete() |
post.force_delete(db) |
$post->trashed() |
post.is_trashed() |
deleteQuietly(), restoreQuietly() |
soft_delete_quietly, restore_quietly |
| global scope hiding trashed rows | find_without_trashed(), .without_trashed() |
withTrashed() |
plain find() |
onlyTrashed() |
find_only_trashed(), .only_trashed() |
Post::where(...)->restore() |
restore_many(db, condition) |
deleting, trashed, restoring, restored, forceDeleting, forceDeleted |
before_* / after_* hooks |
Prunable, MassPrunable, model:prune --pretend |
prune_trashed, mass_prune_trashed, count_prunable |
| — | deny_hard_delete(), unique_while_kept(...) |
Contributing
Issues and pull requests are welcome. The tests need no database server:
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.