1
0
Fork 0
mirror of synced 2024-09-25 13:51:40 +12:00

Fix search API break.

This commit is contained in:
Sam Rose 2024-09-24 18:07:31 +01:00
parent e3256cb005
commit f475454bce
No known key found for this signature in database
14 changed files with 66 additions and 67 deletions

View file

@ -79,8 +79,8 @@ export class QueryBuilder<T> {
return this
}
setSource(sourceId: string) {
this.#query.equal!.tableId = sourceId
setTable(tableId: string) {
this.#query.equal!.tableId = tableId
return this
}
@ -637,8 +637,8 @@ async function recursiveSearch<T>(
.setSortOrder(params.sortOrder)
.setSortType(params.sortType)
if (params.sourceId) {
queryBuilder.setSource(params.sourceId)
if (params.tableId) {
queryBuilder.setTable(params.tableId)
}
const page = await queryBuilder.run()
@ -671,8 +671,8 @@ export async function paginatedSearch<T>(
if (params.version) {
search.setVersion(params.version)
}
if (params.sourceId) {
search.setSource(params.sourceId)
if (params.tableId) {
search.setTable(params.tableId)
}
if (params.sort) {
search
@ -694,8 +694,8 @@ export async function paginatedSearch<T>(
// Try fetching 1 row in the next page to see if another page of results
// exists or not
search.setBookmark(searchResults.bookmark).setLimit(1)
if (params.sourceId) {
search.setSource(params.sourceId)
if (params.tableId) {
search.setTable(params.tableId)
}
const nextResults = await search.run()

View file

@ -366,7 +366,7 @@ describe("lucene", () => {
},
},
{
sourceId: TABLE_ID,
tableId: TABLE_ID,
limit: 1,
sort: "property",
sortType: SortType.STRING,
@ -390,7 +390,7 @@ describe("lucene", () => {
},
},
{
sourceId: TABLE_ID,
tableId: TABLE_ID,
query: {},
}
)

View file

@ -221,7 +221,8 @@ export async function search(ctx: Ctx<SearchRowRequest, SearchRowResponse>) {
const searchParams: RowSearchParams = {
...ctx.request.body,
query: enrichedQuery,
sourceId: viewId || tableId,
tableId,
viewId,
}
ctx.status = 200

View file

@ -3,8 +3,6 @@ import {
ViewV2,
SearchRowResponse,
SearchViewRowRequest,
RequiredKeys,
RowSearchParams,
SearchFilterKey,
LogicalOperator,
Aggregation,
@ -83,9 +81,9 @@ export async function searchView(
field,
}))
const searchOptions: RequiredKeys<SearchViewRowRequest> &
RequiredKeys<Pick<RowSearchParams, "sourceId" | "query" | "fields">> = {
sourceId: view.id,
const result = await sdk.rows.search({
viewId: view.id,
tableId: view.tableId,
query: enrichedQuery,
fields: viewFields,
...getSortOptions(body, view),
@ -94,9 +92,8 @@ export async function searchView(
paginate: body.paginate,
countRows: body.countRows,
aggregations,
}
})
const result = await sdk.rows.search(searchOptions)
result.rows.forEach(r => (r._viewId = view.id))
ctx.body = result
}

View file

@ -157,7 +157,11 @@ describe.each([
if (isInMemory) {
return dataFilters.search(_.cloneDeep(rows), this.query)
} else {
return config.api.row.search(this.query.sourceId, this.query)
const sourceId = this.query.viewId || this.query.tableId
if (!sourceId) {
throw new Error("No source ID provided")
}
return config.api.row.search(sourceId, this.query)
}
}
@ -327,8 +331,8 @@ describe.each([
}
}
function expectSearch(query: Omit<RowSearchParams, "sourceId">) {
return new SearchAssertion({ ...query, sourceId: table._id! })
function expectSearch(query: Omit<RowSearchParams, "tableId">) {
return new SearchAssertion({ ...query, tableId: table._id! })
}
function expectQuery(query: SearchFilters) {
@ -1898,7 +1902,7 @@ describe.each([
let { rows: fullRowList } = await config.api.row.search(
table._id!,
{
sourceId: table._id!,
tableId: table._id!,
query: {},
}
)
@ -1909,7 +1913,7 @@ describe.each([
rowCount: number = 0
do {
const response = await config.api.row.search(table._id!, {
sourceId: table._id!,
tableId: table._id!,
limit: 1,
paginate: true,
query: {},
@ -1933,7 +1937,7 @@ describe.each([
// eslint-disable-next-line no-constant-condition
while (true) {
const response = await config.api.row.search(table._id!, {
sourceId: table._id!,
tableId: table._id!,
limit: 3,
query: {},
bookmark,

View file

@ -113,7 +113,7 @@ describe("/templates", () => {
expect(users.name).toBe("Users")
const { rows } = await config.api.row.search(agencyProjects._id!, {
sourceId: agencyProjects._id!,
tableId: agencyProjects._id!,
query: {},
})

View file

@ -219,7 +219,7 @@ describe("Google Sheets Integration", () => {
})
let resp = await config.api.row.search(table._id!, {
sourceId: table._id!,
tableId: table._id!,
query: {},
paginate: true,
limit: 10,
@ -228,7 +228,7 @@ describe("Google Sheets Integration", () => {
while (resp.hasNextPage) {
resp = await config.api.row.search(table._id!, {
sourceId: table._id!,
tableId: table._id!,
query: {},
paginate: true,
limit: 10,
@ -637,7 +637,7 @@ describe("Google Sheets Integration", () => {
it("should be able to find rows with equals filter", async () => {
const response = await config.api.row.search(table._id!, {
sourceId: table._id!,
tableId: table._id!,
query: {
equal: {
name: "Foo",
@ -651,7 +651,7 @@ describe("Google Sheets Integration", () => {
it("should be able to find rows with not equals filter", async () => {
const response = await config.api.row.search(table._id!, {
sourceId: table._id!,
tableId: table._id!,
query: {
notEqual: {
name: "Foo",
@ -666,7 +666,7 @@ describe("Google Sheets Integration", () => {
it("should be able to find rows with empty filter", async () => {
const response = await config.api.row.search(table._id!, {
sourceId: table._id!,
tableId: table._id!,
query: {
empty: {
name: null,
@ -679,7 +679,7 @@ describe("Google Sheets Integration", () => {
it("should be able to find rows with not empty filter", async () => {
const response = await config.api.row.search(table._id!, {
sourceId: table._id!,
tableId: table._id!,
query: {
notEmpty: {
name: null,
@ -692,7 +692,7 @@ describe("Google Sheets Integration", () => {
it("should be able to find rows with one of filter", async () => {
const response = await config.api.row.search(table._id!, {
sourceId: table._id!,
tableId: table._id!,
query: {
oneOf: {
name: ["Foo", "Bar"],
@ -707,7 +707,7 @@ describe("Google Sheets Integration", () => {
it("should be able to find rows with fuzzy filter", async () => {
const response = await config.api.row.search(table._id!, {
sourceId: table._id!,
tableId: table._id!,
query: {
fuzzy: {
name: "oo",
@ -721,7 +721,7 @@ describe("Google Sheets Integration", () => {
it("should be able to find rows with range filter", async () => {
const response = await config.api.row.search(table._id!, {
sourceId: table._id!,
tableId: table._id!,
query: {
range: {
name: {
@ -750,7 +750,7 @@ describe("Google Sheets Integration", () => {
})
let response = await config.api.row.search(table._id!, {
sourceId: table._id!,
tableId: table._id!,
query: { equal: { name: "Unique value!" } },
paginate: true,
limit: 10,
@ -759,7 +759,7 @@ describe("Google Sheets Integration", () => {
while (response.hasNextPage) {
response = await config.api.row.search(table._id!, {
sourceId: table._id!,
tableId: table._id!,
query: { equal: { name: "Unique value!" } },
paginate: true,
limit: 10,

View file

@ -14,7 +14,7 @@ import { ExportRowsParams, ExportRowsResult } from "./search/types"
import { dataFilters } from "@budibase/shared-core"
import sdk from "../../index"
import { searchInputMapping } from "./search/utils"
import { features, docIds } from "@budibase/backend-core"
import { features } from "@budibase/backend-core"
import tracer from "dd-trace"
import { getQueryableFields, removeInvalidFilters } from "./queryUtils"
@ -38,7 +38,8 @@ export async function search(
): Promise<SearchResponse<Row>> {
return await tracer.trace("search", async span => {
span?.addTags({
sourceId: options.sourceId,
tableId: options.tableId,
viewId: options.viewId,
query: options.query,
sort: options.sort,
sortOrder: options.sortOrder,
@ -76,20 +77,16 @@ export async function search(
let source: Table | ViewV2
let table: Table
if (docIds.isTableId(options.sourceId)) {
source = await sdk.tables.getTable(options.sourceId)
table = source
options = searchInputMapping(source, options)
} else if (docIds.isViewId(options.sourceId)) {
source = await sdk.views.get(options.sourceId)
table = await sdk.tables.getTable(source.tableId)
if (options.viewId) {
source = await sdk.views.get(options.viewId)
table = await sdk.views.getTable(source)
options = searchInputMapping(table, options)
} else if (options.tableId) {
source = await sdk.tables.getTable(options.tableId)
table = source
options = searchInputMapping(table, options)
span.addTags({
tableId: table._id,
})
} else {
throw new Error(`Invalid source ID: ${options.sourceId}`)
throw new Error(`Invalid source ID: ${options.viewId || options.tableId}`)
}
if (options.query) {

View file

@ -200,7 +200,7 @@ export async function exportRows(
}
let result = await search(
{ sourceId: table._id!, query: requestQuery, sort, sortOrder },
{ tableId: table._id!, query: requestQuery, sort, sortOrder },
table
)
let rows: Row[] = []

View file

@ -64,7 +64,7 @@ export async function exportRows(
result = await outputProcessing(table, response)
} else if (query) {
let searchResponse = await sdk.rows.search({
sourceId: tableId,
tableId,
query,
sort,
sortOrder,

View file

@ -19,8 +19,6 @@ export async function search(
options: RowSearchParams,
source: Table | ViewV2
): Promise<SearchResponse<Row>> {
const { sourceId } = options
let table: Table
if (sdk.views.isView(source)) {
table = await sdk.views.getTable(source.id)
@ -31,7 +29,8 @@ export async function search(
const { paginate, query } = options
const params: RowSearchParams = {
sourceId: table._id!,
tableId: options.tableId,
viewId: options.viewId,
sort: options.sort,
sortOrder: options.sortOrder,
sortType: options.sortType,
@ -59,7 +58,7 @@ export async function search(
// Enrich search results with relationships
if (response.rows && response.rows.length) {
// enrich with global users if from users table
if (sourceId === InternalTables.USER_METADATA) {
if (table._id === InternalTables.USER_METADATA) {
response.rows = await getGlobalUsersFromMetadata(response.rows as User[])
}

View file

@ -122,7 +122,7 @@ describe.each([
it("querying by fields will always return data attribute columns", async () => {
await config.doInContext(config.appId, async () => {
const { rows } = await search({
sourceId: table._id!,
tableId: table._id!,
query: {},
fields: ["name", "age"],
})
@ -142,7 +142,7 @@ describe.each([
it("will decode _id in oneOf query", async () => {
await config.doInContext(config.appId, async () => {
const result = await search({
sourceId: table._id!,
tableId: table._id!,
query: {
oneOf: {
_id: ["%5B1%5D", "%5B4%5D", "%5B8%5D"],
@ -174,7 +174,7 @@ describe.each([
},
})
const result = await search({
sourceId: table._id!,
tableId: table._id!,
query: {},
})
expect(result.rows).toHaveLength(10)
@ -205,7 +205,7 @@ describe.each([
},
})
const result = await search({
sourceId: table._id!,
tableId: table._id!,
query: {},
fields: ["name", "age"],
})
@ -229,7 +229,7 @@ describe.each([
async (queryFields, expectedRows) => {
await config.doInContext(config.appId, async () => {
const { rows } = await search({
sourceId: table._id!,
tableId: table._id!,
query: {
$or: {
conditions: [

View file

@ -48,7 +48,7 @@ describe.each([tableWithUserCol, tableWithUsersCol])(
it("should be able to map ro_ to global user IDs", () => {
const params: RowSearchParams = {
sourceId: tableId,
tableId: tableId,
query: {
equal: {
"1:user": userMedataId,
@ -61,7 +61,7 @@ describe.each([tableWithUserCol, tableWithUsersCol])(
it("should handle array of user IDs", () => {
const params: RowSearchParams = {
sourceId: tableId,
tableId: tableId,
query: {
oneOf: {
"1:user": [userMedataId, globalUserId],
@ -78,7 +78,7 @@ describe.each([tableWithUserCol, tableWithUsersCol])(
it("shouldn't change any other input", () => {
const email = "test@example.com"
const params: RowSearchParams = {
sourceId: tableId,
tableId: tableId,
query: {
equal: {
"1:user": email,
@ -91,7 +91,7 @@ describe.each([tableWithUserCol, tableWithUsersCol])(
it("shouldn't error if no query supplied", () => {
// @ts-expect-error - intentionally passing in a bad type
const output = searchInputMapping(col, { sourceId: tableId })
const output = searchInputMapping(col, { tableId })
expect(output.query).toBeUndefined()
})
}

View file

@ -10,7 +10,8 @@ export interface Aggregation {
}
export interface SearchParams {
sourceId?: string
tableId?: string
viewId?: string
query?: SearchFilters
paginate?: boolean
bookmark?: string | number
@ -29,7 +30,7 @@ export interface SearchParams {
// when searching for rows we want a more extensive search type that requires certain properties
export interface RowSearchParams
extends WithRequired<SearchParams, "sourceId" | "query"> {}
extends WithRequired<SearchParams, "tableId" | "query"> {}
export interface SearchResponse<T> {
rows: T[]