Skip to content

softDelete

hooks
Source Code | Documentation

Marks items as deleted instead of physically removing them. On remove, the hook patches the record with removeData (e.g. { deletedAt: new Date() }). On all other methods, it appends deletedQuery (e.g. { deletedAt: null }) to filter out soft-deleted items.

ts
  import { 
softDelete
} from 'feathers-utils/hooks';

Example

ts
import { softDelete } from 'feathers-utils/hooks'

app.service('users').hooks({
  around: {
    all: [softDelete({ deletedQuery: { deletedAt: null }, removeData: { deletedAt: new Date() } })]
  }
})

Type declaration

Show Type Declarations
ts
export type SoftDeleteOptionFunction<H extends HookContext = HookContext> = (
  context?: H,
) => Promisable<{
  [key: string]: any
}>
export interface SoftDeleteOptions<H extends HookContext = HookContext> {
  /**
   * @example { deletedAt: null }
   */
  deletedQuery:
    | {
        [key: string]: any
      }
    | SoftDeleteOptionFunction<H>
  /**
   * @example { deletedAt: new Date() }
   */
  removeData:
    | {
        [key: string]: any
      }
    | SoftDeleteOptionFunction<H>
  /**
   * Transform the params before calling the service method. E.g. remove 'params.provider' or add custom params.
   */
  transformParams?: TransformParamsFn
  /**
   * Key in `params` to disable the soft delete functionality.
   *
   * @default 'disableSoftDelete'
   */
  disableSoftDeleteKey?: string
  /**
   * `softDelete` uses `._patch()` internally to mark items as deleted.
   *
   * If you set this option to `true`, it will use the `.patch()` method with hooks instead.
   */
  usePatchWithHooks?: boolean
}
/**
 * Marks items as deleted instead of physically removing them. On `remove`, the hook
 * patches the record with `removeData` (e.g. `{ deletedAt: new Date() }`). On all other
 * methods, it appends `deletedQuery` (e.g. `{ deletedAt: null }`) to filter out soft-deleted items.
 *
 * @example
 * ```ts
 *
 *
 * app.service('users').hooks({
 *   around: {
 *     all: [softDelete({ deletedQuery: { deletedAt: null }, removeData: { deletedAt: new Date() } })]
 *   }
 * })
 * ```
 *
 * @see https://utils.feathersjs.com/hooks/soft-delete.html
 */
export declare const softDelete: <H extends HookContext = HookContext>(
  options: SoftDeleteOptions<H>,
) => (context: H, next?: NextFunction) => Promise<any>
ArgumentTypeDescription
optionsSoftDeleteOptions<H>
typemethodsmulti
before, aroundfind, get, create, update, patch, removeyes

Released under the MIT License.