Skip to content

defaults

transformers
Source Code | Documentation

Sets default values on an item for fields that are undefined. Values can be static or functions that return a value. Supports dot.notation for nested fields.

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

Example

ts
import { transformData, defaults } from 'feathers-utils/transformers'

transformData(item => defaults(item, { role: 'user', createdAt: () => new Date() }))

Hooks for transformers

HookDescription
transformData

Transforms each item in context.data using the provided transformer function. The transformer receives each item and can mutate it in place or return a new object. Commonly used with built-in transformers like lowercase, trim, or setNow.

transformQuery

Transforms context.params.query using the provided transformer function. The transformer receives the current query and can return a modified version. Useful for normalizing, sanitizing, or enriching queries before they hit the database.

transformResult

Transforms each item in context.result using the provided transformer function. The transformer receives each item and can mutate it in place or return a new object. Optionally operates on context.dispatch via the dispatch option.

Utilities for transformers

UtilityDescription
mutateData

Applies a transformer function to each item in context.data, updating it in place. If the transformer returns a new object, it replaces the original item. Correctly handles both single-item and array data, preserving the original shape.

mutateResult

Applies a transformer function to each item in context.result (and optionally context.dispatch). Handles paginated results, single items, and arrays transparently. Use the dispatch option to control whether result, dispatch, or both are transformed.

Type declaration

Show Type Declarations
ts
/**
 * Sets default values on an item for fields that are `undefined`.
 * Values can be static or functions that return a value.
 * Supports dot.notation for nested fields.
 *
 * @example
 * ```ts
 *
 *
 * transformData(item => defaults(item, { role: 'user', createdAt: () => new Date() }))
 * ```
 *
 * @see https://utils.feathersjs.com/transformers/defaults.html
 */
export declare function defaults<T extends Record<string, any>>(
  item: T,
  defaultValues: DefaultsInput<NoInfer<T>>,
): void
ArgumentTypeDescription
itemT
defaultValuesDefaultsInput<NoInfer<T>>

Released under the MIT License.