Skip to content

stashable ​

Category
Tags
Export size
min 6.29 kB · gzip 2.57 kB
See also

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 { 
stashable
} 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()

Options ​

propNamestringdefault'stashed'

The property name on context.params to store the stash function.

stashFunc(context: HookContext) => Promise<any>

Custom function to fetch the pre-mutation state. Defaults to service.get or service.find.

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()
 * ```
 *
 * @deprecated Use {@link defineStash} instead. `stashable` starts the fetch
 * without waiting for it, so the call can overtake a fetch with slow hooks.
 *
 * @see https://utils.feathersjs.com/hooks/stashable.html
 */
export declare function stashable<H extends HookContext = HookContext>(
  options?: StashableOptions,
): {
  (context: H): void
  (context: H, next: NextFunction): Promise<void>
}
ArgumentTypeDescription
optionsStashableOptions
typemethodsmulti
before, aroundupdate, patch, removeyes

Deprecated

Use defineStash instead. stashable starts the fetch without waiting for it: if get or find have slow hooks, the call can overtake the fetch, and stashed() returns the items after the call. defineStash waits for the fetch and pairs the items before and after the call.

Released under the MIT License.