Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class Builder {
request.not(queryFilter.columnName, queryFilter.operator, queryFilter.criteria)
break

case 'or':
request.or(queryFilter.filters)
break

case 'match':
request.match(queryFilter.query)
break
Expand Down Expand Up @@ -97,6 +101,15 @@ class Builder {
return this
}

or(filters) {
this.queryFilters.push({
filter: 'or',
filters,
})

return this
}

match(query) {
this.queryFilters.push({
filter: 'match',
Expand Down
1 change: 1 addition & 0 deletions src/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ const filters = [
'nxr',
'nxl',
'adj',
'or',
]
filters.forEach(
(filter) =>
Expand Down
16 changes: 16 additions & 0 deletions src/utils/Filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,19 @@ export function _nxr(columnName, filterRange) {
export function _adj(columnName, filterRange) {
return `${columnName}=adj.(${filterRange.join(',')})`
}

/**
* Finds all rows that satisfy at least one of the specified `filters`.
* @param {string} filters Filters to satisfy
* @name or
* @function
* @returns {string}
*
* @example
* _or('id.gt.20,and(name.eq.New Zealand,name.eq.France)')
* //=>
* 'or=(id.gt.20,and(name.eq.New Zealand,name.eq.France))'
*/
export function _or(filters) {
return `or=(${filters})`
}
4 changes: 4 additions & 0 deletions test/integration/Filters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ describe('Filters', () => {
'population_range=nxl.(100,500)',
'population_range=nxr.(100,500)',
'population_range=adj.(100,500)',
'or=(id.gt.20,and(name.eq.New Zealand,name.eq.France))',
]

it('should be able to take in filters before an actual request is made', async () => {
Expand Down Expand Up @@ -240,11 +241,13 @@ describe('Filters', () => {
.nxl('population_range', [100, 500])
.nxr('population_range', [100, 500])
.adj('population_range', [100, 500])
.or('id.gt.20,and(name.eq.New Zealand,name.eq.France)')
.select('*')

assert.deepEqual(response._query, expectedQueryArray)
})

// FIXME: This is still before a request is made.
it('should be able to take in filters after an actual request is made', async () => {
const client = new PostgrestClient(rootUrl)
const response = client
Expand Down Expand Up @@ -273,6 +276,7 @@ describe('Filters', () => {
.nxl('population_range', [100, 500])
.nxr('population_range', [100, 500])
.adj('population_range', [100, 500])
.or('id.gt.20,and(name.eq.New Zealand,name.eq.France)')

assert.deepEqual(response._query, expectedQueryArray)
})
Expand Down