Skip to content

Commit

Permalink
feat(filters): allow the use of the NotIn operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Molinié committed Aug 2, 2024
1 parent ecc3031 commit 938fcca
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/agent/src/utils/condition-tree-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default class ConditionTreeParser {
return 'Number';
}

if (filter.operator === 'In') {
if (filter.operator === 'In' || filter.operator === 'NotIn') {
return [fieldSchema.columnType];
}

Expand Down
7 changes: 4 additions & 3 deletions packages/agent/src/utils/forest-schema/filterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@ export default class FrontendFilterableUtils {
Boolean: FrontendFilterableUtils.baseOperators,
Date: FrontendFilterableUtils.dateOperators,
Dateonly: FrontendFilterableUtils.dateOperators,
Enum: [...FrontendFilterableUtils.baseOperators, 'In'],
Number: [...FrontendFilterableUtils.baseOperators, 'In', 'GreaterThan', 'LessThan'],
Enum: [...FrontendFilterableUtils.baseOperators, 'In', 'NotIn'],
Number: [...FrontendFilterableUtils.baseOperators, 'In', 'NotIn', 'GreaterThan', 'LessThan'],
String: [
...FrontendFilterableUtils.baseOperators,
'In',
'NotIn',
'StartsWith',
'EndsWith',
'Contains',
'NotContains',
],
Time: [...FrontendFilterableUtils.baseOperators, 'GreaterThan', 'LessThan'],
Uuid: FrontendFilterableUtils.baseOperators,
Uuid: [...FrontendFilterableUtils.baseOperators, 'In', 'NotIn'],
};

/**
Expand Down
14 changes: 8 additions & 6 deletions packages/agent/test/utils/condition-tree-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,17 @@ describe('ConditionTreeParser', () => {
value: 'id1,id2 , id3',
});

expect(tree).toStrictEqual(new ConditionTreeLeaf('id', 'In', ['id1', 'id2', 'id3']));
expect(tree).toStrictEqual(new ConditionTreeLeaf('id', 'NotIn', ['id1', 'id2', 'id3']));

const treeWithArray = ConditionTreeParser.fromPlainObject(collection, {
field: 'id',
operator: 'not_in',
value: ['id1', 'id2', 'id3'],
});

expect(treeWithArray).toStrictEqual(new ConditionTreeLeaf('id', 'In', ['id1', 'id2', 'id3']));
expect(treeWithArray).toStrictEqual(
new ConditionTreeLeaf('id', 'NotIn', ['id1', 'id2', 'id3']),
);
});

test('should work with not_in and number', () => {
Expand All @@ -131,15 +133,15 @@ describe('ConditionTreeParser', () => {
value: '1, 2, 3 , invalid',
});

expect(tree).toStrictEqual(new ConditionTreeLeaf('number', 'In', [1, 2, 3]));
expect(tree).toStrictEqual(new ConditionTreeLeaf('number', 'NotIn', [1, 2, 3]));

const treeWithArray = ConditionTreeParser.fromPlainObject(collection, {
field: 'number',
operator: 'not_in',
value: [1, 2, 3, 'invalid'],
});

expect(treeWithArray).toStrictEqual(new ConditionTreeLeaf('number', 'In', [1, 2, 3]));
expect(treeWithArray).toStrictEqual(new ConditionTreeLeaf('number', 'NotIn', [1, 2, 3]));
});

test('should work with not_in and boolean', () => {
Expand All @@ -150,7 +152,7 @@ describe('ConditionTreeParser', () => {
});

expect(tree).toStrictEqual(
new ConditionTreeLeaf('boolean', 'In', [true, false, false, true, false]),
new ConditionTreeLeaf('boolean', 'NotIn', [true, false, false, true, false]),
);

const treeWithArray = ConditionTreeParser.fromPlainObject(collection, {
Expand All @@ -160,7 +162,7 @@ describe('ConditionTreeParser', () => {
});

expect(treeWithArray).toStrictEqual(
new ConditionTreeLeaf('boolean', 'In', [true, false, false, true, false]),
new ConditionTreeLeaf('boolean', 'NotIn', [true, false, false, true, false]),
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ export default class EmptyCollectionDecorator extends CollectionDecorator {
valuesByField[field] = valuesByField[field].includes(value) ? [value] : [];
} else if (valuesByField[field] && operator === 'In') {
valuesByField[field] = valuesByField[field].filter(v => (value as unknown[]).includes(v));
} else if (valuesByField[field] && operator === 'NotIn') {
valuesByField[field] = valuesByField[field].filter(v => !(value as unknown[]).includes(v));
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/datasource-mongoose/src/utils/pipeline/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ export default class FilterGenerator {
return { $ne: formattedLeafValue };
case 'In':
return { $in: formattedLeafValue };
case 'NotIn':
return { $nin: formattedLeafValue };
case 'IncludesAll':
return { $all: formattedLeafValue };
case 'IncludesNone':
Expand Down

0 comments on commit 938fcca

Please sign in to comment.