Skip to content

Commit b3352c9

Browse files
committed
Update readme with conditionMapper documentation
1 parent dd2b750 commit b3352c9

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,29 @@ const query = knexFlexFilter(
165165

166166
Set to `true` if you want to use insensitive-case searches when using `contains` or `starts_with` filters. Defaults to false.
167167

168+
### conditionMapper
169+
Useful to transform/change the default operator or condition that a
170+
particular filter is being evaluated against. `conditionMapper` receives the column name, the condition being evaluated and the default/normal value that should returned for the condition.
171+
172+
For example, here we change the `contains` condition to use json containment operator `@>`:
173+
174+
```javascript
175+
import { knexFlexFilter, CONTAINS } from 'knex-flex-filter';
176+
...
177+
178+
const opts = {
179+
conditionMapper: (column, condition, defaultValue) => {
180+
if (condition === CONTAINS) {
181+
return '@> ?';
182+
}
183+
184+
return defaultValue;
185+
}
186+
}
187+
188+
knexFlexFilter(baseQuery, where, opts).then(console.log);
189+
```
190+
168191
## Contributing
169192

170193
Make sure all the tests pass before sending a PR. To run the test suite, run `yarn test`. Please note that the codebase is using `dotenv` package to connect to a test db, so, to connect to your own, add a `.env` file inside the `tests` folder with the following structure:

tests/knex-flex-filter.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require('./helpers/database');
77

88

99
describe('knex-flex-filter', () => {
10-
let castFn;
10+
let castFn;
1111

1212
beforeEach(async (done) => {
1313
await seedsFn(knex);

0 commit comments

Comments
 (0)