Merge pull request #12299 from Budibase/fix/export-row-data

Fix filtering/sorting of row exports
This commit is contained in:
deanhannigan 2023-11-07 15:38:34 +00:00 committed by GitHub
commit 3e3c63b1d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 8 deletions

View File

@ -254,7 +254,7 @@ export const exportRows = async (
const format = ctx.query.format const format = ctx.query.format
const { rows, columns, query } = ctx.request.body const { rows, columns, query, sort, sortOrder } = ctx.request.body
if (typeof format !== "string" || !exporters.isFormat(format)) { if (typeof format !== "string" || !exporters.isFormat(format)) {
ctx.throw( ctx.throw(
400, 400,
@ -272,6 +272,8 @@ export const exportRows = async (
rowIds: rows, rowIds: rows,
columns, columns,
query, query,
sort,
sortOrder,
}) })
ctx.attachment(fileName) ctx.attachment(fileName)
return apiFileReturn(content) return apiFileReturn(content)

View File

@ -1,4 +1,10 @@
import { Row, SearchFilters, SearchParams } from "@budibase/types" import {
Row,
SearchFilters,
SearchParams,
SortOrder,
SortType,
} from "@budibase/types"
import { isExternalTableID } from "../../../integrations/utils" import { isExternalTableID } 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"
@ -32,6 +38,8 @@ export interface ExportRowsParams {
rowIds?: string[] rowIds?: string[]
columns?: string[] columns?: string[]
query?: SearchFilters query?: SearchFilters
sort?: string
sortOrder?: SortOrder
} }
export interface ExportRowsResult { export interface ExportRowsResult {

View File

@ -98,12 +98,12 @@ export async function search(options: SearchParams) {
export async function exportRows( export async function exportRows(
options: ExportRowsParams options: ExportRowsParams
): Promise<ExportRowsResult> { ): Promise<ExportRowsResult> {
const { tableId, format, columns, rowIds } = options const { tableId, format, columns, rowIds, query, sort, sortOrder } = options
const { datasourceId, tableName } = breakExternalTableId(tableId) const { datasourceId, tableName } = breakExternalTableId(tableId)
let query: SearchFilters = {} let requestQuery: SearchFilters = {}
if (rowIds?.length) { if (rowIds?.length) {
query = { requestQuery = {
oneOf: { oneOf: {
_id: rowIds.map((row: string) => { _id: rowIds.map((row: string) => {
const ids = JSON.parse( const ids = JSON.parse(
@ -119,6 +119,8 @@ export async function exportRows(
}), }),
}, },
} }
} else {
requestQuery = query || {}
} }
const datasource = await sdk.datasources.get(datasourceId!) const datasource = await sdk.datasources.get(datasourceId!)
@ -126,7 +128,7 @@ export async function exportRows(
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 }) let result = await search({ tableId, query: requestQuery, sort, sortOrder })
let rows: Row[] = [] let rows: Row[] = []
// Filter data to only specified columns if required // Filter data to only specified columns if required

View File

@ -84,7 +84,7 @@ export async function search(options: SearchParams) {
export async function exportRows( export async function exportRows(
options: ExportRowsParams options: ExportRowsParams
): Promise<ExportRowsResult> { ): Promise<ExportRowsResult> {
const { tableId, format, rowIds, columns, query } = options const { tableId, format, rowIds, columns, query, sort, sortOrder } = options
const db = context.getAppDB() const db = context.getAppDB()
const table = await sdk.tables.getTable(tableId) const table = await sdk.tables.getTable(tableId)
@ -99,7 +99,12 @@ export async function exportRows(
result = await outputProcessing(table, response) result = await outputProcessing(table, response)
} else if (query) { } else if (query) {
let searchResponse = await search({ tableId, query }) let searchResponse = await search({
tableId,
query,
sort,
sortOrder,
})
result = searchResponse.rows result = searchResponse.rows
} }

View File

@ -1,5 +1,6 @@
import { SearchFilters, SearchParams } from "../../../sdk" import { SearchFilters, SearchParams } from "../../../sdk"
import { Row } from "../../../documents" import { Row } from "../../../documents"
import { SortOrder } from "../../../api"
import { ReadStream } from "fs" import { ReadStream } from "fs"
export interface SaveRowRequest extends Row {} export interface SaveRowRequest extends Row {}
@ -34,6 +35,8 @@ export interface ExportRowsRequest {
rows: string[] rows: string[]
columns?: string[] columns?: string[]
query?: SearchFilters query?: SearchFilters
sort?: string
sortOrder?: SortOrder
} }
export type ExportRowsResponse = ReadStream export type ExportRowsResponse = ReadStream