Skip to content

softDelete ​

Category
Tags
Export size
min 7.81 kB · gzip 3.21 kB

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() } })]
  }
})

Options ​

deletedQuery{[key: string]: any} | SoftDeleteOptionFunction<H>required

The query that filters out soft-deleted items. It is appended to params.query on every method but remove.

A function receives the hook context, for a query that depends on the request, and may return a promise.

Example

removeData{[key: string]: any} | SoftDeleteOptionFunction<H>required

The data a remove patches the records with instead of deleting them.

A function receives the hook context and may return a promise — which is what you want for a timestamp, so it is taken per request rather than once when the hook is created.

Example

transformParamsTransformParamsFn

Transform the params before calling the service method. E.g. remove 'params.provider' or add custom params.

disableSoftDeleteKeystringdefault'disableSoftDelete'

Key in params to disable the soft delete functionality.

usePatchWithHooksboolean

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.

allowQueryOverrideboolean | PredicateFn<H>defaulttrue

Whether the caller may override the deletedQuery filter. Can be a boolean or a predicate that receives the HookContext.

By default, if the incoming params.query already references a key of deletedQuery (e.g. deletedAt) — including nested inside $and/$or/$nor — the deletedQuery filter is NOT added, letting the caller read soft-deleted items while remove still soft-deletes them. That includes external callers — pass isProvider('server') to allow it for internal calls only.

Set this to false to always enforce the deletedQuery filter.

Example

isProvider('server')

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> {
  /**
   * The query that filters out soft-deleted items. It is appended to
   * `params.query` on every method but `remove`.
   *
   * A function receives the hook context, for a query that depends on the
   * request, and may return a promise.
   *
   * @example { deletedAt: null }
   */
  deletedQuery:
    | {
        [key: string]: any
      }
    | SoftDeleteOptionFunction<H>
  /**
   * The data a `remove` patches the records with instead of deleting them.
   *
   * A function receives the hook context and may return a promise — which is
   * what you want for a timestamp, so it is taken per request rather than once
   * when the hook is created.
   *
   * @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
  /**
   * Whether the caller may override the `deletedQuery` filter. Can be a boolean
   * or a predicate that receives the `HookContext`.
   *
   * By default, if the incoming `params.query` already references a key of
   * `deletedQuery` (e.g. `deletedAt`) — including nested inside `$and`/`$or`/`$nor` —
   * the `deletedQuery` filter is NOT added, letting the caller read soft-deleted
   * items while `remove` still soft-deletes them. That includes external
   * callers — pass `isProvider('server')` to allow it for internal calls only.
   *
   * Set this to `false` to always enforce the `deletedQuery` filter.
   *
   * @example isProvider('server')
   * @default true
   */
  allowQueryOverride?: boolean | PredicateFn<H>
}
/**
 * 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<void>
ArgumentTypeDescription
optionsSoftDeleteOptions<H>
typemethodsmulti
before, aroundfind, get, create, update, patch, removeyes

Released under the MIT License.