Symbolise the special __bb_total count field name.

This commit is contained in:
Sam Rose 2024-10-02 09:57:18 +01:00
parent 361a9a4af8
commit 4dd6afd435
No known key found for this signature in database
2 changed files with 6 additions and 3 deletions

View File

@ -43,6 +43,8 @@ import { cloneDeep } from "lodash"
type QueryFunction = (query: SqlQuery | SqlQuery[], operation: Operation) => any type QueryFunction = (query: SqlQuery | SqlQuery[], operation: Operation) => any
export const COUNT_FIELD_NAME = "__bb_total"
function getBaseLimit() { function getBaseLimit() {
const envLimit = environment.SQL_MAX_ROWS const envLimit = environment.SQL_MAX_ROWS
? parseInt(environment.SQL_MAX_ROWS) ? parseInt(environment.SQL_MAX_ROWS)
@ -846,7 +848,7 @@ class InternalBuilder {
throw new Error("SQL counting requires primary key to be supplied") throw new Error("SQL counting requires primary key to be supplied")
} }
return query.countDistinct( return query.countDistinct(
`${this.getTableName()}.${this.table.primary[0]} as __bb_total` `${this.getTableName()}.${this.table.primary[0]} as ${COUNT_FIELD_NAME}`
) )
} }

View File

@ -22,6 +22,7 @@ import { extractViewInfoFromID, isRelationshipColumn } from "../../../db/utils"
import { isSQL } from "../../../integrations/utils" import { isSQL } from "../../../integrations/utils"
import { docIds } from "@budibase/backend-core" import { docIds } from "@budibase/backend-core"
import { getTableFromSource } from "../../../api/controllers/row/utils" import { getTableFromSource } from "../../../api/controllers/row/utils"
import { COUNT_FIELD_NAME } from "@budibase/backend-core/src/sql/sql"
const SQL_CLIENT_SOURCE_MAP: Record<SourceName, SqlClient | undefined> = { const SQL_CLIENT_SOURCE_MAP: Record<SourceName, SqlClient | undefined> = {
[SourceName.POSTGRES]: SqlClient.POSTGRES, [SourceName.POSTGRES]: SqlClient.POSTGRES,
@ -57,8 +58,8 @@ export function getSQLClient(datasource: Datasource): SqlClient {
export function processRowCountResponse( export function processRowCountResponse(
response: DatasourcePlusQueryResponse response: DatasourcePlusQueryResponse
): number { ): number {
if (response && response.length === 1 && "__bb_total" in response[0]) { if (response && response.length === 1 && COUNT_FIELD_NAME in response[0]) {
const total = response[0].__bb_total const total = response[0][COUNT_FIELD_NAME]
return typeof total === "number" ? total : parseInt(total) return typeof total === "number" ? total : parseInt(total)
} else { } else {
throw new Error("Unable to count rows in query - no count response") throw new Error("Unable to count rows in query - no count response")