This repository has been archived by the owner on Sep 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #387 from stockpile-co/177/fix-use-of-knex-modify
177/Fix use of knex.modify in filterQuery
- Loading branch information
Showing
3 changed files
with
32 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,20 @@ | ||
/** | ||
* Filter a database query based on request query parameters | ||
* @module services/filter-query | ||
* @param {object} queryBuilder Knex query builder | ||
* @param {object} req HTTP request | ||
* @param {map} paramNames Values `req.params` to filter with | ||
* @return {object} Query builder with `where` clauses appended to it | ||
*/ | ||
module.exports = (req, paramNames = new Map()) => { | ||
return (queryBuilder) => { | ||
for (const [name, key] of paramNames) { | ||
// Get value from request query parameters | ||
const value = req.params[name] | ||
module.exports = (queryBuilder, req, paramNames = new Map()) => { | ||
for (const [name, key] of paramNames) { | ||
// Get value from request query parameters | ||
const value = req.params[name] | ||
|
||
// Add to query if value is defined | ||
if (value) { | ||
queryBuilder.where(key, value) | ||
} | ||
// Add to query if value is defined | ||
if (value) { | ||
queryBuilder.where(key, value) | ||
} | ||
return queryBuilder | ||
} | ||
return queryBuilder | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters