Skip to content

stashable

hooks
Source Code | Documentation

Stashes the pre-mutation state of a record into context.params. Eagerly starts the fetch but exposes a memoized function — calling it multiple times only hits the database once. Use in before hooks on update, patch, or remove methods.

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

Example

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

app.service('users').hooks({
  before: { patch: [stashable()] }
})

// In a later hook (before or after):
const before = await context.params.stashed()

Type declaration

Show Type Declarations
ts
export type StashableOptions = {
  /** The property name on `context.params` to store the stash function. @default 'stashed' */
  propName?: string
  /** Custom function to fetch the pre-mutation state. Defaults to `service.get` or `service.find`. */
  stashFunc?: (context: HookContext) => Promise<any>
}
/**
 * Stashes the pre-mutation state of a record into `context.params`.
 * Eagerly starts the fetch but exposes a memoized function — calling it
 * multiple times only hits the database once.
 * Use in `before` hooks on `update`, `patch`, or `remove` methods.
 *
 * @example
 * ```ts
 *
 *
 * app.service('users').hooks({
 *   before: { patch: [stashable()] }
 * })
 *
 * // In a later hook (before or after):
 * const before = await context.params.stashed()
 * ```
 *
 * @see https://utils.feathersjs.com/hooks/stashable.html
 */
export declare function stashable<H extends HookContext = HookContext>(
  options?: StashableOptions,
): {
  (context: H, next: NextFunction): Promise<void>
  (context: H): H
}
ArgumentTypeDescription
optionsStashableOptions
typemethodsmulti
before, aroundupdate, patch, removeyes

Released under the MIT License.