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

Commit b762cb2

Browse files
committed
feat: Adds $search operator.
1 parent 50f9995 commit b762cb2

11 files changed

+38
-13
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"check-coverage": true
2626
},
2727
"dependencies": {
28-
"@js-entity-repos/core": "^9.0.0",
28+
"@js-entity-repos/core": "^9.1.0",
2929
"lodash": "^4.17.4",
3030
"mongodb": "^3.0.0"
3131
},

src/functions/countEntities.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import CountEntities from '@js-entity-repos/core/dist/signatures/CountEntities';
22
import Entity from '@js-entity-repos/core/dist/types/Entity';
33
import FacadeConfig from '../FacadeConfig';
4+
import constructMongoFilter from '../utils/constructMongoFilter';
45

56
export default <E extends Entity>(config: FacadeConfig<E>): CountEntities<E> => {
67
return async ({ filter = {} }) => {
78
const db = (await config.db());
89
const collection = db.collection(config.collectionName);
910
const constructedFilter = config.constructFilter(filter);
10-
const count = await collection.find(constructedFilter).count();
11+
const mongoFilter = constructMongoFilter(constructedFilter);
12+
const count = await collection.find(mongoFilter).count();
1113
return { count };
1214
};
1315
};

src/functions/getEntities.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import createGetEntitiesResult from '@js-entity-repos/core/dist/utils/createGetE
88
import createPaginationFilter from '@js-entity-repos/core/dist/utils/createPaginationFilter';
99
import { mapValues } from 'lodash';
1010
import FacadeConfig from '../FacadeConfig';
11+
import constructMongoFilter from '../utils/constructMongoFilter';
1112

