Skip to content

transformResult

hooks
Source Code | Documentation

See also: transformers utils/mutateResult

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.

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

Example

ts
import { transformResult, omit } from 'feathers-utils/transformers'

app.service('users').hooks({
  after: { all: [transformResult(omit('password'))] }
})

Type declaration

Show Type Declarations
ts
export type TransformResultOptions = {
  dispatch?: DispatchOption
}
/**
 * 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.
 *
 * @example
 * ```ts
 *
 *
 * app.service('users').hooks({
 *   after: { all: [transformResult(omit('password'))] }
 * })
 * ```
 *
 * @see https://utils.feathersjs.com/hooks/transform-result.html
 */
export declare const transformResult: <
  T = Record<string, any>,
  H extends HookContext = HookContext,
>(
  transformer: TransformerFn<T, H>,
  options?: TransformResultOptions,
) => (context: H, next?: NextFunction) => Promise<H>
ArgumentTypeDescription
transformerTransformerFn<T, H>
optionsTransformResultOptions
typemethodsmulti
before, after, aroundfind, get, create, update, patch, removeyes

transformResult is a hook that allows you to transform the result data after it has been retrieved from the database. This can be useful for modifying the structure of the result, applying transformations to the fields, or adding additional information to the result.

Async Transformations

This hook supports asynchronous transformations. You can return a promise from the hook to perform asynchronous operations before the result is processed. However, be cautious as this can lead to performance issues if the transformations are slow or if there are many records to process. When you need to call another service or perform a database query, it's usually better to create your own hook that fetches the necessary data before transformResult is called.

You can use transformResult like this:

ts
import { transformResult } from "feathers-utils/hooks";
import { getResultIsArray } from "feathers-utils/utils";

const myHook = () => async (context) => {
  const { result } = getResultIsArray(context);

  await transformResult((item) => {
    // or whatever transformation you need
    item.userEmail = users.find((u) => u.id === item.userId)?.email;
    return item;
  })(context);
};

Transformers

'feathers-utils' provides a set of transformers that can be used with this hook. These transformers can be used to trim strings, convert dates, or omit fields from the result.

TransformerDescription
lowercase

Transforms the specified fields of an item to lowercase.

omit

Omit the specified fields from an item.

parseDate

Parses the specified fields of an item into Date objects.

pick

Picks the specified fields from an item.

setNow

Sets the specified fields of an item to the current date and time.

trim

Trims the specified fields of an item.

Released under the MIT License.