feat: contains operator (#536)
This commit is contained in:
parent
7d6b964c4b
commit
7f0beeb5a7
@ -14,7 +14,7 @@ Pagination and filtering helper method for TypeORM repositories or query builder
|
|||||||
- Sort by multiple columns
|
- Sort by multiple columns
|
||||||
- Search across columns
|
- Search across columns
|
||||||
- Select columns
|
- Select columns
|
||||||
- Filter using operators (`$eq`, `$not`, `$null`, `$in`, `$gt`, `$gte`, `$lt`, `$lte`, `$btw`, `$ilike`, `$sw`)
|
- Filter using operators (`$eq`, `$not`, `$null`, `$in`, `$gt`, `$gte`, `$lt`, `$lte`, `$btw`, `$ilike`, `$sw`, `$contains`)
|
||||||
- Include relations and nested relations
|
- Include relations and nested relations
|
||||||
- Virtual column support
|
- Virtual column support
|
||||||
|
|
||||||
@ -406,6 +406,10 @@ Filter operators must be whitelisted per column in `PaginateConfig`.
|
|||||||
|
|
||||||
`?filter.createdAt=$btw:2022-02-02,2022-02-10` where column `createdAt` is between the dates `2022-02-02` and `2022-02-10`
|
`?filter.createdAt=$btw:2022-02-02,2022-02-10` where column `createdAt` is between the dates `2022-02-02` and `2022-02-10`
|
||||||
|
|
||||||
|
`?filter.roles=$contains:Moderator` where column `roles` is an array and contains the value "Moderator".
|
||||||
|
|
||||||
|
`?filter.roles=$contains:Moderator,Admin` where column `roles` is an array and contains the values "Moderator" and "Admin".
|
||||||
|
|
||||||
## Multi Filters
|
## Multi Filters
|
||||||
|
|
||||||
Multi filters are filters that can be applied to a single column with a comparator. As for single filters, multi filters must be whitelisted per column in `PaginateConfig`.
|
Multi filters are filters that can be applied to a single column with a comparator. As for single filters, multi filters must be whitelisted per column in `PaginateConfig`.
|
||||||
|
@ -1,16 +1,17 @@
|
|||||||
import { values } from 'lodash'
|
import { values } from 'lodash'
|
||||||
import {
|
import {
|
||||||
|
ArrayContains,
|
||||||
|
Between,
|
||||||
Brackets,
|
Brackets,
|
||||||
Equal,
|
Equal,
|
||||||
FindOperator,
|
FindOperator,
|
||||||
|
ILike,
|
||||||
In,
|
In,
|
||||||
MoreThan,
|
|
||||||
MoreThanOrEqual,
|
|
||||||
IsNull,
|
IsNull,
|
||||||
LessThan,
|
LessThan,
|
||||||
LessThanOrEqual,
|
LessThanOrEqual,
|
||||||
Between,
|
MoreThan,
|
||||||
ILike,
|
MoreThanOrEqual,
|
||||||
Not,
|
Not,
|
||||||
SelectQueryBuilder,
|
SelectQueryBuilder,
|
||||||
} from 'typeorm'
|
} from 'typeorm'
|
||||||
@ -35,6 +36,7 @@ export enum FilterOperator {
|
|||||||
BTW = '$btw',
|
BTW = '$btw',
|
||||||
ILIKE = '$ilike',
|
ILIKE = '$ilike',
|
||||||
SW = '$sw',
|
SW = '$sw',
|
||||||
|
CONTAINS = '$contains',
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isOperator(value: unknown): value is FilterOperator {
|
export function isOperator(value: unknown): value is FilterOperator {
|
||||||
@ -73,6 +75,7 @@ export const OperatorSymbolToFunction = new Map<
|
|||||||
[FilterOperator.ILIKE, ILike],
|
[FilterOperator.ILIKE, ILike],
|
||||||
[FilterSuffix.NOT, Not],
|
[FilterSuffix.NOT, Not],
|
||||||
[FilterOperator.SW, ILike],
|
[FilterOperator.SW, ILike],
|
||||||
|
[FilterOperator.CONTAINS, ArrayContains],
|
||||||
])
|
])
|
||||||
|
|
||||||
type Filter = { comparator: FilterComparator; findOperator: FindOperator<string> }
|
type Filter = { comparator: FilterComparator; findOperator: FindOperator<string> }
|
||||||
@ -264,6 +267,7 @@ export function parseFilter(
|
|||||||
params.findOperator = OperatorSymbolToFunction.get(token.operator)(...token.value.split(','))
|
params.findOperator = OperatorSymbolToFunction.get(token.operator)(...token.value.split(','))
|
||||||
break
|
break
|
||||||
case FilterOperator.IN:
|
case FilterOperator.IN:
|
||||||
|
case FilterOperator.CONTAINS: // <- IN and CONTAINS are identically handled.
|
||||||
params.findOperator = OperatorSymbolToFunction.get(token.operator)(token.value.split(','))
|
params.findOperator = OperatorSymbolToFunction.get(token.operator)(token.value.split(','))
|
||||||
break
|
break
|
||||||
case FilterOperator.ILIKE:
|
case FilterOperator.ILIKE:
|
||||||
|
Loading…
Reference in New Issue
Block a user