Getting search input mapping up a level in the search SDK - avoids having to call it for every search type.
This commit is contained in:
parent
c40e965634
commit
bfb7750213
|
@ -13,6 +13,8 @@ import * as sqs from "./search/sqs"
|
||||||
import env from "../../../environment"
|
import env from "../../../environment"
|
||||||
import { ExportRowsParams, ExportRowsResult } from "./search/types"
|
import { ExportRowsParams, ExportRowsResult } from "./search/types"
|
||||||
import { dataFilters } from "@budibase/shared-core"
|
import { dataFilters } from "@budibase/shared-core"
|
||||||
|
import sdk from "../../index"
|
||||||
|
import { searchInputMapping } from "./search/utils"
|
||||||
|
|
||||||
export { isValidFilter } from "../../../integrations/utils"
|
export { isValidFilter } from "../../../integrations/utils"
|
||||||
|
|
||||||
|
@ -72,12 +74,15 @@ export async function search(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const table = await sdk.tables.getTable(options.tableId)
|
||||||
|
options = searchInputMapping(table, options)
|
||||||
|
|
||||||
if (isExternalTable) {
|
if (isExternalTable) {
|
||||||
return external.search(options)
|
return external.search(options, table)
|
||||||
} else if (env.SQS_SEARCH_ENABLE) {
|
} else if (env.SQS_SEARCH_ENABLE) {
|
||||||
return sqs.search(options)
|
return sqs.search(options, table)
|
||||||
} else {
|
} else {
|
||||||
return internal.search(options)
|
return internal.search(options, table)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ import {
|
||||||
SearchFilters,
|
SearchFilters,
|
||||||
RowSearchParams,
|
RowSearchParams,
|
||||||
SearchResponse,
|
SearchResponse,
|
||||||
|
Table,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import * as exporters from "../../../../api/controllers/view/exporters"
|
import * as exporters from "../../../../api/controllers/view/exporters"
|
||||||
import { handleRequest } from "../../../../api/controllers/row/external"
|
import { handleRequest } from "../../../../api/controllers/row/external"
|
||||||
|
@ -18,13 +19,13 @@ import {
|
||||||
import { utils } from "@budibase/shared-core"
|
import { utils } from "@budibase/shared-core"
|
||||||
import { ExportRowsParams, ExportRowsResult } from "./types"
|
import { ExportRowsParams, ExportRowsResult } from "./types"
|
||||||
import { HTTPError, db } from "@budibase/backend-core"
|
import { HTTPError, db } from "@budibase/backend-core"
|
||||||
import { searchInputMapping } from "./utils"
|
|
||||||
import pick from "lodash/pick"
|
import pick from "lodash/pick"
|
||||||
import { outputProcessing } from "../../../../utilities/rowProcessor"
|
import { outputProcessing } from "../../../../utilities/rowProcessor"
|
||||||
import sdk from "../../../"
|
import sdk from "../../../"
|
||||||
|
|
||||||
export async function search(
|
export async function search(
|
||||||
options: RowSearchParams
|
options: RowSearchParams,
|
||||||
|
table: Table
|
||||||
): Promise<SearchResponse<Row>> {
|
): Promise<SearchResponse<Row>> {
|
||||||
const { tableId } = options
|
const { tableId } = options
|
||||||
const { paginate, query, ...params } = options
|
const { paginate, query, ...params } = options
|
||||||
|
@ -68,8 +69,6 @@ export async function search(
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const table = await sdk.tables.getTable(tableId)
|
|
||||||
options = searchInputMapping(table, options)
|
|
||||||
let rows = await handleRequest(Operation.READ, tableId, {
|
let rows = await handleRequest(Operation.READ, tableId, {
|
||||||
filters: query,
|
filters: query,
|
||||||
sort,
|
sort,
|
||||||
|
@ -150,11 +149,15 @@ export async function exportRows(
|
||||||
}
|
}
|
||||||
|
|
||||||
const datasource = await sdk.datasources.get(datasourceId!)
|
const datasource = await sdk.datasources.get(datasourceId!)
|
||||||
|
const table = await sdk.tables.getTable(tableId)
|
||||||
if (!datasource || !datasource.entities) {
|
if (!datasource || !datasource.entities) {
|
||||||
throw new HTTPError("Datasource has not been configured for plus API.", 400)
|
throw new HTTPError("Datasource has not been configured for plus API.", 400)
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = await search({ tableId, query: requestQuery, sort, sortOrder })
|
let result = await search(
|
||||||
|
{ tableId, query: requestQuery, sort, sortOrder },
|
||||||
|
table
|
||||||
|
)
|
||||||
let rows: Row[] = []
|
let rows: Row[] = []
|
||||||
let headers
|
let headers
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,8 @@ import pick from "lodash/pick"
|
||||||
import { breakRowIdField } from "../../../../integrations/utils"
|
import { breakRowIdField } from "../../../../integrations/utils"
|
||||||
|
|
||||||
export async function search(
|
export async function search(
|
||||||
options: RowSearchParams
|
options: RowSearchParams,
|
||||||
|
table: Table
|
||||||
): Promise<SearchResponse<Row>> {
|
): Promise<SearchResponse<Row>> {
|
||||||
const { tableId } = options
|
const { tableId } = options
|
||||||
|
|
||||||
|
@ -51,8 +52,6 @@ export async function search(
|
||||||
query: {},
|
query: {},
|
||||||
}
|
}
|
||||||
|
|
||||||
let table = await sdk.tables.getTable(tableId)
|
|
||||||
options = searchInputMapping(table, options)
|
|
||||||
if (params.sort && !params.sortType) {
|
if (params.sort && !params.sortType) {
|
||||||
const schema = table.schema
|
const schema = table.schema
|
||||||
const sortField = schema[params.sort]
|
const sortField = schema[params.sort]
|
||||||
|
@ -122,12 +121,15 @@ export async function exportRows(
|
||||||
|
|
||||||
result = await outputProcessing<Row[]>(table, response)
|
result = await outputProcessing<Row[]>(table, response)
|
||||||
} else if (query) {
|
} else if (query) {
|
||||||
let searchResponse = await search({
|
let searchResponse = await search(
|
||||||
tableId,
|
{
|
||||||
query,
|
tableId,
|
||||||
sort,
|
query,
|
||||||
sortOrder,
|
sort,
|
||||||
})
|
sortOrder,
|
||||||
|
},
|
||||||
|
table
|
||||||
|
)
|
||||||
result = searchResponse.rows
|
result = searchResponse.rows
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -112,7 +112,7 @@ describe("external search", () => {
|
||||||
tableId,
|
tableId,
|
||||||
query: {},
|
query: {},
|
||||||
}
|
}
|
||||||
const result = await search(searchParams)
|
const result = await search(searchParams, config.table!)
|
||||||
|
|
||||||
expect(result.rows).toHaveLength(10)
|
expect(result.rows).toHaveLength(10)
|
||||||
expect(result.rows).toEqual(
|
expect(result.rows).toEqual(
|
||||||
|
@ -130,7 +130,7 @@ describe("external search", () => {
|
||||||
query: {},
|
query: {},
|
||||||
fields: ["name", "age"],
|
fields: ["name", "age"],
|
||||||
}
|
}
|
||||||
const result = await search(searchParams)
|
const result = await search(searchParams, config.table!)
|
||||||
|
|
||||||
expect(result.rows).toHaveLength(10)
|
expect(result.rows).toHaveLength(10)
|
||||||
expect(result.rows).toEqual(
|
expect(result.rows).toEqual(
|
||||||
|
@ -157,7 +157,7 @@ describe("external search", () => {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
const result = await search(searchParams)
|
const result = await search(searchParams, config.table!)
|
||||||
|
|
||||||
expect(result.rows).toHaveLength(3)
|
expect(result.rows).toHaveLength(3)
|
||||||
expect(result.rows.map(row => row.id)).toEqual([1, 4, 8])
|
expect(result.rows.map(row => row.id)).toEqual([1, 4, 8])
|
||||||
|
|
|
@ -81,7 +81,7 @@ describe("internal", () => {
|
||||||
tableId,
|
tableId,
|
||||||
query: {},
|
query: {},
|
||||||
}
|
}
|
||||||
const result = await search(searchParams)
|
const result = await search(searchParams, config.table!)
|
||||||
|
|
||||||
expect(result.rows).toHaveLength(10)
|
expect(result.rows).toHaveLength(10)
|
||||||
expect(result.rows).toEqual(
|
expect(result.rows).toEqual(
|
||||||
|
@ -99,7 +99,7 @@ describe("internal", () => {
|
||||||
query: {},
|
query: {},
|
||||||
fields: ["name", "age"],
|
fields: ["name", "age"],
|
||||||
}
|
}
|
||||||
const result = await search(searchParams)
|
const result = await search(searchParams, config.table!)
|
||||||
|
|
||||||
expect(result.rows).toHaveLength(10)
|
expect(result.rows).toHaveLength(10)
|
||||||
expect(result.rows).toEqual(
|
expect(result.rows).toEqual(
|
||||||
|
|
Loading…
Reference in New Issue