Skip to content

Commit

Permalink
Merge pull request #6 from rjlopezdev/feat/not-operator
Browse files Browse the repository at this point in the history
Feat: not operator
  • Loading branch information
rjlopezdev authored Apr 2, 2019
2 parents a2f1b9f + e452db8 commit e94e3fa
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 8 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ This library allows you to transfrom automatically Express.js _req.query_ into T

## How it works?

![](typeorm-express-pipeline.png)
![](https://raw.githubusercontent.com/rjlopezdev/typeorm-express-query-builder/master/typeorm-express-pipeline.png)


## Usage
Expand Down Expand Up @@ -118,6 +118,11 @@ __gte__ | Return entries with value greater than or equal to provided | `foo__gt
__in__ | Return entries that match with values in list | `foo__in=admin,common`
__between__ | Return entries in range | `foo__between=1,27`

**Notice**: you can use negative logic prefixing lookup with `__not`.

*Example:*
`foo__not__contains=value`

## Options

| Option | Default | Behaviour | Example |
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typeorm-express-query-builder",
"version": "1.0.1",
"version": "1.1.1",
"description": "Easily transform an Express req.query into TypeORM query",
"main": "index.js",
"scripts": {
Expand Down
10 changes: 8 additions & 2 deletions src/field-filter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { AbstractFilter } from './filter';
import { LookupFilter } from './lookup.enum';
import { Like, IsNull, LessThan, LessThanOrEqual, MoreThan, MoreThanOrEqual, In, Between } from 'typeorm';
import { Like, IsNull, LessThan, LessThanOrEqual, MoreThan, MoreThanOrEqual, In, Between, Not } from 'typeorm';

export class FieldFilter extends AbstractFilter {

constructor(query: any, prop: string, lookup: LookupFilter, value: string) {
private notOperator: boolean;

constructor(query: any, prop: string, lookup: LookupFilter, value: string, notOperator: boolean = false) {
super(query, prop, lookup, value);
this.notOperator = notOperator;
}

public buildQuery() {
Expand Down Expand Up @@ -48,6 +51,9 @@ export class FieldFilter extends AbstractFilter {
queryToAdd = { [this.prop]: Between(+rangeValues[0], +rangeValues[1]) };
break;
}
if(this.notOperator) {
queryToAdd[this.prop] = Not(queryToAdd[this.prop]);
}
this.query['where'] = {
...this.query['where'],
...queryToAdd
Expand Down
5 changes: 3 additions & 2 deletions src/filter-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ export class FilterFactory {
public get(query: any, key: string, value: string): AbstractFilter {
if (this.isFieldFilter(key)) {
const field = key.split(LookupDelimiter.LOOKUP_DELIMITER)[0];
const notQuery = key.includes(`${LookupDelimiter.LOOKUP_DELIMITER}${LookupFilter.NOT}`);
const lookup = key.includes(LookupDelimiter.LOOKUP_DELIMITER)
? key.split(LookupDelimiter.LOOKUP_DELIMITER)[1] as LookupFilter
? key.split(LookupDelimiter.LOOKUP_DELIMITER)[notQuery ? 2 : 1] as LookupFilter
: LookupFilter.EXACT;
return new FieldFilter(query, field, lookup, value);
return new FieldFilter(query, field, lookup, value, notQuery);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/lookup.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export enum LookupFilter {
STARTS_WITH = 'startswith',
ENDS_WITH = 'endswith',
IN = 'in',
BETWEEN = 'between'
BETWEEN = 'between',
NOT = 'not',
}

export enum LookupDelimiter {
Expand Down
8 changes: 7 additions & 1 deletion test/unit/field-filter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Like, IsNull, MoreThan, MoreThanOrEqual, LessThanOrEqual, LessThan, Between, In } from 'typeorm';
import { Like, IsNull, MoreThan, MoreThanOrEqual, LessThanOrEqual, LessThan, Between, In, Not } from 'typeorm';
import { FieldFilter } from '../../src/field-filter';
import { LookupFilter } from '../../src/lookup.enum';

Expand Down Expand Up @@ -72,4 +72,10 @@ describe('Test FieldFilter #buildQuery', () => {
expect(built['where']['name']).toEqual(In(['1', '2', '3', '4', 'foo']));
});

it('should return a <not> filter', () => {
const fieldFilter = new FieldFilter(built, 'name', LookupFilter.EXACT, 'value', true);
fieldFilter.buildQuery();
expect(built['where']['name']).toEqual(Not('value'));
});

});
12 changes: 12 additions & 0 deletions test/unit/filter-factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ describe('Test FilterFactory #get', () => {
const filter = factory.get({}, 'field', 'value');
expect(filter).toBeInstanceOf(FieldFilter);
});

it('should return an instance of FieldFilter with notOperator equals to true', () => {
const filter = factory.get({}, 'field__not', 'value') as any;
expect(filter).toBeInstanceOf(FieldFilter);
expect(filter.notOperator).toBeTruthy();
});

it('should return an instance of FieldFilter with notOperator equals to false', () => {
const filter = factory.get({}, 'field', 'value') as any;
expect(filter).toBeInstanceOf(FieldFilter);
expect(filter.notOperator).toBeFalsy();
});
})


Expand Down

0 comments on commit e94e3fa

Please sign in to comment.