1213
const xor = (conditionA: boolean, conditionB: boolean) => {
1314
return (conditionA && !conditionB) || (!conditionA && conditionB);
@@ -28,12 +29,13 @@ export default <E extends Entity>(config: FacadeConfig<E>): GetEntities<E> => {
2829
const fullFilter = { $and: [filter, paginationFilter] };
2930
const constructedFilter = config.constructFilter(fullFilter);
3031
const constructedSort = config.constructSort(sort);
32+
const mongoFilter = constructMongoFilter(constructedFilter);
3133
const mongoSort = mapValues(constructedSort, (sortOrder: SortOrder) => {
3234
return !xor(pagination.direction === forward, sortOrder === asc) ? 1 : -1;
3335
});
3436

3537
const results = await collection
36-
.find(constructedFilter)
38+
.find(mongoFilter)
3739
.sort(mongoSort)
3840
.limit(pagination.limit + 1)
3941
.toArray();

src/functions/getEntity.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import GetEntity from '@js-entity-repos/core/dist/signatures/GetEntity';
33
import Entity from '@js-entity-repos/core/dist/types/Entity';
44
import FacadeConfig from '../FacadeConfig';
55
import constructIdFilter from '../utils/constructIdFilter';
6+
import constructMongoFilter from '../utils/constructMongoFilter';
67

78
export default <E extends Entity>(config: FacadeConfig<E>): GetEntity<E> => {
89
return async ({ id, filter = {} }) => {
910
const db = (await config.db());
1011
const collection = db.collection(config.collectionName);
1112
const constructedFilter = constructIdFilter({ id, filter, config });
12-
const document = await collection.findOne(constructedFilter);
13+
const mongoFilter = constructMongoFilter(constructedFilter);
14+
const document = await collection.findOne(mongoFilter);
1315

1416
if (document === undefined || document === null) {
1517
throw new MissingEntityError(config.entityName, id);

src/functions/patchEntity.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import PatchEntity from '@js-entity-repos/core/dist/signatures/PatchEntity';
33
import Entity from '@js-entity-repos/core/dist/types/Entity';
44
import FacadeConfig from '../FacadeConfig';
55
import constructIdFilter from '../utils/constructIdFilter';
6+
import constructMongoFilter from '../utils/constructMongoFilter';
67

78
export default <E extends Entity>(config: FacadeConfig<E>): PatchEntity<E> => {
89
return async ({ id, patch, filter = {} }) => {
@@ -12,7 +13,8 @@ export default <E extends Entity>(config: FacadeConfig<E>): PatchEntity<E> => {
1213
const update = { $set: document };
1314
const opts = { returnOriginal: false, upsert: false };
1415
const constructedFilter = constructIdFilter({ id, filter, config });
15-
const { value } = await collection.findOneAndUpdate(constructedFilter, update, opts);
16+
const mongoFilter = constructMongoFilter(constructedFilter);
17+
const { value } = await collection.findOneAndUpdate(mongoFilter, update, opts);
1618

1719
if (value === undefined || value === null) {
1820
throw new MissingEntityError(config.entityName, id);

src/functions/removeEntities.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import RemoveEntities from '@js-entity-repos/core/dist/signatures/RemoveEntities';
22
import Entity from '@js-entity-repos/core/dist/types/Entity';
33
import FacadeConfig from '../FacadeConfig';
4+
import constructMongoFilter from '../utils/constructMongoFilter';
45

56
export default <E extends Entity>(config: FacadeConfig<E>): RemoveEntities<E> => {
67
return async ({ filter = {} }) => {
78
const db = (await config.db());
89
const collection = db.collection(config.collectionName);
910
const constructedFilter = config.constructFilter(filter);
10-
await collection.remove(constructedFilter);
11+
const mongoFilter = constructMongoFilter(constructedFilter);
12+
await collection.remove(mongoFilter);
1113
};
1214
};

src/functions/removeEntity.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import RemoveEntity from '@js-entity-repos/core/dist/signatures/RemoveEntity';
33
import Entity from '@js-entity-repos/core/dist/types/Entity';
44
import FacadeConfig from '../FacadeConfig';
55
import constructIdFilter from '../utils/constructIdFilter';
6+
import constructMongoFilter from '../utils/constructMongoFilter';
67

78
export default <E extends Entity>(config: FacadeConfig<E>): RemoveEntity<E> => {
89
return async ({ id, filter = {} }) => {
910
const db = (await config.db());
1011
const collection = db.collection(config.collectionName);
1112
const constructedFilter = constructIdFilter({ id, filter, config });
12-
const { value } = await collection.findOneAndDelete(constructedFilter);
13+
const mongoFilter = constructMongoFilter(constructedFilter);
14+
const { value } = await collection.findOneAndDelete(mongoFilter);
1315

1416
if (value === undefined || value === null) {
1517
throw new MissingEntityError(config.entityName, id);

src/functions/replaceEntity.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ import ReplaceEntity from '@js-entity-repos/core/dist/signatures/ReplaceEntity';
33
import Entity from '@js-entity-repos/core/dist/types/Entity';
44
import FacadeConfig from '../FacadeConfig';
55
import constructIdFilter from '../utils/constructIdFilter';
6+
import constructMongoFilter from '../utils/constructMongoFilter';
67

78
export default <E extends Entity>(config: FacadeConfig<E>): ReplaceEntity<E> => {
89
return async ({ id, entity, filter = {} }) => {
910
const db = (await config.db());
1011
const collection = db.collection(config.collectionName);
1112
const opts = { returnOriginal: false, upsert: false };
1213
const constructedFilter = constructIdFilter({ id, filter, config });
13-
const { value } = await collection.findOneAndUpdate(constructedFilter, entity, opts);
14+
const mongoFilter = constructMongoFilter(constructedFilter);
15+
const { value } = await collection.findOneAndUpdate(mongoFilter, entity, opts);
1416

1517
if (value === undefined || value === null) {
1618
throw new MissingEntityError(config.entityName, id);

src/utils/constructMongoFilter.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Entity from '@js-entity-repos/core/dist/types/Entity';
2+
import { Filter } from '@js-entity-repos/core/dist/types/Filter';
3+
import parseFilterKey from './parseFilterKey';
4+
5+
export default <E extends Entity>(filter: Filter<E>) => {
6+
return parseFilterKey('$search', (value: any) => {
7+
return { $regex: new RegExp(value, 'i') };
8+
})(filter);
9+
};

0 commit comments

Comments
 (0)