diff --git a/packages/server/src/api/controllers/row/index.ts b/packages/server/src/api/controllers/row/index.ts index 1a6747a085..018283c8c5 100644 --- a/packages/server/src/api/controllers/row/index.ts +++ b/packages/server/src/api/controllers/row/index.ts @@ -254,7 +254,7 @@ export const exportRows = async ( 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)) { ctx.throw( 400, @@ -272,6 +272,8 @@ export const exportRows = async ( rowIds: rows, columns, query, + sort, + sortOrder, }) ctx.attachment(fileName) return apiFileReturn(content) diff --git a/packages/server/src/sdk/app/rows/search.ts b/packages/server/src/sdk/app/rows/search.ts index 31f7c74601..36ef538d8b 100644 --- a/packages/server/src/sdk/app/rows/search.ts +++ b/packages/server/src/sdk/app/rows/search.ts @@ -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 * as internal from "./search/internal" import * as external from "./search/external" @@ -32,6 +38,8 @@ export interface ExportRowsParams { rowIds?: string[] columns?: string[] query?: SearchFilters + sort?: string + sortOrder?: SortOrder } export interface ExportRowsResult { diff --git a/packages/server/src/sdk/app/rows/search/external.ts b/packages/server/src/sdk/app/rows/search/external.ts index 981ae1bf8d..974900ba6d 100644 --- a/packages/server/src/sdk/app/rows/search/external.ts +++ b/packages/server/src/sdk/app/rows/search/external.ts @@ -98,12 +98,12 @@ export async function search(options: SearchParams) { export async function exportRows( options: ExportRowsParams ): Promise { - const { tableId, format, columns, rowIds } = options + const { tableId, format, columns, rowIds, query, sort, sortOrder } = options const { datasourceId, tableName } = breakExternalTableId(tableId) - let query: SearchFilters = {} + let requestQuery: SearchFilters = {} if (rowIds?.length) { - query = { + requestQuery = { oneOf: { _id: rowIds.map((row: string) => { const ids = JSON.parse( @@ -119,6 +119,8 @@ export async function exportRows( }), }, } + } else { + requestQuery = query || {} } 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) } - let result = await search({ tableId, query }) + let result = await search({ tableId, query: requestQuery, sort, sortOrder }) let rows: Row[] = [] // Filter data to only specified columns if required diff --git a/packages/server/src/sdk/app/rows/search/internal.ts b/packages/server/src/sdk/app/rows/search/internal.ts index 1aec8a321e..e31bda1a15 100644 --- a/packages/server/src/sdk/app/rows/search/internal.ts +++ b/packages/server/src/sdk/app/rows/search/internal.ts @@ -84,7 +84,7 @@ export async function search(options: SearchParams) { export async function exportRows( options: ExportRowsParams ): Promise { - const { tableId, format, rowIds, columns, query } = options + const { tableId, format, rowIds, columns, query, sort, sortOrder } = options const db = context.getAppDB() const table = await sdk.tables.getTable(tableId) @@ -99,7 +99,12 @@ export async function exportRows( result = await outputProcessing(table, response) } else if (query) { - let searchResponse = await search({ tableId, query }) + let searchResponse = await search({ + tableId, + query, + sort, + sortOrder, + }) result = searchResponse.rows } diff --git a/packages/types/src/api/web/app/rows.ts b/packages/types/src/api/web/app/rows.ts index 62ea90a6a4..dad3286754 100644 --- a/packages/types/src/api/web/app/rows.ts +++ b/packages/types/src/api/web/app/rows.ts @@ -1,5 +1,6 @@ import { SearchFilters, SearchParams } from "../../../sdk" import { Row } from "../../../documents" +import { SortOrder } from "../../../api" import { ReadStream } from "fs" export interface SaveRowRequest extends Row {} @@ -34,6 +35,8 @@ export interface ExportRowsRequest { rows: string[] columns?: string[] query?: SearchFilters + sort?: string + sortOrder?: SortOrder } export type ExportRowsResponse = ReadStream