npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@codenameryuu/adonis-lucid-soft-deletes

v2.0.0

Published

Addon for soft deletes Adonis JS with Lucid ORM

Readme

@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'

export default class Product extends compose(BaseModel, 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
  }
}

Set custom column name for deletedAt

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

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

  @column.dateTime({ columnName: 'customDeletedAtColumn' })
  declare deletedAt: DateTime | null
}

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.