Skip to content

Commit

Permalink
fix: sql query for multifilter (#970)
Browse files Browse the repository at this point in the history
  • Loading branch information
anddyyyy46 authored Sep 9, 2024
1 parent 189f937 commit 1883c2d
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ export function addFilter<T>(
const filter = parseFilter(query, filterableColumns)

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'))
const orFilters = filterEntries.filter(([_, value]) => value[0].comparator === '$or')
const andFilters = filterEntries.filter(([_, value]) => value[0].comparator === '$and')

qb.andWhere(
new Brackets((qb: SelectQueryBuilder<T>) => {
Expand All @@ -333,13 +333,13 @@ export function addFilter<T>(
})
)

qb.andWhere(
new Brackets((qb: SelectQueryBuilder<T>) => {
for (const [column] of andFilters) {
for (const [column] of andFilters) {
qb.andWhere(
new Brackets((qb: SelectQueryBuilder<T>) => {
addWhereCondition(qb, column, filter)
}
})
)
})
)
}

return qb
}
83 changes: 83 additions & 0 deletions src/paginate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2255,6 +2255,89 @@ describe('paginate', () => {
)
})

it('should return result based on two multifilters chained together with and operator', async () => {
const config: PaginateConfig<CatEntity> = {
sortableColumns: ['id'],
filterableColumns: {
name: true,
color: true,
},
}
const query: PaginateQuery = {
path: '',
filter: {
name: ['Milo', '$or:Garfield'],
color: ['brown', '$or:white'],
},
}
const result = await paginate<CatEntity>(query, catRepo, config)
const expected = cats.filter(
(cat) =>
(cat.name === 'Milo' || cat.name === 'Garfield') && (cat.color === 'brown' || cat.color === 'white')
)
expect(result.data).toStrictEqual(expected)
expect(result.links.current).toBe(
'?page=1&limit=20&sortBy=id:ASC&filter.name=Milo&filter.name=$or:Garfield&filter.color=brown&filter.color=$or:white'
)
})

it('should return result based on two multifilters chained together with or operator', async () => {
const config: PaginateConfig<CatEntity> = {
sortableColumns: ['id'],
filterableColumns: {
name: true,
color: true,
},
}
const query: PaginateQuery = {
path: '',
filter: {
name: ['$or:Milo', '$or:Garfield'],
color: ['$or:brown', '$or:white'],
},
}
const result = await paginate<CatEntity>(query, catRepo, config)
const expected = cats.filter(
(cat) => cat.name === 'Milo' || cat.name === 'Garfield' || cat.color === 'brown' || cat.color === 'white'
)
expect(result.data).toStrictEqual(expected)
expect(result.links.current).toBe(
'?page=1&limit=20&sortBy=id:ASC&filter.name=$or:Milo&filter.name=$or:Garfield&filter.color=$or:brown&filter.color=$or:white'
)
})

it('should return result based on filters chained together with and operators and or operators', async () => {
const config: PaginateConfig<CatEntity> = {
sortableColumns: ['id'],
filterableColumns: {
name: true,
color: true,
age: true,
cutenessLevel: true,
},
}
const query: PaginateQuery = {
path: '',
filter: {
name: ['$or:Milo', '$or:Garfield'],
age: '$or:$null',
color: ['brown', '$or:white'],
cutenessLevel: [CutenessLevel.HIGH, `$or:${CutenessLevel.LOW}`],
},
}
const result = await paginate<CatEntity>(query, catRepo, config)
const expected = cats.filter(
(cat) =>
(cat.name === 'Milo' || cat.name === 'Garfield' || !cat.age) &&
(cat.color === 'brown' || cat.color === 'white') &&
(cat.cutenessLevel === CutenessLevel.HIGH || cat.cutenessLevel === CutenessLevel.LOW)
)
expect(result.data).toStrictEqual(expected)
expect(result.links.current).toBe(
'?page=1&limit=20&sortBy=id:ASC&filter.name=$or:Milo&filter.name=$or:Garfield&filter.age=$or:$null&filter.color=brown&filter.color=$or:white&filter.cutenessLevel=high&filter.cutenessLevel=$or:low'
)
})

it("should return all columns if select doesn't contain all primary columns", async () => {
const config: PaginateConfig<CatEntity> = {
sortableColumns: ['id', 'name'],
Expand Down

0 comments on commit 1883c2d

Please sign in to comment.