fix: not:null on relation (#431)

This commit is contained in:
xMase 2023-01-03 09:52:35 +01:00 committed by GitHub
parent b7f600f7f6
commit b1fd9dd700
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 9 deletions

View File

@ -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",

View File

@ -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<CatEntity> = {
sortableColumns: ['id'],
filterableColumns: {
age: [FilterOperator.NOT, FilterOperator.NULL],
},
}
const query: PaginateQuery = {
path: '',
filter: {
age: '$not:$null',
},
}
const result = await paginate<CatEntity>(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<CatEntity> = {
sortableColumns: ['id'],
filterableColumns: {
'home.name': [FilterOperator.NOT, FilterOperator.NULL],
},
relations: ['home'],
}
const query: PaginateQuery = {
path: '',
filter: {
'home.name': '$not:$null',
},
}
const result = await paginate<CatEntity>(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<CatEntity> = {
sortableColumns: ['id'],

View File

@ -328,17 +328,18 @@ export async function paginate<T extends ObjectLiteral>(
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<T extends ObjectLiteral>(
data: items,
meta: {
itemsPerPage: isPaginated ? limit : items.length,
totalItems,
totalItems: isPaginated ? totalItems : items.length,
currentPage: page,
totalPages,
sortBy,

View File

@ -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"]
}