mergeQuery
Category
Tags
Export size
min 4.56 kB · gzip 1.85 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.
Under combine, branches that constrain the same single property with an equality or an $in are collapsed into a single $in over the union of their values — the same condition, without the $or (opt out with collapseOrToIn: false).
This is well suited to merging a client-provided query with a server-side restriction inside a hook.
ts
import { mergeQuery } from 'feathers-utils/utils';Examples
Example 1
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 }] }
// an $or on one property is a $in over the union of its values
mergeQuery({ something: { $in: ['a'] } }, { something: { $in: ['b'] } })
// => { something: { $in: ['a', 'b'] } }Example 2
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
/**
* Collapse `$or` branches that constrain the same single property with an
* equality or `$in` into one `$in` over the union of their values. Only ever
* applies to `combine`, the mode that produces an `$or`. Turn this off when
* `$in` is not an option for that property. Default `true`.
*/
collapseOrToIn?: boolean
}
/**
* 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.
*
* Under `combine`, branches that constrain the same single property with an equality
* or an `$in` are collapsed into a single `$in` over the union of their values — the
* same condition, without the `$or` (opt out with `collapseOrToIn: false`).
*
* 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 }] }
*
* // an $or on one property is a $in over the union of its values
* mergeQuery({ something: { $in: ['a'] } }, { something: { $in: ['b'] } })
* // => { something: { $in: ['a', 'b'] } }
* ```
*
* @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 |
