Skip to content
Open
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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@
"@types/jest": "24.0.11",
"@types/node": "11.13.4",
"@types/supertest": "2.0.7",
"rxjs": "^6.5.4",
"jest": "24.7.1",
"prettier": "1.17.0",
"rxjs": "^6.5.4",
"supertest": "4.0.2",
"ts-jest": "24.0.2",
"ts-node": "8.1.0",
"tsc-watch": "2.2.1",
"tsconfig-paths": "3.8.0",
"typeorm": "^0.2.24",
"tslint": "5.16.0",
"typescript": "3.4.3"
"typeorm": "^0.2.29",
"typescript": "~3.9.7"
},
"jest": {
"moduleFileExtensions": [
Expand Down
9 changes: 3 additions & 6 deletions src/paginateable.entity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { BadRequestException } from '@nestjs/common';
import { BaseEntity, ObjectType, FindManyOptions, In, Equal } from 'typeorm';
import { BaseEntity, ObjectType, FindManyOptions } from 'typeorm';
import { PaginationParams } from './pagination.decorator';
import { Pagination } from './pagination.interface';
import { getFindOperator } from './utils/getFindOperator';

export abstract class PaginateableBaseEntity extends BaseEntity {
/**
Expand Down Expand Up @@ -49,11 +50,7 @@ export abstract class PaginateableBaseEntity extends BaseEntity {
const filterQuery = {};

toFilter.forEach(([key, value]) => {
if (Array.isArray(value)) {
filterQuery[key] = In(value);
} else {
filterQuery[key] = Equal(value);
}
filterQuery[key] = getFindOperator(value);
});

query = {
Expand Down
46 changes: 46 additions & 0 deletions src/utils/getFindOperator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Equal, FindOperator, In, LessThan, LessThanOrEqual, MoreThan, MoreThanOrEqual } from 'typeorm';

enum ComparatorTypeEnum {
GREATER_THAN= '>',
LESS_THAN = '<',
GREATER_THAN_OR_EQUAL = '>=',
LESS_THAN_OR_EQUAL = '<=',
EQUAL = '=',
}

const isCompareSign = char => {
return Object.values(ComparatorTypeEnum).includes(char as ComparatorTypeEnum);
};

export function getFindOperator(value: any): FindOperator<any> {
if (Array.isArray(value)) {
return In(value);
}

const compareSign = value
.substr(0, 2)
.split('')
.filter(isCompareSign)
.join('');

const realValue = value.substr(compareSign.length);

switch (compareSign) {
case ComparatorTypeEnum.GREATER_THAN: {
return MoreThan(realValue);
}
case ComparatorTypeEnum.GREATER_THAN_OR_EQUAL: {
return MoreThanOrEqual(realValue);
}
case ComparatorTypeEnum.LESS_THAN: {
return LessThan(realValue);
}
case ComparatorTypeEnum.LESS_THAN_OR_EQUAL: {
return LessThanOrEqual(realValue);
}
case ComparatorTypeEnum.EQUAL:
default: {
return Equal(realValue);
}
}
}