Skip to content
This repository was archived by the owner on Jun 22, 2021. It is now read-only.

feat: Adds $search operator. #14

Merged
merged 2 commits into from
Mar 29, 2018
Merged
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
14 changes: 9 additions & 5 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Operator | Description
[$gte](https://docs.mongodb.com/manual/reference/operator/query/gt/#op._S_gt) | Includes entities where the value of a given property is greater than or equal to the specified value.
[$in](https://docs.mongodb.com/manual/reference/operator/query/in/#op._S_in) | Includes entities where the value of a given property is equal to one of the specified values.
[$nin](https://docs.mongodb.com/manual/reference/operator/query/ni/#op._S_ni) | Includes entities where the value of a given property is not equal to any of the specified values.
$search | Includes entities where the value of a given property contains the specified value as a substring.

The filter below is comprehensive example using all of the operators.

Expand All @@ -72,6 +73,9 @@ The filter below is comprehensive example using all of the operators.
"$and": [
{
"stringProp1": "string value 1",
"stringProp2": {
"$search": "string value 2"
},
"numberProp2": {
"$gte": 0,
"$lte": 1
Expand All @@ -80,11 +84,11 @@ The filter below is comprehensive example using all of the operators.
"numberProp4": { "$eq": 0 }
},
{
"stringProp2": {
"$in": ["string value 2", "string value 3"]
},
"stringProp3": {
"$nin": ["string value 4", "string value 5"]
"$in": ["string value 3", "string value 4"]
},
"stringProp4": {
"$nin": ["string value 5", "string value 6"]
}
}
]
Expand All @@ -93,7 +97,7 @@ The filter below is comprehensive example using all of the operators.
{
"$nor": [
{
"stringProp4": "string value 6"
"stringProp5": "string value 7"
}
]
}
Expand Down
46 changes: 36 additions & 10 deletions src/tests/utils/filterTest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// tslint:disable:max-file-line-count
import 'mocha'; // tslint:disable-line:no-import-side-effect
import Facade from '../../Facade';
import Filter from '../../types/Filter';
Expand All @@ -13,8 +14,8 @@ export interface Opts {
}
export const firstId = 'test_id_1';
export const secondId = 'test_id_2';
export const firstEntity = { ...testEntity, id: firstId, stringProp: 'a', numberProp: 1 };
export const secondEntity = { ...testEntity, id: secondId, stringProp: 'b', numberProp: 2 };
export const firstEntity = { ...testEntity, id: firstId, stringProp: 'abc', numberProp: 1 };
export const secondEntity = { ...testEntity, id: secondId, stringProp: 'def', numberProp: 2 };

export default (opts: Opts) => {
const createTestEntities = async () => {
Expand All @@ -39,17 +40,17 @@ export default (opts: Opts) => {

it('should filter correctly when not using an operator', async () => {
await createTestEntities();
await opts.assertFirstEntityFilter({ stringProp: 'a' });
await opts.assertFirstEntityFilter({ stringProp: firstEntity.stringProp });
});

it('should filter correctly when using $eq operator', async () => {
await createTestEntities();
await opts.assertFirstEntityFilter({ stringProp: { $eq: 'a' } });
await opts.assertFirstEntityFilter({ stringProp: { $eq: firstEntity.stringProp } });
});

it('should filter correctly when using $ne operator', async () => {
await createTestEntities();
await opts.assertFirstEntityFilter({ stringProp: { $ne: 'b' } });
await opts.assertFirstEntityFilter({ stringProp: { $ne: secondEntity.stringProp } });
});

it('should filter correctly when using $gt and $lt operators', async () => {
Expand All @@ -64,26 +65,51 @@ export default (opts: Opts) => {

it('should filter correctly when using $in operator', async () => {
await createTestEntities();
await opts.assertFirstEntityFilter({ stringProp: { $in: ['a'] } });
await opts.assertFirstEntityFilter({ stringProp: { $in: [firstEntity.stringProp] } });
});

it('should filter correctly when using $and operator', async () => {
await createTestEntities();
await opts.assertFirstEntityFilter({ $and: [{ stringProp: 'a' }, { numberProp: 1 }] });
await opts.assertFirstEntityFilter({
$and: [
{ stringProp: firstEntity.stringProp },
{ numberProp: 1 },
],
});
});

it('should filter correctly when using $or operator', async () => {
await createTestEntities();
await opts.assertFirstEntityFilter({ $or: [{ stringProp: 'a' }, { numberProp: 1 }] });
await opts.assertFirstEntityFilter({
$or: [
{ stringProp: firstEntity.stringProp },
{ numberProp: 1 },
],
});
});

it('should filter correctly when using $nor operator', async () => {
await createTestEntities();
await opts.assertFirstEntityFilter({ $nor: [{ stringProp: 'b' }, { numberProp: 2 }] });
await opts.assertFirstEntityFilter({
$nor: [
{ stringProp: secondEntity.stringProp },
{ numberProp: 2 },
],
});
});

it('should filter correctly when using $not operator', async () => {
await createTestEntities();
await opts.assertFirstEntityFilter({ stringProp: { $not: { $eq: 'b' } } });
await opts.assertFirstEntityFilter({ stringProp: { $not: { $eq: secondEntity.stringProp } } });
});

it('should filter correctly when using $search operator with lowercase', async () => {
await createTestEntities();
await opts.assertFirstEntityFilter({ stringProp: { $search: 'b' } });
});

it('should filter correctly when using $search operator with uppercase', async () => {
await createTestEntities();
await opts.assertFirstEntityFilter({ stringProp: { $search: 'B' } });
});
};
1 change: 1 addition & 0 deletions src/types/Filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface PropFilter<Prop> {
readonly $eq?: Prop;
readonly $ne?: Prop;
readonly $not?: PropFilter<Prop>;
readonly $search?: Prop;
}

export type EntityFilter<E extends Entity> = {
Expand Down