This commit is contained in:
ppetzold 2021-10-12 13:22:25 +02:00
parent 8447f4c90f
commit 8a7c130c18
3 changed files with 285 additions and 10404 deletions

10660
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -50,6 +50,9 @@
"typeorm": "^0.2.38", "typeorm": "^0.2.38",
"typescript": "^4.4.3" "typescript": "^4.4.3"
}, },
"dependencies": {
"lodash": "^4.17.21"
},
"peerDependencies": { "peerDependencies": {
"@nestjs/common": "^8.0.11", "@nestjs/common": "^8.0.11",
"express": "^4.17.1", "express": "^4.17.1",
@ -84,8 +87,5 @@
"branches": [ "branches": [
"master" "master"
] ]
},
"dependencies": {
"lodash": "^4.17.21"
} }
} }

View File

@ -81,29 +81,30 @@ export async function paginate<T>(
return !!entityColumns.find((c) => c === column) return !!entityColumns.find((c) => c === column)
} }
const { sortableColumns, searchableColumns } = config
if (config.sortableColumns.length < 1) throw new ServiceUnavailableException() if (config.sortableColumns.length < 1) throw new ServiceUnavailableException()
if (query.sortBy) { if (query.sortBy) {
for (const order of query.sortBy) { for (const order of query.sortBy) {
if (isEntityKey(sortableColumns, order[0]) && ['ASC', 'DESC'].includes(order[1])) { if (isEntityKey(config.sortableColumns, order[0]) && ['ASC', 'DESC'].includes(order[1])) {
sortBy.push(order as Order<T>) sortBy.push(order as Order<T>)
} }
} }
} }
if (!sortBy.length) { if (!sortBy.length) {
sortBy.push(...(config.defaultSortBy || [[sortableColumns[0], 'ASC']])) sortBy.push(...(config.defaultSortBy || [[config.sortableColumns[0], 'ASC']]))
} }
if (query.searchBy && searchableColumns) { if (config.searchableColumns) {
for (const column of query.searchBy) { if (query.searchBy) {
if (isEntityKey(searchableColumns, column)) { for (const column of query.searchBy) {
searchBy.push(column as Column<T>) if (isEntityKey(config.searchableColumns, column)) {
searchBy.push(column)
}
} }
} else {
searchBy.push(...config.searchableColumns)
} }
} else if (!query.searchBy && searchableColumns) {
searchBy.push(...searchableColumns)
} }
if (page < 1) page = 1 if (page < 1) page = 1
@ -232,7 +233,7 @@ export async function paginate<T>(
) )
: '' : ''
const options = `&limit=${limit}${sortByQuery}${searchQuery}${filterQuery}${searchByQuery}` const options = `&limit=${limit}${sortByQuery}${searchQuery}${searchByQuery}${filterQuery}`
const buildLink = (p: number): string => path + '?page=' + p + options const buildLink = (p: number): string => path + '?page=' + p + options