Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for searching by row ID (with a limit) when the row has many related rows #14257

Merged
merged 5 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions packages/server/src/api/routes/tests/search.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import {
knexClient,
} from "../../../integrations/tests/utils"
import {
db as dbCore,
context,
db as dbCore,
MAX_VALID_DATE,
MIN_VALID_DATE,
utils,
SQLITE_DESIGN_DOC_ID,
utils,
} from "@budibase/backend-core"

import * as setup from "./utilities"
Expand Down Expand Up @@ -2560,4 +2560,48 @@ describe.each([
}).toContainExactly([{ name: "foo" }])
})
})

!isInMemory &&
describe("search by _id", () => {
let row: Row

beforeAll(async () => {
const toRelateTable = await createTable({
name: {
name: "name",
type: FieldType.STRING,
},
})
table = await createTable({
name: {
name: "name",
type: FieldType.STRING,
},
rel: {
name: "rel",
type: FieldType.LINK,
relationshipType: RelationshipType.MANY_TO_MANY,
tableId: toRelateTable._id!,
fieldName: "rel",
},
})
const [row1, row2] = await Promise.all([
config.api.row.save(toRelateTable._id!, { name: "tag 1" }),
config.api.row.save(toRelateTable._id!, { name: "tag 2" }),
])
row = await config.api.row.save(table._id!, {
name: "product 1",
rel: [row1._id, row2._id],
})
})

it("can filter by the row ID with limit 1", async () => {
await expectSearch({
query: {
equal: { _id: row._id },
},
limit: 1,
}).toContainExactly([row])
})
})
})
50 changes: 37 additions & 13 deletions packages/server/src/sdk/app/rows/search/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ import { HTTPError } from "@budibase/backend-core"
import pick from "lodash/pick"
import { outputProcessing } from "../../../../utilities/rowProcessor"
import sdk from "../../../"
import { isSearchingByRowID } from "./utils"

export async function search(
options: RowSearchParams,
table: Table
): Promise<SearchResponse<Row>> {
const { tableId } = options
const { countRows, paginate, query, ...params } = options
const { limit } = params
let bookmark =
(params.bookmark && parseInt(params.bookmark as string)) || undefined
if (paginate && !bookmark) {
bookmark = 0
}
function getPaginationAndLimitParameters(
filters: SearchFilters,
paginate: boolean | undefined,
bookmark: number | undefined,
limit: number | undefined
): PaginationJson | undefined {
let paginateObj: PaginationJson | undefined

// only try set limits/pagination if we aren't doing a row ID search
if (isSearchingByRowID(filters)) {
return
}

if (paginate && !limit) {
throw new Error("Cannot paginate query without a limit")
}
Expand All @@ -49,11 +49,35 @@ export async function search(
if (bookmark) {
paginateObj.offset = limit * bookmark
}
} else if (params && limit) {
} else if (limit) {
paginateObj = {
limit: limit,
}
}

return paginateObj
}

export async function search(
options: RowSearchParams,
table: Table
): Promise<SearchResponse<Row>> {
const { tableId } = options
const { countRows, paginate, query, ...params } = options
const { limit } = params
let bookmark =
(params.bookmark && parseInt(params.bookmark as string)) || undefined
if (paginate && !bookmark) {
bookmark = 0
}

let paginateObj = getPaginationAndLimitParameters(
query,
paginate,
bookmark,
limit
)

let sort: SortJson | undefined
if (params.sort) {
const direction =
Expand Down
13 changes: 8 additions & 5 deletions packages/server/src/sdk/app/rows/search/sqs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
getTableIDList,
} from "./filters"
import { dataFilters, PROTECTED_INTERNAL_COLUMNS } from "@budibase/shared-core"
import { isSearchingByRowID } from "./utils"

const builder = new sql.Sql(SqlClient.SQL_LITE)
const MISSING_COLUMN_REGEX = new RegExp(`no such column: .+`)
Expand Down Expand Up @@ -264,17 +265,18 @@ export async function search(

const relationships = buildInternalRelationships(table)

const searchFilters: SearchFilters = {
...cleanupFilters(query, table, allTables),
documentType: DocumentType.ROW,
}
const request: QueryJson = {
endpoint: {
// not important, we query ourselves
datasourceId: SQS_DATASOURCE_INTERNAL,
entityId: table._id!,
operation: Operation.READ,
},
filters: {
...cleanupFilters(query, table, allTables),
documentType: DocumentType.ROW,
},
filters: searchFilters,
table,
meta: {
table,
Expand Down Expand Up @@ -304,7 +306,8 @@ export async function search(
}

const bookmark: number = (params.bookmark as number) || 0
if (params.limit) {
// limits don't apply if we doing a row ID search
if (!isSearchingByRowID(searchFilters) && params.limit) {
paginate = true
request.paginate = {
limit: params.limit + 1,
Expand Down
15 changes: 15 additions & 0 deletions packages/server/src/sdk/app/rows/search/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,18 @@ export function searchInputMapping(table: Table, options: RowSearchParams) {
}
return options
}

export function isSearchingByRowID(query: SearchFilters): boolean {
for (let searchField of Object.values(query)) {
if (typeof searchField !== "object") {
continue
}
const hasId = Object.keys(searchField).find(
key => dbCore.removeKeyNumbering(key) === "_id" && searchField[key]
)
if (hasId) {
return true
}
}
return false
}
Loading