From f46523f38d039f06c5b4dba22916ab433db2fec4 Mon Sep 17 00:00:00 2001 From: Ryan Smith <0ryansmith1994@gmail.com> Date: Thu, 29 Mar 2018 10:58:00 +0100 Subject: [PATCH 1/2] feat: Adds $search operator. --- docs/options.md | 14 +++++++---- src/tests/utils/filterTest.ts | 46 +++++++++++++++++++++++++++-------- src/types/Filter.ts | 1 + 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/docs/options.md b/docs/options.md index 7788d3a0..5b1cf992 100644 --- a/docs/options.md +++ b/docs/options.md @@ -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. @@ -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 @@ -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"] } } ] @@ -93,7 +97,7 @@ The filter below is comprehensive example using all of the operators. { "$nor": [ { - "stringProp4": "string value 6" + "stringProp5": "string value 7" } ] } diff --git a/src/tests/utils/filterTest.ts b/src/tests/utils/filterTest.ts index 6d559bad..2e823bd0 100644 --- a/src/tests/utils/filterTest.ts +++ b/src/tests/utils/filterTest.ts @@ -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'; @@ -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 () => { @@ -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 () => { @@ -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({ id: { $search: 'b' } }); + }); + + it('should filter correctly when using $search operator with uppercase', async () => { + await createTestEntities(); + await opts.assertFirstEntityFilter({ id: { $search: 'B' } }); }); }; diff --git a/src/types/Filter.ts b/src/types/Filter.ts index 79be844a..19a68218 100644 --- a/src/types/Filter.ts +++ b/src/types/Filter.ts @@ -10,6 +10,7 @@ export interface PropFilter { readonly $eq?: Prop; readonly $ne?: Prop; readonly $not?: PropFilter; + readonly $search?: Prop; } export type EntityFilter = { From 0cf39b65fd81fd886ac0497a2c0d5ea4b06d4364 Mon Sep 17 00:00:00 2001 From: Ryan Smith <0ryansmith1994@gmail.com> Date: Thu, 29 Mar 2018 10:59:27 +0100 Subject: [PATCH 2/2] fix: Corrects search property name. --- src/tests/utils/filterTest.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tests/utils/filterTest.ts b/src/tests/utils/filterTest.ts index 2e823bd0..7305524c 100644 --- a/src/tests/utils/filterTest.ts +++ b/src/tests/utils/filterTest.ts @@ -105,11 +105,11 @@ export default (opts: Opts) => { it('should filter correctly when using $search operator with lowercase', async () => { await createTestEntities(); - await opts.assertFirstEntityFilter({ id: { $search: 'b' } }); + await opts.assertFirstEntityFilter({ stringProp: { $search: 'b' } }); }); it('should filter correctly when using $search operator with uppercase', async () => { await createTestEntities(); - await opts.assertFirstEntityFilter({ id: { $search: 'B' } }); + await opts.assertFirstEntityFilter({ stringProp: { $search: 'B' } }); }); };