Improving types around row search.

This commit is contained in:
mike12345567 2024-03-26 16:34:19 +00:00
parent 3843dc994b
commit 42214919be
7 changed files with 67 additions and 44 deletions

View File

@ -236,4 +236,4 @@ export async function fetchEnrichedRow(ctx: UserCtx) {
}
}
return row
}
}

View File

@ -1,9 +1,16 @@
import { Row, SearchFilters, SearchParams, SortOrder } from "@budibase/types"
import {
Row,
SearchFilters,
SearchParams,
SearchResponse,
} from "@budibase/types"
import { isExternalTableID } from "../../../integrations/utils"
import * as internal from "./search/internal"
import * as external from "./search/external"
import { Format } from "../../../api/controllers/view/exporters"
import { NoEmptyFilterStrings } from "../../../constants"
import * as sqs from "./search/sqs"
import env from "../../../environment"
import { ExportRowsParams, ExportRowsResult } from "./search/types"
export { isValidFilter } from "../../../integrations/utils"
@ -49,29 +56,15 @@ export function removeEmptyFilters(filters: SearchFilters) {
return filters
}
export async function search(options: SearchParams): Promise<{
rows: any[]
hasNextPage?: boolean
bookmark?: number | null
}> {
return pickApi(options.tableId).search(options)
}
export interface ExportRowsParams {
tableId: string
format: Format
delimiter?: string
rowIds?: string[]
columns?: string[]
query?: SearchFilters
sort?: string
sortOrder?: SortOrder
customHeaders?: { [key: string]: string }
}
export interface ExportRowsResult {
fileName: string
content: string
export async function search(options: SearchParams): Promise<SearchResponse> {
const isExternalTable = isExternalTableID(options.tableId)
if (isExternalTable) {
return external.search(options)
} else if (env.SQS_SEARCH_ENABLE) {
return sqs.search(options)
} else {
return internal.search(options)
}
}
export async function exportRows(

View File

@ -7,6 +7,7 @@ import {
Row,
SearchFilters,
SearchParams,
SearchResponse,
} from "@budibase/types"
import * as exporters from "../../../../api/controllers/view/exporters"
import { handleRequest } from "../../../../api/controllers/row/external"
@ -15,18 +16,18 @@ import {
breakRowIdField,
} from "../../../../integrations/utils"
import { utils } from "@budibase/shared-core"
import { ExportRowsParams, ExportRowsResult } from "../search"
import { ExportRowsParams, ExportRowsResult } from "./types"
import { HTTPError, db } from "@budibase/backend-core"
import { searchInputMapping } from "./utils"
import pick from "lodash/pick"
import { outputProcessing } from "../../../../utilities/rowProcessor"
import sdk from "../../../"
export async function search(options: SearchParams) {
export async function search(options: SearchParams): Promise<SearchResponse> {
const { tableId } = options
const { paginate, query, ...params } = options
const { limit } = params
let bookmark = (params.bookmark && parseInt(params.bookmark)) || null
let bookmark = (params.bookmark && parseInt(params.bookmark)) || undefined
if (paginate && !bookmark) {
bookmark = 1
}

View File

@ -12,6 +12,7 @@ import {
Row,
Table,
SearchParams,
SearchResponse,
DocumentType,
} from "@budibase/types"
import { getGlobalUsersFromMetadata } from "../../../../utilities/global"
@ -30,17 +31,12 @@ import {
migrateToInMemoryView,
} from "../../../../api/controllers/view/utils"
import sdk from "../../../../sdk"
import { ExportRowsParams, ExportRowsResult } from "../search"
import { ExportRowsParams, ExportRowsResult } from "./types"
import { searchInputMapping } from "./utils"
import pick from "lodash/pick"
import { breakRowIdField } from "../../../../integrations/utils"
import * as sqs from "./sqs"
export async function search(options: SearchParams) {
if (env.SQS_SEARCH_ENABLE) {
return sqs.search(options)
}
export async function search(options: SearchParams): Promise<SearchResponse> {
const { tableId } = options
const { paginate, query } = options
@ -106,10 +102,10 @@ export async function exportRows(
const db = context.getAppDB()
const table = await sdk.tables.getTable(tableId)
let result
let result: Row[] = []
if (rowIds) {
let response = (
await db.allDocs({
await db.allDocs<Row>({
include_docs: true,
keys: rowIds.map((row: string) => {
const ids = breakRowIdField(row)
@ -122,9 +118,9 @@ export async function exportRows(
return ids[0]
}),
})
).rows.map(row => row.doc)
).rows.map(row => row.doc!)
result = await outputProcessing(table, response)
result = await outputProcessing<Row[]>(table, response)
} else if (query) {
let searchResponse = await search({
tableId,

View File

@ -6,6 +6,7 @@ import {
Row,
SearchFilters,
SearchParams,
SearchResponse,
SortDirection,
SortOrder,
SortType,
@ -92,7 +93,7 @@ function buildTableMap(tables: Table[]) {
return tableMap
}
export async function search(options: SearchParams) {
export async function search(options: SearchParams): Promise<SearchResponse> {
const { tableId, paginate, query, ...params } = options
const builder = new SqlQueryBuilder(SqlClient.SQL_LITE)
@ -164,9 +165,15 @@ export async function search(options: SearchParams) {
const rows = await db.sql<Row>(sql)
return {
rows: sqlOutputProcessing(rows, table!, allTablesMap, relationships, {
internal: true,
}),
rows: await sqlOutputProcessing(
rows,
table!,
allTablesMap,
relationships,
{
internal: true,
}
),
}
} catch (err: any) {
const msg = typeof err === "string" ? err : err.message

View File

@ -0,0 +1,19 @@
import { Format } from "../../../../api/controllers/view/exporters"
import { SearchFilters, SortOrder } from "@budibase/types"
export interface ExportRowsParams {
tableId: string
format: Format
delimiter?: string
rowIds?: string[]
columns?: string[]
query?: SearchFilters
sort?: string
sortOrder?: SortOrder
customHeaders?: { [key: string]: string }
}
export interface ExportRowsResult {
fileName: string
content: string
}

View File

@ -1,5 +1,6 @@
import { SortOrder, SortType } from "../api"
import { SearchFilters } from "./search"
import { Row } from "../documents"
export interface SearchParams {
tableId: string
@ -14,3 +15,9 @@ export interface SearchParams {
disableEscaping?: boolean
fields?: string[]
}
export interface SearchResponse {
rows: Row[]
hasNextPage?: boolean
bookmark?: string | number
}