diff --git a/packages/backend-core/src/db/lucene.ts b/packages/backend-core/src/db/lucene.ts index 7f58c7068e..0206bb2140 100644 --- a/packages/backend-core/src/db/lucene.ts +++ b/packages/backend-core/src/db/lucene.ts @@ -79,8 +79,8 @@ export class QueryBuilder { 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( .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( 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( // 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() diff --git a/packages/backend-core/src/db/tests/lucene.spec.ts b/packages/backend-core/src/db/tests/lucene.spec.ts index 8747f56a4b..c41bdf88d1 100644 --- a/packages/backend-core/src/db/tests/lucene.spec.ts +++ b/packages/backend-core/src/db/tests/lucene.spec.ts @@ -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: {}, } ) diff --git a/packages/server/src/api/controllers/row/index.ts b/packages/server/src/api/controllers/row/index.ts index 84b5edcc12..680a7671d5 100644 --- a/packages/server/src/api/controllers/row/index.ts +++ b/packages/server/src/api/controllers/row/index.ts @@ -221,7 +221,8 @@ export async function search(ctx: Ctx) { const searchParams: RowSearchParams = { ...ctx.request.body, query: enrichedQuery, - sourceId: viewId || tableId, + tableId, + viewId, } ctx.status = 200 diff --git a/packages/server/src/api/controllers/row/views.ts b/packages/server/src/api/controllers/row/views.ts index 7008c5e0be..fa75990136 100644 --- a/packages/server/src/api/controllers/row/views.ts +++ b/packages/server/src/api/controllers/row/views.ts @@ -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 & - RequiredKeys> = { - 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 } diff --git a/packages/server/src/api/routes/tests/search.spec.ts b/packages/server/src/api/routes/tests/search.spec.ts index 5788d9195a..090514250d 100644 --- a/packages/server/src/api/routes/tests/search.spec.ts +++ b/packages/server/src/api/routes/tests/search.spec.ts @@ -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) { - return new SearchAssertion({ ...query, sourceId: table._id! }) + function expectSearch(query: Omit) { + 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, diff --git a/packages/server/src/api/routes/tests/templates.spec.ts b/packages/server/src/api/routes/tests/templates.spec.ts index 4290b4386f..6f4d468a68 100644 --- a/packages/server/src/api/routes/tests/templates.spec.ts +++ b/packages/server/src/api/routes/tests/templates.spec.ts @@ -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: {}, }) diff --git a/packages/server/src/integrations/tests/googlesheets.spec.ts b/packages/server/src/integrations/tests/googlesheets.spec.ts index 91addf8a50..34be1c0c6c 100644 --- a/packages/server/src/integrations/tests/googlesheets.spec.ts +++ b/packages/server/src/integrations/tests/googlesheets.spec.ts @@ -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, diff --git a/packages/server/src/sdk/app/rows/search.ts b/packages/server/src/sdk/app/rows/search.ts index fa01a6cf13..c1685d8024 100644 --- a/packages/server/src/sdk/app/rows/search.ts +++ b/packages/server/src/sdk/app/rows/search.ts @@ -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> { 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) { diff --git a/packages/server/src/sdk/app/rows/search/external.ts b/packages/server/src/sdk/app/rows/search/external.ts index a41ae8dcda..925b472a48 100644 --- a/packages/server/src/sdk/app/rows/search/external.ts +++ b/packages/server/src/sdk/app/rows/search/external.ts @@ -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[] = [] diff --git a/packages/server/src/sdk/app/rows/search/internal/internal.ts b/packages/server/src/sdk/app/rows/search/internal/internal.ts index c9e2aba237..6617fc376c 100644 --- a/packages/server/src/sdk/app/rows/search/internal/internal.ts +++ b/packages/server/src/sdk/app/rows/search/internal/internal.ts @@ -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, diff --git a/packages/server/src/sdk/app/rows/search/internal/lucene.ts b/packages/server/src/sdk/app/rows/search/internal/lucene.ts index 65cf4053d7..694c660625 100644 --- a/packages/server/src/sdk/app/rows/search/internal/lucene.ts +++ b/packages/server/src/sdk/app/rows/search/internal/lucene.ts @@ -19,8 +19,6 @@ export async function search( options: RowSearchParams, source: Table | ViewV2 ): Promise> { - 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[]) } diff --git a/packages/server/src/sdk/app/rows/search/tests/search.spec.ts b/packages/server/src/sdk/app/rows/search/tests/search.spec.ts index 194f2dd4e3..e7fd095865 100644 --- a/packages/server/src/sdk/app/rows/search/tests/search.spec.ts +++ b/packages/server/src/sdk/app/rows/search/tests/search.spec.ts @@ -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: [ diff --git a/packages/server/src/sdk/app/rows/search/tests/utils.spec.ts b/packages/server/src/sdk/app/rows/search/tests/utils.spec.ts index e3f241f15a..daa658455b 100644 --- a/packages/server/src/sdk/app/rows/search/tests/utils.spec.ts +++ b/packages/server/src/sdk/app/rows/search/tests/utils.spec.ts @@ -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() }) } diff --git a/packages/types/src/sdk/row.ts b/packages/types/src/sdk/row.ts index 2b6ff3a6c6..f81d56c082 100644 --- a/packages/types/src/sdk/row.ts +++ b/packages/types/src/sdk/row.ts @@ -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 {} + extends WithRequired {} export interface SearchResponse { rows: T[]