Skip to content

Commit

Permalink
Merge d1a678f into d0a0e2b
Browse files Browse the repository at this point in the history
  • Loading branch information
Nataniel López committed Feb 20, 2020
2 parents d0a0e2b + d1a678f commit 50312ed
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 44 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Multi filtering issues with AND and OR operations

## [1.0.0] - 2019-12-06
### Added
- Solr DB Driver Package
Expand Down
55 changes: 25 additions & 30 deletions lib/helpers/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ class Filters {

const filtersGroup = this._parseFilterGroup(filters, modelFields);

const filtersByType = this._formatByType(filtersGroup);

return Object.values(filtersByType);
return this._formatByType(filtersGroup);
}

static _parseFilterGroup(filterGroup, modelFields) {
Expand All @@ -44,58 +42,55 @@ class Filters {

const filterValues = this._getFilterObjects(filterData, modelField);

parsedFilterGroup[filterKey] = filterKey in parsedFilterGroup ? [...parsedFilterGroup[filterKey], ...filterValues] : filterValues;
parsedFilterGroup.push({ field: filterKey, ...filterValues });

return parsedFilterGroup;

}, {});
}, []);
}

static _getFilterObjects(filterData, modelField) {

if(!Array.isArray(filterData))
filterData = [filterData];

return filterData.map(filterObject => {

if(!isObject(filterObject))
filterObject = { value: filterObject };
if(!isObject(filterData))
filterData = { value: filterData };

const { value } = filterObject;
const { value } = filterData;

if(isObject(value) || Array.isArray(value))
throw new SolrError('Invalid filters: JSON/Array filters are not supported by Solr.', SolrError.codes.UNSUPPORTED_FILTER);
const type = filterData.type || modelField.type || DEFAULT_FILTER_TYPE;

const type = filterObject.type || modelField.type || DEFAULT_FILTER_TYPE;
if(!value)
throw new SolrError(`Invalid filters: Missing value for filter type ${type}.`, SolrError.codes.INVALID_FILTER_VALUE);

if(!value)
throw new SolrError(`Invalid filters: Missing value for filter type ${type}.`, SolrError.codes.INVALID_FILTER_VALUE);

return { type, value };
});
return { type, value };
}

static _formatByType(filtersGroup) {

return Object.entries(filtersGroup).reduce((filtersByType, [field, filterData]) => {
return filtersGroup.reduce((filtersByType, { field, ...filterData }) => {

filtersByType[field] = this._getFilterByType(field, filterData);
filtersByType.push(this._getFilterByType(field, filterData));

return filtersByType;

}, {});
}, []);
}

static _getFilterByType(field, filterData) {
static _getFilterByType(field, { type, value }) {

const formatter = this.formatters[type];

if(!formatter)
throw new SolrError(`'${type}' is not a valid or supported filter type.`, SolrError.codes.INVALID_FILTER_TYPE);

return filterData.reduce((filters, { type, value }) => {
if(!Array.isArray(value))
value = [value];

const formatter = this.formatters[type];
return value.reduce((filters, filter) => {

if(!formatter)
throw new SolrError(`'${type}' is not a valid or supported filter type.`, SolrError.codes.INVALID_FILTER_TYPE);
if(Array.isArray(filter))
throw new SolrError('Invalid filters: Array filtering are not supported by Solr.', SolrError.codes.UNSUPPORTED_FILTER);

const formattedFilter = formatter(field, value);
const formattedFilter = isObject(filter) ? this._getFilterByType(field, filter) : formatter(field, filter);

return filters ? `${filters} OR ${formattedFilter}` : formattedFilter;

Expand Down
21 changes: 7 additions & 14 deletions tests/helpers/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,22 @@ describe('Helpers', () => {
assert.deepStrictEqual(filters, [
'equalDefault:"something"',
'customDefault:[32 TO *]',
'equal:"something" OR -equal:"foobar"',
'equal:"something"',
'-notEqual:"something"',
'greater:{10 TO *}',
'greaterOrEqual:[10 TO *]',
'lesser:{* TO 10}',
'lesserOrEqual:[* TO 10]',
'multiFilters:"some" OR multiFilters:"other" OR multiFilters:{5 TO *}'
'multiFilters:"some" OR multiFilters:"other" OR multiFilters:{5 TO *}',
'-equal:"foobar"'
]);
});

[
it('Should throw when the received filters is not supported', () => {

{ field: { type: 'equal', value: ['array'] } },
{ field: { type: 'equal', value: { an: 'object' } } }

].forEach(filters => {

it('Should throw when the received filters is not supported', () => {

assert.throws(() => Filters.build(filters), {
name: 'SolrError',
code: SolrError.codes.UNSUPPORTED_FILTER
});
assert.throws(() => Filters.build({ field: { type: 'equal', value: ['somevalue', ['array']] } }), {
name: 'SolrError',
code: SolrError.codes.UNSUPPORTED_FILTER
});
});

Expand Down

0 comments on commit 50312ed

Please sign in to comment.