Skip to content

transformQuery

hooks
Source Code | Documentation

See also: transformers

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.

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

Example

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

app.service('users').hooks({
  before: { find: [transformQuery((query) => ({ ...query, active: true }))] }
})

Type declaration

Show Type Declarations
ts
/**
 * 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.
 *
 * @example
 * ```ts
 *
 *
 * app.service('users').hooks({
 *   before: { find: [transformQuery((query) => ({ ...query, active: true }))] }
 * })
 * ```
 *
 * @see https://utils.feathersjs.com/hooks/transform-query.html
 */
export declare const transformQuery: <
  Q extends Query,
  H extends HookContext = HookContext,
>(
  transformer: TransformerFn<Q, H>,
) => (context: H, next?: NextFunction) => any
ArgumentTypeDescription
transformerTransformerFn<Q, H>
typemethodsmulti
before, after, aroundallyes

transformQuery is a hook that allows you to transform the query parameters before they are sent to the database. This can be useful for modifying the query structure, adding default values, or applying transformations to the query fields.

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 query.

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.