@codenameryuu/adonis-lucid-soft-deletes

中文文档
2.0.4发布于 2026/10/04MITES Module零依赖近 30 天下载 1897

Addon for soft deletes Adonis JS with Lucid ORM

原始使用文档
文档目录9 节
  1. Requirement
  2. Installation
  3. Applying Soft Deletes to a Model
  4. Restoring Soft Deleted Models
  5. Permanently Deleting Models
  6. Including Soft Deleted Models
  7. Retrieving only Soft Deleted Models
  8. Soft Deletes methods
  9. License

@codenameryuu/adonis-lucid-soft-deletes

This addon adds the functionality to soft deletes Lucid Models through the deleted_at flag in AdonisJS.

Requirement

  • Adonis Js 7
  • Lucid 22 or higher

Installation

  • Install the package
yarn add @codenameryuu/adonis-lucid-soft-deletes
  • Configure the package
node ace configure @codenameryuu/adonis-lucid-soft-deletes
  • Make sure to register the provider inside adonisrc.ts file.
providers: [
  // ...
  () => import('@codenameryuu/adonis-lucid-soft-deletes/provider'),
],

You should add the deleted_at column to your database tables for models with soft deletes.

// migrations/1234566666_users.ts
import { BaseSchema } from '@adonisjs/lucid/schema'

export default class extends BaseSchema {
  protected tableName = 'products'

  async up() {
    this.schema.createTable(this.tableName, (table) => {
      // ...
      table.timestamp('deleted_at').nullable()
    })
  }
  // ...
}

Applying Soft Deletes to a Model

import { compose } from '@adonisjs/core/helpers'
import { SoftDeletes } from '@codenameryuu/adonis-lucid-soft-deletes'

import { ProductSchema } from "#database/schema";

export default class Product extends compose(ProductSchema, SoftDeletes) {
  // ...columns and props
}

Now, when you call the .delete() method on the model, the deleted_at ( customDeletedAtColumn ) column will be set to the current date and time. However, the model's database record will be left in the table.

import type { HttpContext } from '@adonisjs/core/http'
import Product from '#models/product'

export default class ProductsController {
  /**
   * Delete product by id
   * DELETE /products/:id
   */
  async destroy({ request, response }: HttpContext) {
    const product = await Product.findOrFail(request.input('id'))
    await product.delete()

    return product // or response.noContent()
  }
}

:boom: Soft delete only works for model instances. await Product.query().delete() as before will delete models from database

:point_right: When querying a model that uses soft deletes, the soft deleted models will automatically be excluded from all query results.

To determine if a given model instance has been soft deleted, you may use the .trashed getter:

import type { HttpContext } from '@adonisjs/core/http'
import Product from '#models/product'

export default class ProductsController {
  /**
   * Get product by id
   * GET /products/:id
   */
  async show({ request, response }: HttpContext) {
    const product = await Product.withTrashed().where('id', request.input('id')).firstOrFail()
    if (product.trashed) {
      return response.forbidden()
    }
    return product
  }
}

Restoring Soft Deleted Models

To restore a soft deleted model, you may call the .restore() method on a model instance. Also, method .restore() exists after methods .withTrashed() and .onlyTrashed()

The restore method will set the model's deleted_at column to null :

import type { HttpContext } from '@adonisjs/core/http'
import Product from '#models/product'

export default class TrashProductsController {
  /**
   * Update trashed product by id
   * PUT /trash/products/:id
   */
  async update({ request }: HttpContext) {
    const product = await Product.withTrashed().where('id', request.input('id')).firstOrFail()
    await product.restore()

    return product

    // or

    await Product.withTrashed().where('id', request.input('id')).restore()
    await Product.query().withTrashed().where('id', request.input('id')).restore()
  }
}

Permanently Deleting Models

Sometimes you may need to truly remove a model from your database. You may use the .forceDelete() method to permanently remove a soft deleted model from the database table:

import type { HttpContext } from '@adonisjs/core/http'
import Product from '#models/product'

export default class ProductsController {
  /**
   * Delete product by id
   * DELETE /products/:id
   */
  async destroy({ request, response }: HttpContext) {
    const product = await Product.findOrFail(request.input('id'))
    await product.forceDelete()

    return response.noContent()
  }
}

Including Soft Deleted Models

As noted above, soft deleted models will automatically be excluded from query results. However, you may force soft deleted models to be included in a query's results by calling the .withTrashed() method on the model:

import type { HttpContext } from '@adonisjs/core/http'
import Product from '#models/product'

export default class ProductsController {
  /**
   * Get a list products
   * GET /products?withTrashed=1
   */
  async index({ request }: HttpContext) {
    return Product.query()
      .if(request.input('withTrashed'), (query) => {
        query.withTrashed()
      })
      .paginate(request.input('page', 1), request.input('perPage', 10))
  }
}

Retrieving only Soft Deleted Models

The .onlyTrashed() method will retrieve only soft deleted models:

import type { HttpContext } from '@adonisjs/core/http'
import Product from '#models/product'

export default class TrashProductsController {
  /**
   * Get a list trashed products
   * GET /trash/products
   */
  async index({ request }: HttpContext) {
    return Product.onlyTrashed().paginate(request.input('page', 1), request.input('perPage', 10))
  }
}

Soft Deletes methods

Methods .withTrashed() , .onlyTrashed() and .restore() also available in ModelQueryBuilder for models with soft delete, example:

await Product.query().withTrashed().paginate(request.input('page', 1), request.input('perPage', 10))
await Product.query().onlyTrashed().paginate(request.input('page', 1), request.input('perPage', 10))

License

This package is open-sourced software licensed under the MIT license.

版本与发布记录

@codenameryuu/adonis-lucid-soft-deletes 共发布 14 个版本,首次发布于 2026/03/25;过去 12 个月发布了 14 个版本。

版本发布日期距今
2.0.4latest2026/10/041 个月前
2.0.32026/06/294 个月前
2.0.22026/06/294 个月前
2.0.12026/06/284 个月前
2.0.02026/03/317 个月前
1.9.02026/03/317 个月前

关于 @codenameryuu/adonis-lucid-soft-deletes 的常见问题

@codenameryuu/adonis-lucid-soft-deletes 是什么?

Addon for soft deletes Adonis JS with Lucid ORM

如何安装 @codenameryuu/adonis-lucid-soft-deletes?

在项目目录运行 npm install @codenameryuu/adonis-lucid-soft-deletes,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@adonisjs/core(^7.0.0)、@adonisjs/lucid(^22.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@codenameryuu/adonis-lucid-soft-deletes 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/codenameryuu__adonis-lucid-soft-deletes 是否存在。

@codenameryuu/adonis-lucid-soft-deletes 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@codenameryuu/adonis-lucid-soft-deletes 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@codenameryuu/adonis-lucid-soft-deletes 可以商用吗?

@codenameryuu/adonis-lucid-soft-deletes 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@codenameryuu/adonis-lucid-soft-deletes 的下载量有多少?

根据 npm 官方下载统计,@codenameryuu/adonis-lucid-soft-deletes 在 2026/10/10 – 2026/11/08 这 30 天内被下载 1,897 次,日均约 63 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@codenameryuu/adonis-lucid-soft-deletes 还在维护吗?

最新版本 2.0.4 发布于 2026/10/04(1 个月前);至今共发布 14 个版本,其中过去 12 个月发布了 14 个;首次发布于 2026/03/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。