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 @beforePaginate to automatically append WHERE <table>.deleted_at IS NULL to queries.
  • Query Builder Macros: Extends Lucid's ModelQueryBuilder with .withTrashed(), .onlyTrashed(), and .restore() methods.
  • Model Static Scopes: Query soft-deleted records directly from the model class via Model.withTrashed() and Model.onlyTrashed().
  • Instance Helpers: Manage individual model lifecycle with .delete() (soft delete), .restore(), .forceDelete(), and the .$trashed getter.
  • 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 ModelQueryBuilder and ModelQueryBuilderContract.

Requirements

DependencyVersion
Node.js>=24.0.0
@adonisjs/core^7.6.0
@adonisjs/lucid^22.0.0

How It Works

  1. Service Provider (LucidSoftDeletesProvider): During application boot, the provider registers the withTrashed, onlyTrashed, and restore macros onto Lucid's ModelQueryBuilder.
  2. Model Mixin (SoftDeletes): Composing a BaseModel with SoftDeletes declares the @column.dateTime() deletedAt column, overrides $getQueryFor('delete') to update deletedAt instead of issuing a DELETE statement (unless $forceDelete is 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.