Skip to content

Commit

Permalink
Merge pull request #14685 from Budibase/count-field-name
Browse files Browse the repository at this point in the history
Symbolise the special __bb_total count field name.
  • Loading branch information
samwho authored Oct 2, 2024
2 parents 92988f1 + 8e120b2 commit 2ddafee
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/backend-core/src/sql/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * as utils from "./utils"

export { default as Sql } from "./sql"
export { default as Sql, COUNT_FIELD_NAME } from "./sql"
export { default as SqlTable } from "./sqlTable"
export * as designDoc from "./designDoc"
4 changes: 3 additions & 1 deletion packages/backend-core/src/sql/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}`
)
}

Expand Down
10 changes: 7 additions & 3 deletions packages/server/src/sdk/app/rows/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { Format } from "../../../api/controllers/view/exporters"
import sdk from "../.."
import { extractViewInfoFromID, isRelationshipColumn } from "../../../db/utils"
import { isSQL } from "../../../integrations/utils"
import { docIds } from "@budibase/backend-core"
import { docIds, sql } from "@budibase/backend-core"
import { getTableFromSource } from "../../../api/controllers/row/utils"

const SQL_CLIENT_SOURCE_MAP: Record<SourceName, SqlClient | undefined> = {
Expand Down Expand Up @@ -57,8 +57,12 @@ 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 &&
sql.COUNT_FIELD_NAME in response[0]
) {
const total = response[0][sql.COUNT_FIELD_NAME]
return typeof total === "number" ? total : parseInt(total)
} else {
throw new Error("Unable to count rows in query - no count response")
Expand Down

0 comments on commit 2ddafee

Please sign in to comment.