From 4dd6afd4352ba774bde40764cf6d9a315d983d7b Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 2 Oct 2024 09:57:18 +0100 Subject: [PATCH] Symbolise the special __bb_total count field name. --- packages/backend-core/src/sql/sql.ts | 4 +++- packages/server/src/sdk/app/rows/utils.ts | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/backend-core/src/sql/sql.ts b/packages/backend-core/src/sql/sql.ts index 627be039ca..3585dacbed 100644 --- a/packages/backend-core/src/sql/sql.ts +++ b/packages/backend-core/src/sql/sql.ts @@ -43,6 +43,8 @@ import { cloneDeep } from "lodash" type QueryFunction = (query: SqlQuery | SqlQuery[], operation: Operation) => any +export const COUNT_FIELD_NAME = "__bb_total" + function getBaseLimit() { const envLimit = 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") } return query.countDistinct( - `${this.getTableName()}.${this.table.primary[0]} as __bb_total` + `${this.getTableName()}.${this.table.primary[0]} as ${COUNT_FIELD_NAME}` ) } diff --git a/packages/server/src/sdk/app/rows/utils.ts b/packages/server/src/sdk/app/rows/utils.ts index 3d6bf39d3f..e1b5615046 100644 --- a/packages/server/src/sdk/app/rows/utils.ts +++ b/packages/server/src/sdk/app/rows/utils.ts @@ -22,6 +22,7 @@ import { extractViewInfoFromID, isRelationshipColumn } from "../../../db/utils" import { isSQL } from "../../../integrations/utils" import { docIds } from "@budibase/backend-core" 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.POSTGRES]: SqlClient.POSTGRES, @@ -57,8 +58,8 @@ export function getSQLClient(datasource: Datasource): SqlClient { export function processRowCountResponse( response: DatasourcePlusQueryResponse ): number { - if (response && response.length === 1 && "__bb_total" in response[0]) { - const total = response[0].__bb_total + if (response && response.length === 1 && COUNT_FIELD_NAME in response[0]) { + const total = response[0][COUNT_FIELD_NAME] return typeof total === "number" ? total : parseInt(total) } else { throw new Error("Unable to count rows in query - no count response")