Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIv3: Support filter paramters passed via filter_parameters array #8253

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
28 changes: 26 additions & 2 deletions lib/api3/generic/search/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,34 @@ function parseFilter (req, res) {
, operators = ['eq', 'ne', 'gt', 'gte', 'lt', 'lte', 'in', 'nin', 're']
;

// filter_parameters query parameter needs to be split
// into individual parameters before it can be processed
const processedQueryParams = {};

for (let param in req.query) {
if (!Object.prototype.hasOwnProperty.call(req.query, param)
|| reservedParams.includes(param)) continue;

if (param === 'filter_parameters') {
// filter_parameters are a space separated list of
// <param>$<operator>=<value>
// for the purpose of building the filter, we want
// to take each <param>$<operator> as the key for
// the processes query params map
const filterValues = req.query[param].split(' ');

for (const filterValue of filterValues) {
const filterParts = filterValue.split('=');

processedQueryParams[filterParts[0]] = filterParts[1];
}
}
else {
processedQueryParams[param] = req.query[param];
}
}

for (let param in processedQueryParams) {
let field = param
, operator = 'eq'
;
Expand All @@ -74,7 +98,7 @@ function parseFilter (req, res) {
return null;
}
}
const value = parseValue(field, req.query[param]);
const value = parseValue(field, processedQueryParams[param]);

filter.push({ field, operator, value });
}
Expand Down Expand Up @@ -137,4 +161,4 @@ module.exports = {
parseFilter,
parseSort,
parseSkip
};
};
36 changes: 19 additions & 17 deletions lib/api3/storage/mongoCollection/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function normalizeDoc (doc) {
*/
function parseFilter (filterDef, logicalOperator, onlyValid) {
let filter = { };

if (!filterDef)
return filter;

Expand All @@ -39,56 +40,57 @@ function parseFilter (filterDef, logicalOperator, onlyValid) {
for (const itemDef of filterDef) {
let item;

// Check if a query is already started for given field
if (!filter.hasOwnProperty(itemDef.field)) {
filter[itemDef.field] = {};
}

switch (itemDef.operator) {
case 'eq':
item = itemDef.value;
filter[itemDef.field]['$eq'] = itemDef.value;
break;

case 'ne':
item = { $ne: itemDef.value };
filter[itemDef.field]['$ne'] = itemDef.value;
break;

case 'gt':
item = { $gt: itemDef.value };
filter[itemDef.field]['$gt'] = itemDef.value;
break;

case 'gte':
item = { $gte: itemDef.value };
filter[itemDef.field]['$gte'] = itemDef.value;
break;

case 'lt':
item = { $lt: itemDef.value };
filter[itemDef.field]['$lt'] = itemDef.value;
break;

case 'lte':
item = { $lte: itemDef.value };
filter[itemDef.field]['$lte'] = itemDef.value;
break;

case 'in':
item = { $in: itemDef.value.toString().split('|') };
filter[itemDef.field]['$in'] = itemDef.value.toString().split('|');
break;

case 'nin':
item = { $nin: itemDef.value.toString().split('|') };
filter[itemDef.field]['$nin'] = itemDef.value.toString().split('|');
break;

case 're':
item = { $regex: itemDef.value.toString() };
filter[itemDef.field]['$regex'] = itemDef.value.toString();
break;

default:
throw new Error('Unsupported or missing filter operator ' + itemDef.operator);
}
} // switch (itemDef.operator)

if (logicalOperator === 'or') {
let clause = { };
clause[itemDef.field] = item;
clauses.push(clause);
}
else {
filter[itemDef.field] = item;
clauses.push(filter);
filter = { };
}
}
} // for (const itemDef of filterDef)

if (logicalOperator === 'or') {
filter = { $or: clauses };
Expand Down
Loading