Skip to content

Commit

Permalink
Added ability to use filter in the first level
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalanamini committed Dec 27, 2018
1 parent 13343b7 commit f8013d5
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 78 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

---

## [v1.6.0](https://github.com/foxifyjs/foxify-restify-odin/releases/tag/v1.6.0) - *(2018-12-28)*

- :star2: Added ability to use `has` filter in the first `and` level

## [v1.5.0](https://github.com/foxifyjs/foxify-restify-odin/releases/tag/v1.5.0) - *(2018-12-16)*

- :zap: Added `Odin`'s `whereHas` compatibility
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Easily restify odin databases

- [Node.js](https://nodejs.org/en/download) `8.12` or higher is required.
- [foxify](https://github.com/foxifyjs/foxify) `0.10.20` or higher is required.
- [@foxify/odin](https://github.com/foxifyjs/odin) `0.6.0` or higher is required.
- [@foxify/odin](https://github.com/foxifyjs/odin) `0.8.0` or higher is required.

### Installation

Expand Down Expand Up @@ -183,7 +183,7 @@ possible operators:
qs.stringify({
include: [
"relation1",
"relation2",
"relation2.subRelation1.subRelation2",
]
})
```
Expand Down Expand Up @@ -218,13 +218,13 @@ qs.stringify({

## Versioning

We use [SemVer](http://semver.org) for versioning. For the versions available, see the [tags on this repository](https://github.com/foxifyjs/foxify/tags).
We use [SemVer](http://semver.org) for versioning. For the versions available, see the [tags on this repository](https://github.com/foxifyjs/foxify-restify-odin/tags).

## Authors

- **Ardalan Amini** - *Owner/Developer* - [@ardalanamini](https://github.com/ardalanamini)

See also the list of [contributors](https://github.com/foxifyjs/foxify/contributors) who participated in this project.
See also the list of [contributors](https://github.com/foxifyjs/foxify-restify-odin/contributors) who participated in this project.

## License

Expand Down
103 changes: 59 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "foxify-restify-odin",
"version": "1.5.0",
"version": "1.6.0",
"description": "Easily restify odin databases",
"author": "Ardalan Amini <[email protected]> [https://github.com/ardalanamini]",
"license": "MIT",
Expand Down Expand Up @@ -38,11 +38,12 @@
"qs": "^6.6.0"
},
"peerDependencies": {
"@foxify/odin": ">=0.7.5",
"@foxify/odin": ">=0.8.0",
"foxify": ">=0.10.20"
},
"devDependencies": {
"@foxify/odin": "^0.7.5",
"@foxify/odin": "^0.8.0",
"@foxify/schema": "^1.0.1",
"@types/body-parser": "^1.17.0",
"@types/jest": "^23.3.10",
"@types/qs": "^6.5.1",
Expand All @@ -52,9 +53,9 @@
"jest": "^23.6.0",
"jest-environment-node": "^23.4.0",
"mongodb": "^3.1.10",
"mongodb-memory-server": "^2.8.0",
"mongodb-memory-server": "^2.9.1",
"ts-jest": "^23.10.5",
"tslint": "^5.11.0",
"tslint": "^5.12.0",
"tslint-config-airbnb": "^5.11.1",
"typescript": "^3.2.2"
},
Expand Down
28 changes: 20 additions & 8 deletions src/builders/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,25 @@ const operate = (query: typeof Odin, field: string, operator: Operator, value: a
}
};

const and = (query: any, filters: Array<FilterObject | Filter>, had: boolean): any => filters
.reduce((prev, curr) => prev.where((q: any) => filter(q, curr, had)), query);
const and = (
query: any, filters: Array<FilterObject | Filter>, had: boolean, level: number,
): any => filters
.reduce(
(prev, curr) => {
if ((curr as any).has) return filter(prev, curr, had, level);

const or = (query: any, filters: Array<FilterObject | Filter>, had: boolean): any => filters.reduce(
return prev.where((q: any) => filter(q, curr, had, level + 1));
},
query,
);

const or = (
query: any, filters: Array<FilterObject | Filter>, had: boolean, level: number,
): any => filters.reduce(
(prev, curr, index) => {
if (index === 0) return prev.where((q: any) => filter(q, curr, had));
if (index === 0) return prev.where((q: any) => filter(q, curr, had, level + 1));

return prev.orWhere((q: any) => filter(q, curr, had));
return prev.orWhere((q: any) => filter(q, curr, had, level + 1));
},
query,
);
Expand All @@ -66,23 +77,24 @@ const has = (
);
};

const filter = (model: any, filters: any, had = false) => {
const filter = (model: any, filters: any, had = false, level = 0) => {
if (filters.and) {
if (filters.or || filters.has) {
throw new TypeError("filter can only have one of [\"and\", \"or\", \"has\"]");
}

return and(model, filters.and, had);
return and(model, filters.and, had, level);
}

if (filters.or) {
if (filters.has) throw new TypeError("filter can only have one of [\"and\", \"or\", \"has\"]");

return or(model, filters.or, had);
return or(model, filters.or, had, level);
}

if (filters.has) {
if (had) throw new Error("Can't use \"has\" inside has");
if (level !== 0) throw new Error("Can't use \"has\" at this level");

return has(model, filters.has);
}
Expand Down
Loading

0 comments on commit f8013d5

Please sign in to comment.