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

feat(filters): allow the use of the NotIn operator #1153

Open
wants to merge 4 commits into
base: main
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
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
86 changes: 86 additions & 0 deletions packages/agent/test/utils/condition-tree-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ describe('ConditionTreeParser', () => {
});

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

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

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

test('should work with in and number', () => {
Expand All @@ -66,6 +74,14 @@ describe('ConditionTreeParser', () => {
});

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

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

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

test('should work with in and boolean', () => {
Expand All @@ -78,6 +94,76 @@ describe('ConditionTreeParser', () => {
expect(tree).toStrictEqual(
new ConditionTreeLeaf('boolean', 'In', [true, false, false, true, false]),
);

const treeWithArray = ConditionTreeParser.fromPlainObject(collection, {
field: 'boolean',
operator: 'in',
value: [true, 0, false, 'yes', 'no'],
});

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

test('should work with not_in and string', () => {
const tree = ConditionTreeParser.fromPlainObject(collection, {
field: 'id',
operator: 'not_in',
value: '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', 'NotIn', ['id1', 'id2', 'id3']),
);
});

test('should work with not_in and number', () => {
const tree = ConditionTreeParser.fromPlainObject(collection, {
field: 'number',
operator: 'not_in',
value: '1, 2, 3 , invalid',
});

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', 'NotIn', [1, 2, 3]));
});

test('should work with not_in and boolean', () => {
const tree = ConditionTreeParser.fromPlainObject(collection, {
field: 'boolean',
operator: 'not_in',
value: 'true, 0, false, yes, no',
});

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

const treeWithArray = ConditionTreeParser.fromPlainObject(collection, {
field: 'boolean',
operator: 'not_in',
value: [true, 0, false, 'yes', 'no'],
});

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

test('should work with ShorterThan and string', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/agent/test/utils/forest-schema/filterable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('FrontendFilterableUtils', () => {
'Present',
'Blank',
'In',
'NotIn',
'StartsWith',
'EndsWith',
'Contains',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ describe('SchemaGeneratorFields > Column', () => {
'Present',
'Blank',
'In',
'NotIn',
'StartsWith',
'EndsWith',
'Contains',
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 @@ -181,6 +181,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
Loading