1
0
Fork 0
mirror of synced 2024-07-07 07:15:43 +12:00

Addressing PR comments.

This commit is contained in:
mike12345567 2024-06-19 18:46:48 +01:00
parent 58ec7a50b0
commit 67c00c9e4c
3 changed files with 28 additions and 20 deletions

View file

@ -642,23 +642,24 @@ class InternalBuilder {
} else if (paginate && paginate.limit) {
foundLimit = paginate.limit
}
// add the found limit if supplied
if (foundLimit) {
query = query.limit(foundLimit)
}
// add overall pagination
if (foundOffset) {
query = query.offset(foundOffset)
// counting should not sort, limit or offset
if (!counting) {
// add the found limit if supplied
if (foundLimit != null) {
query = query.limit(foundLimit)
}
// add overall pagination
if (foundOffset != null) {
query = query.offset(foundOffset)
}
// add sorting to pre-query
// no point in sorting when counting
query = this.addSorting(query, json)
}
// add filters to the query (where)
query = this.addFilters(query, filters, json.meta.table, {
aliases: tableAliases,
})
// add sorting to pre-query
// no point in sorting when counting
if (!counting) {
query = this.addSorting(query, json)
}
const alias = tableAliases?.[tableName] || tableName
let preQuery: Knex.QueryBuilder = knex({
@ -668,11 +669,10 @@ class InternalBuilder {
// be a table name, not a pre-query
[alias]: query as any,
})
if (!counting) {
preQuery = preQuery.select(generateSelectStatement(json, knex))
} else {
preQuery = this.addDistinctCount(preQuery, json)
}
// if counting, use distinct count, else select
preQuery = !counting
? preQuery.select(generateSelectStatement(json, knex))
: this.addDistinctCount(preQuery, json)
// have to add after as well (this breaks MS-SQL)
if (this.client !== SqlClient.MS_SQL && !counting) {
preQuery = this.addSorting(preQuery, json)

View file

@ -88,7 +88,8 @@ export async function search(
}
const responses = await Promise.all(queries)
let rows = responses[0] as Row[]
const totalRows = responses[1] ? (responses[1] as number) : undefined
const totalRows =
responses.length > 1 ? (responses[1] as number) : undefined
let hasNextPage = false
// remove the extra row if it's there
@ -115,6 +116,9 @@ export async function search(
if (totalRows != null) {
response.totalRows = totalRows
}
if (paginate && !hasNextPage) {
response.hasNextPage = false
}
return response
} catch (err: any) {
if (err.message && err.message.includes("does not exist")) {

View file

@ -227,7 +227,8 @@ export async function search(
}
const responses = await Promise.all(queries)
let rows = responses[0] as Row[]
const totalRows = responses[1] ? (responses[1] as number) : undefined
const totalRows =
responses.length > 1 ? (responses[1] as number) : undefined
// process from the format of tableId.column to expected format also
// make sure JSON columns corrected
@ -260,7 +261,7 @@ export async function search(
const response: SearchResponse<Row> = {
rows: finalRows,
}
if (totalRows) {
if (totalRows != null) {
response.totalRows = totalRows
}
// check for pagination
@ -268,6 +269,9 @@ export async function search(
response.hasNextPage = true
response.bookmark = bookmark + 1
}
if (paginate && !nextRow) {
response.hasNextPage = false
}
return response
} catch (err: any) {
const msg = typeof err === "string" ? err : err.message