Skip to content

Commit

Permalink
ResourceFilter: add support for "exists"
Browse files Browse the repository at this point in the history
ResourceFilter now supports the "exists"
comparison operator to be able to test if a field
exists or not.
  • Loading branch information
maxime-beguin committed Oct 29, 2021
1 parent b37f213 commit 291c151
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- require('@openveo/api').fileSystem.getFileType has been added to be able to get the type of a file (only for supported files)
- require('@openveo/api').fileSystem.extract is now capable of extracting zip files
- require('@openveo/api').fileSystem now supports zip files
- require('@openveo/api').storages.ResourceFilter now supports `exists` comparison operator to test if a field exists or not

## BUG FIXES

Expand Down
20 changes: 20 additions & 0 deletions lib/storages/ResourceFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @example
* var filter = new ResourceFilter()
* .equal('field1', 42)
* .exists('field1', true)
* .notEqual('field2', 42)
* .greaterThan('field3', 42)
* .greaterThanEqual('field4', 42)
Expand Down Expand Up @@ -75,6 +76,7 @@ ResourceFilter.OPERATORS = {
NOR: 'nor',
AND: 'and',
EQUAL: 'equal',
EXISTS: 'exists',
NOT_EQUAL: 'notEqual',
IN: 'in',
NOT_IN: 'notIn',
Expand Down Expand Up @@ -191,6 +193,24 @@ ResourceFilter.prototype.equal = function(field, value) {
);
};

/**
* Adds an exists operation to the filter.
*
* @param {String} field The name of the field
* @param {Boolean} value true if the field should exist, false otherwise
* @return {module:storages/ResourceFilter~ResourceFilter} The actual filter
* @throws {TypeError} An error if field and / or value is not valid
*/
ResourceFilter.prototype.exists = function(field, value) {
return addComparisonOperation.call(
this,
field,
value,
ResourceFilter.OPERATORS.EXISTS,
['Boolean']
);
};

/**
* Adds a "not equal" operation to the filter.
*
Expand Down
4 changes: 4 additions & 0 deletions lib/storages/databases/mongodb/MongoDatabase.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ MongoDatabase.buildFilter = function(resourceFilter) {
if (!filter[operation.field]) filter[operation.field] = {};
filter[operation.field]['$eq'] = operation.value;
break;
case ResourceFilter.OPERATORS.EXISTS:
if (!filter[operation.field]) filter[operation.field] = {};
filter[operation.field]['$exists'] = operation.value;
break;
case ResourceFilter.OPERATORS.NOT_EQUAL:
if (!filter[operation.field]) filter[operation.field] = {};
filter[operation.field]['$ne'] = operation.value;
Expand Down
12 changes: 12 additions & 0 deletions tests/server/storages/MongoDatabase.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ describe('MongoDatabase', function() {
type: ResourceFilter.OPERATORS.EQUAL,
mongoOperator: '$eq'
},
{
field: 'field1',
value: true,
type: ResourceFilter.OPERATORS.EXISTS,
mongoOperator: '$exists'
},
{
field: 'field2',
value: 'value2',
Expand Down Expand Up @@ -354,6 +360,12 @@ describe('MongoDatabase', function() {
type: ResourceFilter.OPERATORS.EQUAL,
mongoOperator: '$eq'
},
{
field: 'field',
value: true,
type: ResourceFilter.OPERATORS.EXISTS,
mongoOperator: '$exists'
},
{
field: 'field',
value: '43',
Expand Down
24 changes: 24 additions & 0 deletions tests/server/storages/ResourceFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,30 @@ describe('ResourceFilter', function() {

});

describe(ResourceFilter.OPERATORS.EXISTS, function() {

it('should add a "' + ResourceFilter.OPERATORS.EXISTS + '" operation', function() {
var expectedValue = true;
var expectedField = 'field';
filter[ResourceFilter.OPERATORS.EXISTS](expectedField, expectedValue);

assert.equal(filter.operations[0].type, ResourceFilter.OPERATORS.EXISTS, 'Wrong operation type');
assert.equal(filter.operations[0].field, expectedField, 'Wrong operation field');
assert.equal(filter.operations[0].value, expectedValue, 'Wrong operation value');
});

it('should throw a TypeError if value is not a boolean', function() {
var wrongValues = [[], 42, /regexp/, {}, 'String'];

wrongValues.forEach(function(wrongValue) {
assert.throws(function() {
filter[ResourceFilter.OPERATORS.EXISTS]('field', wrongValue);
});
});
});

});

describe('hasOperation', function() {

it('should return true if an operation type is already present in the list of operations', function() {
Expand Down

0 comments on commit 291c151

Please sign in to comment.