feat: contains operator (#536)

This commit is contained in:
zkldi 2023-03-15 10:20:00 +00:00 committed by GitHub
parent 7d6b964c4b
commit 7f0beeb5a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 5 deletions

View File

@ -14,7 +14,7 @@ Pagination and filtering helper method for TypeORM repositories or query builder
- Sort by multiple columns
- Search across 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
- 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.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 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`.

View File

@ -1,16 +1,17 @@
import { values } from 'lodash'
import {
ArrayContains,
Between,
Brackets,
Equal,
FindOperator,
ILike,
In,
MoreThan,
MoreThanOrEqual,
IsNull,
LessThan,
LessThanOrEqual,
Between,
ILike,
MoreThan,
MoreThanOrEqual,
Not,
SelectQueryBuilder,
} from 'typeorm'
@ -35,6 +36,7 @@ export enum FilterOperator {
BTW = '$btw',
ILIKE = '$ilike',
SW = '$sw',
CONTAINS = '$contains',
}
export function isOperator(value: unknown): value is FilterOperator {
@ -73,6 +75,7 @@ export const OperatorSymbolToFunction = new Map<
[FilterOperator.ILIKE, ILike],
[FilterSuffix.NOT, Not],
[FilterOperator.SW, ILike],
[FilterOperator.CONTAINS, ArrayContains],
])
type Filter = { comparator: FilterComparator; findOperator: FindOperator<string> }
@ -264,6 +267,7 @@ export function parseFilter(
params.findOperator = OperatorSymbolToFunction.get(token.operator)(...token.value.split(','))
break
case FilterOperator.IN:
case FilterOperator.CONTAINS: // <- IN and CONTAINS are identically handled.
params.findOperator = OperatorSymbolToFunction.get(token.operator)(token.value.split(','))
break
case FilterOperator.ILIKE: