mergeQuery
Category
Export size
min 2.89 kB · gzip 1.20 kB
See also
Properties are combined with a logical operator rather than merged at the value level, so the result is always a valid query: combine always wraps the two queries in $or (broaden — OR has no flat form), while intersect merges non-conflicting properties flat and wraps conflicts in $and (narrow). The special filters $select, $limit, $skip and $sort are merged separately. Inputs are never mutated.
This is well suited to merging a client-provided query with a server-side restriction inside a hook.
ts
import { } from 'feathers-utils/utils';Examples
ts
import { mergeQuery } from 'feathers-utils/utils'
// combine (default): the two queries always become an $or
mergeQuery({ id: 1 }, { id: 2 })
// => { $or: [{ id: 1 }, { id: 2 }] }
mergeQuery({ status: 'active' }, { authorId: 5 })
// => { $or: [{ status: 'active' }, { authorId: 5 }] }ts
// intersect: non-conflicting properties merge flat, conflicts become an $and
mergeQuery({ status: 'active' }, { authorId: 5 }, { mode: 'intersect' })
// => { status: 'active', authorId: 5 }
mergeQuery({ id: 1 }, { id: 2 }, { mode: 'intersect' })
// => { $and: [{ id: 1 }, { id: 2 }] }Type declaration
Show Type Declarations
ts
export type MergeQueryMode = "target" | "source" | "combine" | "intersect"
export interface MergeQueryOptions {
/**
* How to merge query properties that both queries constrain.
*
* - `combine` (default): broaden — the two queries always become branches of an `$or`.
* - `intersect`: narrow — non-conflicting properties merge flat, conflicts become an `$and`.
* - `target`: keep the target's value on conflict.
* - `source`: keep the source's value on conflict.
*/
mode?: MergeQueryMode
}
/**
* Properties are combined with a logical operator rather than merged at the value
* level, so the result is always a valid query: `combine` always wraps the two
* queries in `$or` (broaden — OR has no flat form), while `intersect` merges
* non-conflicting properties flat and wraps conflicts in `$and` (narrow). The
* special filters `$select`, `$limit`, `$skip` and `$sort` are merged separately.
* Inputs are never mutated.
*
* This is well suited to merging a client-provided query with a server-side
* restriction inside a hook.
*
* @param target Query to be merged into
* @param source Query to be merged from
* @param options
* @returns the merged query
*
* @example
* ```ts
*
*
* // combine (default): the two queries always become an $or
* mergeQuery({ id: 1 }, { id: 2 })
* // => { $or: [{ id: 1 }, { id: 2 }] }
*
* mergeQuery({ status: 'active' }, { authorId: 5 })
* // => { $or: [{ status: 'active' }, { authorId: 5 }] }
* ```
*
* @example
* ```ts
* // intersect: non-conflicting properties merge flat, conflicts become an $and
* mergeQuery({ status: 'active' }, { authorId: 5 }, { mode: 'intersect' })
* // => { status: 'active', authorId: 5 }
*
* mergeQuery({ id: 1 }, { id: 2 }, { mode: 'intersect' })
* // => { $and: [{ id: 1 }, { id: 2 }] }
* ```
*
* @see https://utils.feathersjs.com/utils/merge-query.html
*/
export declare function mergeQuery(
target: Query,
source: Query,
options?: MergeQueryOptions,
): Query| Argument | Type | Description |
|---|---|---|
| target | Query | |
| source | Query | |
| options | MergeQueryOptions |
