diff --git a/package.json b/package.json index 7f393db..c4f549b 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/src/paginateable.entity.ts b/src/paginateable.entity.ts index 5a6e074..4d4ce54 100755 --- a/src/paginateable.entity.ts +++ b/src/paginateable.entity.ts @@ -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 { /** @@ -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 = { diff --git a/src/utils/getFindOperator.ts b/src/utils/getFindOperator.ts new file mode 100644 index 0000000..dc31c8d --- /dev/null +++ b/src/utils/getFindOperator.ts @@ -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 { + 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); + } + } +}