diff --git a/package.json b/package.json index 4dfde61..4e8f71e 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "scripts": { "prebuild": "rimraf lib", "build": "tsc", + "dev:yalc": "watch 'npm run build && yalc push' src", "format": "prettier --write \"src/**/*.ts\"", "format:ci": "prettier --list-different \"src/**/*.ts\"", "lint": "eslint -c .eslintrc.json --ext .ts --max-warnings 0 src", diff --git a/src/paginate.spec.ts b/src/paginate.spec.ts index b452414..a469407 100644 --- a/src/paginate.spec.ts +++ b/src/paginate.spec.ts @@ -1359,6 +1359,52 @@ describe('paginate', () => { expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&filter.age=$not:$null') }) + it('should return result based on not null query', async () => { + const config: PaginateConfig = { + sortableColumns: ['id'], + filterableColumns: { + age: [FilterOperator.NOT, FilterOperator.NULL], + }, + } + const query: PaginateQuery = { + path: '', + filter: { + age: '$not:$null', + }, + } + + const result = await paginate(query, catRepo, config) + + expect(result.data).toStrictEqual([cats[0], cats[1], cats[2], cats[3]]) + expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&filter.age=$not:$null') + }) + + it('should return result based on not null query on relation', async () => { + const config: PaginateConfig = { + sortableColumns: ['id'], + filterableColumns: { + 'home.name': [FilterOperator.NOT, FilterOperator.NULL], + }, + relations: ['home'], + } + const query: PaginateQuery = { + path: '', + filter: { + 'home.name': '$not:$null', + }, + } + + const result = await paginate(query, catRepo, config) + const expectedResult = [0, 1].map((i) => { + const ret = Object.assign(clone(cats[i]), { home: Object.assign(clone(catHomes[i])) }) + delete ret.home.cat + return ret + }) + + expect(result.data).toStrictEqual(expectedResult) + expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&filter.home.name=$not:$null') + }) + it('should ignore filterable column which is not configured', async () => { const config: PaginateConfig = { sortableColumns: ['id'], diff --git a/src/paginate.ts b/src/paginate.ts index e13ed03..f608217 100644 --- a/src/paginate.ts +++ b/src/paginate.ts @@ -328,17 +328,18 @@ export async function paginate( for (const column in filter) { const propertyPath = (column as string).split('.') if (propertyPath.length > 1) { - const condition = qb['getWherePredicateCondition']( - column, - filter[column] - ) as WherePredicateOperator let parameters = { [column]: filter[column].value } // TODO: refactor below - const alias = queryBuilder.expressionMap.mainAlias.metadata.hasRelationWithPropertyPath( + const isRelation = queryBuilder.expressionMap.mainAlias.metadata.hasRelationWithPropertyPath( propertyPath[0] ) - ? `${qb.alias}_${column}` - : `${qb.alias}.${column}` + const alias = isRelation ? `${qb.alias}_${column}` : `${qb.alias}.${column}` + + const condition = qb['getWherePredicateCondition']( + alias, + filter[column] + ) as WherePredicateOperator + switch (condition.operator) { case 'between': condition.parameters = [alias, `:${column}_from`, `:${column}_to`] @@ -397,7 +398,7 @@ export async function paginate( data: items, meta: { itemsPerPage: isPaginated ? limit : items.length, - totalItems, + totalItems: isPaginated ? totalItems : items.length, currentPage: page, totalPages, sortBy, diff --git a/tsconfig.json b/tsconfig.json index 3e51f85..61e17ef 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,7 +9,8 @@ "sourceMap": true, "outDir": "./lib", "baseUrl": "./", - "incremental": true + "incremental": true, + "skipLibCheck": true // https://stackoverflow.com/questions/55680391/typescript-error-ts2403-subsequent-variable-declarations-must-have-the-same-typ }, "include": ["src"] }