transformQuery
Source Code | DocumentationSee 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.
import { transformQuery } from 'feathers-utils/hooks';Example
import { transformQuery } from 'feathers-utils/transformers'
app.service('users').hooks({
before: { find: [transformQuery((query) => ({ ...query, active: true }))] }
})Type declaration
Show Type Declarations
/**
* 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| Argument | Type | Description |
|---|---|---|
| transformer | TransformerFn<Q, H> |
| type | methods | multi |
|---|---|---|
| before, after, around | all | yes |
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.
| Transformer | Description |
|---|---|
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. |
