1
0
Fork 0
mirror of synced 2024-10-05 20:44:47 +13: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) { } else if (paginate && paginate.limit) {
foundLimit = paginate.limit foundLimit = paginate.limit
} }
// add the found limit if supplied // counting should not sort, limit or offset
if (foundLimit) { if (!counting) {
query = query.limit(foundLimit) // add the found limit if supplied
} if (foundLimit != null) {
// add overall pagination query = query.limit(foundLimit)
if (foundOffset) { }
query = query.offset(foundOffset) // 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) // add filters to the query (where)
query = this.addFilters(query, filters, json.meta.table, { query = this.addFilters(query, filters, json.meta.table, {
aliases: tableAliases, 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 const alias = tableAliases?.[tableName] || tableName
let preQuery: Knex.QueryBuilder = knex({ let preQuery: Knex.QueryBuilder = knex({
@ -668,11 +669,10 @@ class InternalBuilder {
// be a table name, not a pre-query // be a table name, not a pre-query
[alias]: query as any, [alias]: query as any,
}) })
if (!counting) { // if counting, use distinct count, else select
preQuery = preQuery.select(generateSelectStatement(json, knex)) preQuery = !counting
} else { ? preQuery.select(generateSelectStatement(json, knex))
preQuery = this.addDistinctCount(preQuery, json) : this.addDistinctCount(preQuery, json)
}
// have to add after as well (this breaks MS-SQL) // have to add after as well (this breaks MS-SQL)
if (this.client !== SqlClient.MS_SQL && !counting) { if (this.client !== SqlClient.MS_SQL && !counting) {
preQuery = this.addSorting(preQuery, json) preQuery = this.addSorting(preQuery, json)

View file

@ -88,7 +88,8 @@ export async function search(
} }
const responses = await Promise.all(queries) const responses = await Promise.all(queries)
let rows = responses[0] as Row[] 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 let hasNextPage = false
// remove the extra row if it's there // remove the extra row if it's there
@ -115,6 +116,9 @@ export async function search(
if (totalRows != null) { if (totalRows != null) {
response.totalRows = totalRows response.totalRows = totalRows
} }
if (paginate && !hasNextPage) {
response.hasNextPage = false
}
return response return response
} catch (err: any) { } catch (err: any) {
if (err.message && err.message.includes("does not exist")) { 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) const responses = await Promise.all(queries)
let rows = responses[0] as Row[] 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 // process from the format of tableId.column to expected format also
// make sure JSON columns corrected // make sure JSON columns corrected
@ -260,7 +261,7 @@ export async function search(
const response: SearchResponse<Row> = { const response: SearchResponse<Row> = {
rows: finalRows, rows: finalRows,
} }
if (totalRows) { if (totalRows != null) {
response.totalRows = totalRows response.totalRows = totalRows
} }
// check for pagination // check for pagination
@ -268,6 +269,9 @@ export async function search(
response.hasNextPage = true response.hasNextPage = true
response.bookmark = bookmark + 1 response.bookmark = bookmark + 1
} }
if (paginate && !nextRow) {
response.hasNextPage = false
}
return response return response
} catch (err: any) { } catch (err: any) {
const msg = typeof err === "string" ? err : err.message const msg = typeof err === "string" ? err : err.message