fix: not:null on relation (#431)
This commit is contained in:
parent
b7f600f7f6
commit
b1fd9dd700
@ -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",
|
||||
|
@ -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'],
|
||||
|
@ -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,
|
||||
|
@ -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"]
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user