Getting Started
Introduction
Provider-based soft deletes for AdonisJS with full Lucid ORM integration.
@eienjs/adonisjs-soft-deletes provides seamless soft delete support for AdonisJS v7 and Lucid ORM v22+. Instead of permanently removing rows from your database when calling .delete(), models composed with the SoftDeletes mixin automatically set a deleted_at timestamp and filter out soft-deleted rows across queries, pagination, and relationships.
Key Features
- Automatic Query Filtering: Hooks into
@beforeFind,@beforeFetch, and@beforePaginateto automatically appendWHERE <table>.deleted_at IS NULLto queries. - Query Builder Macros: Extends Lucid's
ModelQueryBuilderwith.withTrashed(),.onlyTrashed(), and.restore()methods. - Model Static Scopes: Query soft-deleted records directly from the model class via
Model.withTrashed()andModel.onlyTrashed(). - Instance Helpers: Manage individual model lifecycle with
.delete()(soft delete),.restore(),.forceDelete(), and the.$trashedgetter. - Relationship & Group Limit Aware: Automatically resolves underlying tables on complex Lucid relationship queries and group limit preloads.
- Type-Safe by Design: Ships with full TypeScript declarations and module augmentations for
ModelQueryBuilderandModelQueryBuilderContract.
Requirements
| Dependency | Version |
|---|---|
| Node.js | >=24.0.0 |
@adonisjs/core | ^7.6.0 |
@adonisjs/lucid | ^22.0.0 |
How It Works
- Service Provider (
LucidSoftDeletesProvider): During application boot, the provider registers thewithTrashed,onlyTrashed, andrestoremacros onto Lucid'sModelQueryBuilder. - Model Mixin (
SoftDeletes): Composing aBaseModelwithSoftDeletesdeclares the@column.dateTime() deletedAtcolumn, overrides$getQueryFor('delete')to updatedeletedAtinstead of issuing aDELETEstatement (unless$forceDeleteis enabled), and registers model hooks to exclude soft-deleted rows by default.
Ready to add soft deletes to your project? Head over to the Installation guide.
