fix: or filters are grouped together (#774)
This commit is contained in:
parent
5f64f1ccbf
commit
c47973d063
1
.gitignore
vendored
1
.gitignore
vendored
@ -4,3 +4,4 @@ coverage/
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
npm-debug.log
|
npm-debug.log
|
||||||
.history
|
.history
|
||||||
|
.idea/
|
||||||
|
@ -313,11 +313,26 @@ export function addFilter<T>(
|
|||||||
filterableColumns?: { [column: string]: (FilterOperator | FilterSuffix)[] | true }
|
filterableColumns?: { [column: string]: (FilterOperator | FilterSuffix)[] | true }
|
||||||
): SelectQueryBuilder<T> {
|
): SelectQueryBuilder<T> {
|
||||||
const filter = parseFilter(query, filterableColumns)
|
const filter = parseFilter(query, filterableColumns)
|
||||||
return qb.andWhere(
|
|
||||||
|
const filterEntries = Object.entries(filter)
|
||||||
|
const orFilters = filterEntries.filter(([_, value]) => value.some((v) => v.comparator === '$or'))
|
||||||
|
const andFilters = filterEntries.filter(([_, value]) => value.some((v) => v.comparator !== '$or'))
|
||||||
|
|
||||||
|
qb.andWhere(
|
||||||
new Brackets((qb: SelectQueryBuilder<T>) => {
|
new Brackets((qb: SelectQueryBuilder<T>) => {
|
||||||
for (const column in filter) {
|
for (const [column] of orFilters) {
|
||||||
addWhereCondition(qb, column, filter)
|
addWhereCondition(qb, column, filter)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
qb.andWhere(
|
||||||
|
new Brackets((qb: SelectQueryBuilder<T>) => {
|
||||||
|
for (const [column] of andFilters) {
|
||||||
|
addWhereCondition(qb, column, filter)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
return qb
|
||||||
}
|
}
|
||||||
|
@ -2129,6 +2129,33 @@ describe('paginate', () => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should return result based on two ors and an and with two cats', async () => {
|
||||||
|
const config: PaginateConfig<CatEntity> = {
|
||||||
|
sortableColumns: ['id'],
|
||||||
|
filterableColumns: {
|
||||||
|
age: [FilterOperator.BTW],
|
||||||
|
name: true,
|
||||||
|
color: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
const query: PaginateQuery = {
|
||||||
|
path: '',
|
||||||
|
filter: {
|
||||||
|
name: '$or:Milo',
|
||||||
|
color: '$or:white',
|
||||||
|
age: '$btw:1,10',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
const result = await paginate<CatEntity>(query, catRepo, config)
|
||||||
|
|
||||||
|
expect(result.data).toStrictEqual(
|
||||||
|
cats.filter((cat) => (cat.name === 'Milo' || cat.color === 'white') && cat.age)
|
||||||
|
)
|
||||||
|
expect(result.links.current).toBe(
|
||||||
|
'?page=1&limit=20&sortBy=id:ASC&filter.name=$or:Milo&filter.color=$or:white&filter.age=$btw:1,10'
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
it("should return all columns if select doesn't contain all primary columns", async () => {
|
it("should return all columns if select doesn't contain all primary columns", async () => {
|
||||||
const config: PaginateConfig<CatEntity> = {
|
const config: PaginateConfig<CatEntity> = {
|
||||||
sortableColumns: ['id', 'name'],
|
sortableColumns: ['id', 'name'],
|
||||||
|
Loading…
Reference in New Issue
Block a user