Remove context from search
This commit is contained in:
parent
90bf4655ea
commit
ccb5143383
|
@ -134,9 +134,14 @@ export async function destroy(ctx: any) {
|
||||||
|
|
||||||
export async function search(ctx: any) {
|
export async function search(ctx: any) {
|
||||||
const tableId = utils.getTableId(ctx)
|
const tableId = utils.getTableId(ctx)
|
||||||
ctx.status = 200
|
|
||||||
|
|
||||||
ctx.body = await quotas.addQuery(() => sdk.rows.search(tableId, ctx), {
|
const searchParams = {
|
||||||
|
...ctx.request.body,
|
||||||
|
tableId,
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.status = 200
|
||||||
|
ctx.body = await quotas.addQuery(() => sdk.rows.search(searchParams), {
|
||||||
datasourceId: tableId,
|
datasourceId: tableId,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { Ctx, SearchFilters } from "@budibase/types"
|
import { SearchFilters } from "@budibase/types"
|
||||||
import { isExternalTable } from "../../../integrations/utils"
|
import { isExternalTable } from "../../../integrations/utils"
|
||||||
import * as internal from "./search/internal"
|
import * as internal from "./search/internal"
|
||||||
import * as external from "./search/external"
|
import * as external from "./search/external"
|
||||||
|
@ -6,13 +6,15 @@ import { Format } from "../../../api/controllers/view/exporters"
|
||||||
|
|
||||||
export interface SearchParams {
|
export interface SearchParams {
|
||||||
tableId: string
|
tableId: string
|
||||||
paginate: boolean
|
paginate?: boolean
|
||||||
query?: SearchFilters
|
query: SearchFilters
|
||||||
bookmark?: number
|
bookmark?: string
|
||||||
limit: number
|
limit?: number
|
||||||
sort?: string
|
sort?: string
|
||||||
sortOrder?: string
|
sortOrder?: string
|
||||||
sortType?: string
|
sortType?: string
|
||||||
|
version?: string
|
||||||
|
disableEscaping?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ViewParams {
|
export interface ViewParams {
|
||||||
|
@ -28,8 +30,8 @@ function pickApi(tableId: any) {
|
||||||
return internal
|
return internal
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function search(tableId: string, ctx: Ctx) {
|
export async function search(options: SearchParams) {
|
||||||
return pickApi(tableId).search(ctx)
|
return pickApi(options.tableId).search(options)
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ExportRowsParams {
|
export interface ExportRowsParams {
|
||||||
|
@ -37,7 +39,7 @@ export interface ExportRowsParams {
|
||||||
format: Format
|
format: Format
|
||||||
rowIds: string[]
|
rowIds: string[]
|
||||||
columns: string[]
|
columns: string[]
|
||||||
query: string
|
query: SearchFilters
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ExportRowsResult {
|
export interface ExportRowsResult {
|
||||||
|
|
|
@ -5,22 +5,23 @@ import {
|
||||||
PaginationJson,
|
PaginationJson,
|
||||||
IncludeRelationship,
|
IncludeRelationship,
|
||||||
Row,
|
Row,
|
||||||
Ctx,
|
SearchFilters,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import * as exporters from "../../../../api/controllers/view/exporters"
|
import * as exporters from "../../../../api/controllers/view/exporters"
|
||||||
import sdk from "../../../../sdk"
|
import sdk from "../../../../sdk"
|
||||||
import { handleRequest } from "../../../../api/controllers/row/external"
|
import { handleRequest } from "../../../../api/controllers/row/external"
|
||||||
import { breakExternalTableId } from "../../../../integrations/utils"
|
import { breakExternalTableId } from "../../../../integrations/utils"
|
||||||
import { cleanExportRows } from "../utils"
|
import { cleanExportRows } from "../utils"
|
||||||
import { apiFileReturn } from "../../../../utilities/fileSystem"
|
|
||||||
import { utils } from "@budibase/shared-core"
|
import { utils } from "@budibase/shared-core"
|
||||||
import { ExportRowsParams, ExportRowsResult } from "../search"
|
import { ExportRowsParams, ExportRowsResult, SearchParams } from "../search"
|
||||||
|
import { HTTPError } from "@budibase/backend-core"
|
||||||
|
|
||||||
export async function search(ctx: Ctx) {
|
export async function search(options: SearchParams) {
|
||||||
const tableId = ctx.params.tableId
|
const { tableId } = options
|
||||||
const { paginate, query, ...params } = ctx.request.body
|
const { paginate, query, ...params } = options
|
||||||
let { bookmark, limit } = params
|
const { limit } = params
|
||||||
if (!bookmark && paginate) {
|
let bookmark = (params.bookmark && parseInt(params.bookmark)) || undefined
|
||||||
|
if (paginate && bookmark) {
|
||||||
bookmark = 1
|
bookmark = 1
|
||||||
}
|
}
|
||||||
let paginateObj = {}
|
let paginateObj = {}
|
||||||
|
@ -60,14 +61,14 @@ export async function search(ctx: Ctx) {
|
||||||
sort,
|
sort,
|
||||||
paginate: {
|
paginate: {
|
||||||
limit: 1,
|
limit: 1,
|
||||||
page: bookmark * limit + 1,
|
page: bookmark! * limit + 1,
|
||||||
},
|
},
|
||||||
includeSqlRelationships: IncludeRelationship.INCLUDE,
|
includeSqlRelationships: IncludeRelationship.INCLUDE,
|
||||||
})) as Row[]
|
})) as Row[]
|
||||||
hasNextPage = nextRows.length > 0
|
hasNextPage = nextRows.length > 0
|
||||||
}
|
}
|
||||||
// need wrapper object for bookmarks etc when paginating
|
// need wrapper object for bookmarks etc when paginating
|
||||||
return { rows, hasNextPage, bookmark: bookmark + 1 }
|
return { rows, hasNextPage, bookmark: (bookmark || 0) + 1 }
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
if (err.message && err.message.includes("does not exist")) {
|
if (err.message && err.message.includes("does not exist")) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
|
@ -87,31 +88,30 @@ export async function exportRows(
|
||||||
|
|
||||||
const datasource = await sdk.datasources.get(datasourceId!)
|
const datasource = await sdk.datasources.get(datasourceId!)
|
||||||
if (!datasource || !datasource.entities) {
|
if (!datasource || !datasource.entities) {
|
||||||
throw ctx.throw(400, "Datasource has not been configured for plus API.")
|
throw new HTTPError("Datasource has not been configured for plus API.", 400)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let query: SearchFilters = {}
|
||||||
if (rowIds?.length) {
|
if (rowIds?.length) {
|
||||||
ctx.request.body = {
|
query = {
|
||||||
query: {
|
oneOf: {
|
||||||
oneOf: {
|
_id: rowIds.map((row: string) => {
|
||||||
_id: rowIds.map((row: string) => {
|
const ids = JSON.parse(
|
||||||
const ids = JSON.parse(
|
decodeURI(row).replace(/'/g, `"`).replace(/%2C/g, ",")
|
||||||
decodeURI(row).replace(/'/g, `"`).replace(/%2C/g, ",")
|
)
|
||||||
|
if (ids.length > 1) {
|
||||||
|
throw new HTTPError(
|
||||||
|
"Export data does not support composite keys.",
|
||||||
|
400
|
||||||
)
|
)
|
||||||
if (ids.length > 1) {
|
}
|
||||||
throw ctx.throw(
|
return ids[0]
|
||||||
400,
|
}),
|
||||||
"Export data does not support composite keys."
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return ids[0]
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = await search(ctx)
|
let result = await search({ tableId, query })
|
||||||
let rows: Row[] = []
|
let rows: Row[] = []
|
||||||
|
|
||||||
// Filter data to only specified columns if required
|
// Filter data to only specified columns if required
|
||||||
|
@ -128,7 +128,7 @@ export async function exportRows(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!tableName) {
|
if (!tableName) {
|
||||||
throw ctx.throw(400, "Could not find table name.")
|
throw new HTTPError("Could not find table name.", 400)
|
||||||
}
|
}
|
||||||
const schema = datasource.entities[tableName].schema
|
const schema = datasource.entities[tableName].schema
|
||||||
let exportRows = cleanExportRows(rows, schema, format, columns)
|
let exportRows = cleanExportRows(rows, schema, format, columns)
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
import { context } from "@budibase/backend-core"
|
import {
|
||||||
|
context,
|
||||||
|
SearchParams as InternalSearchParams,
|
||||||
|
} from "@budibase/backend-core"
|
||||||
import env from "../../../../environment"
|
import env from "../../../../environment"
|
||||||
import { fullSearch, paginatedSearch } from "./internalSearch"
|
import { fullSearch, paginatedSearch } from "./internalSearch"
|
||||||
import {
|
import {
|
||||||
|
@ -8,7 +11,7 @@ import {
|
||||||
} from "../../../../db/utils"
|
} from "../../../../db/utils"
|
||||||
import { getGlobalUsersFromMetadata } from "../../../../utilities/global"
|
import { getGlobalUsersFromMetadata } from "../../../../utilities/global"
|
||||||
import { outputProcessing } from "../../../../utilities/rowProcessor"
|
import { outputProcessing } from "../../../../utilities/rowProcessor"
|
||||||
import { Ctx, Database, Row } from "@budibase/types"
|
import { Database, Row } from "@budibase/types"
|
||||||
import { cleanExportRows } from "../utils"
|
import { cleanExportRows } from "../utils"
|
||||||
import {
|
import {
|
||||||
Format,
|
Format,
|
||||||
|
@ -16,7 +19,6 @@ import {
|
||||||
json,
|
json,
|
||||||
jsonWithSchema,
|
jsonWithSchema,
|
||||||
} from "../../../../api/controllers/view/exporters"
|
} from "../../../../api/controllers/view/exporters"
|
||||||
import { apiFileReturn } from "../../../../utilities/fileSystem"
|
|
||||||
import * as inMemoryViews from "../../../../db/inMemoryView"
|
import * as inMemoryViews from "../../../../db/inMemoryView"
|
||||||
import {
|
import {
|
||||||
migrateToInMemoryView,
|
migrateToInMemoryView,
|
||||||
|
@ -25,10 +27,10 @@ import {
|
||||||
getFromMemoryDoc,
|
getFromMemoryDoc,
|
||||||
} from "../../../../api/controllers/view/utils"
|
} from "../../../../api/controllers/view/utils"
|
||||||
import sdk from "../../../../sdk"
|
import sdk from "../../../../sdk"
|
||||||
import { ExportRowsParams, ExportRowsResult } from "../search"
|
import { ExportRowsParams, ExportRowsResult, SearchParams } from "../search"
|
||||||
|
|
||||||
export async function search(ctx: Ctx) {
|
export async function search(options: SearchParams) {
|
||||||
const { tableId } = ctx.params
|
const { tableId } = options
|
||||||
|
|
||||||
// Fetch the whole table when running in cypress, as search doesn't work
|
// Fetch the whole table when running in cypress, as search doesn't work
|
||||||
if (!env.COUCH_DB_URL && env.isCypress()) {
|
if (!env.COUCH_DB_URL && env.isCypress()) {
|
||||||
|
@ -36,16 +38,25 @@ export async function search(ctx: Ctx) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
const { paginate, query, ...params } = ctx.request.body
|
const { paginate, query } = options
|
||||||
params.version = ctx.version
|
|
||||||
params.tableId = tableId
|
const params: InternalSearchParams<any> = {
|
||||||
|
tableId: options.tableId,
|
||||||
|
sort: options.sort,
|
||||||
|
sortOrder: options.sortOrder,
|
||||||
|
sortType: options.sortType,
|
||||||
|
limit: options.limit,
|
||||||
|
bookmark: options.bookmark,
|
||||||
|
version: options.version,
|
||||||
|
disableEscaping: options.disableEscaping,
|
||||||
|
}
|
||||||
|
|
||||||
let table
|
let table
|
||||||
if (params.sort && !params.sortType) {
|
if (params.sort && !params.sortType) {
|
||||||
table = await db.get(tableId)
|
table = await db.get(tableId)
|
||||||
const schema = table.schema
|
const schema = table.schema
|
||||||
const sortField = schema[params.sort]
|
const sortField = schema[params.sort]
|
||||||
params.sortType = sortField.type == "number" ? "number" : "string"
|
params.sortType = sortField.type === "number" ? "number" : "string"
|
||||||
}
|
}
|
||||||
|
|
||||||
let response
|
let response
|
||||||
|
@ -86,7 +97,7 @@ export async function exportRows(
|
||||||
|
|
||||||
result = await outputProcessing(table, response)
|
result = await outputProcessing(table, response)
|
||||||
} else if (query) {
|
} else if (query) {
|
||||||
let searchResponse = await search(ctx)
|
let searchResponse = await search({ tableId, query })
|
||||||
result = searchResponse.rows
|
result = searchResponse.rows
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue