Skip to content

setData ​

Category
Tags
Export size
min 7.98 kB · gzip 3.41 kB

Sets a property on each item in context.data from another property on the hook context. Supports dot-notation paths for both source and target. Throws a Forbidden error if the source field is missing (unless allowUndefined is true).

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

Example ​

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

app.service('posts').hooks({
  before: { create: [setData('params.user.id', 'userId')] }
})

Options ​

allowUndefinedbooleandefaultfalse

Whether a missing from on the context is allowed. With false, an external call (one with a params.provider) that misses it is rejected — an internal call never is.

overwriteboolean | PredicateItemWithContextdefaulttrue

Whether a value already present at to is overwritten.

Pass a (item, context) => boolean predicate to decide per item — items it returns false for keep the value they came in with.

error(context: HookContext, from: PropertyPath) => FeathersError

Customize the error that is thrown if the context[from] is not available. If not provided, throws a Forbidden error with a message indicating the missing field.

Type declaration ​

Show Type Declarations
ts
export interface HookSetDataOptions {
  /**
   * Whether a missing `from` on the context is allowed. With `false`, an
   * external call (one with a `params.provider`) that misses it is rejected —
   * an internal call never is.
   *
   * @default false
   */
  allowUndefined?: boolean
  /**
   * Whether a value already present at `to` is overwritten.
   *
   * Pass a `(item, context) => boolean` predicate to decide per item — items it
   * returns `false` for keep the value they came in with.
   *
   * @default true
   */
  overwrite?: boolean | PredicateItemWithContext
  /**
   * Customize the error that is thrown if the context[from] is not available.
   * If not provided, throws a `Forbidden` error with a message indicating the missing field.
   */
  error?: (context: HookContext, from: PropertyPath) => FeathersError
}
/**
 * Sets a property on each item in `context.data` from another property on the hook context.
 * Supports dot-notation paths for both source and target. Throws a `Forbidden` error
 * if the source field is missing (unless `allowUndefined` is `true`).
 *
 * @example
 * ```ts
 *
 *
 * app.service('posts').hooks({
 *   before: { create: [setData('params.user.id', 'userId')] }
 * })
 * ```
 *
 * @see https://utils.feathersjs.com/hooks/set-data.html
 */
export declare function setData<H extends HookContext = HookContext>(
  /**
   * The property path of the context to set the value from. 'dot.notation' is supported.
   *
   * If the property does not exist, the hook will throw an error unless `allowUndefined` is set to true.
   * If the property exists, it will be set to the value of the `to` property path of the data item.
   *
   * @example 'params.user.id'
   */
  from: PropertyPath,
  /**
   * The property path of the data item to set the value to. 'dot.notation' is supported.
   *
   * If the property does not exist, it will be created.
   * If the property exists, it will be overwritten unless `overwrite` is set to false.
   *
   * @example 'userId'
   */
  to: PropertyPath,
  options?: HookSetDataOptions,
): {
  (context: H): void
  (context: H, next: NextFunction): Promise<void>
}
ArgumentTypeDescription
fromPropertyPath
toPropertyPath
optionsHookSetDataOptions
typemethodsmulti
before, aroundcreate, update, patchyes

Released under the MIT License.