diff --git a/packages/builder/src/components/backend/DataTable/buttons/ExportButton.svelte b/packages/builder/src/components/backend/DataTable/buttons/ExportButton.svelte index e9352045ea..a43629a5a9 100644 --- a/packages/builder/src/components/backend/DataTable/buttons/ExportButton.svelte +++ b/packages/builder/src/components/backend/DataTable/buttons/ExportButton.svelte @@ -63,21 +63,15 @@ } const exportFilteredData = async () => { - let payload = { - tableId: view, - format: exportFormat, - search: { - paginate: false, - }, - } + let payload = {} if (selectedRows?.length) { payload.rows = selectedRows.map(row => row._id) } if (sorting) { - payload.search.sort = sorting.sortColumn - payload.search.sortOrder = sorting.sortOrder + payload.sort = sorting.sortColumn + payload.sortOrder = sorting.sortOrder } - return await API.exportRows(payload) + return await API.exportRows(view, exportFormat, payload) } const exportData = async () => { diff --git a/packages/client/src/utils/buttonActions.js b/packages/client/src/utils/buttonActions.js index 010e1fea43..9d0bddcc92 100644 --- a/packages/client/src/utils/buttonActions.js +++ b/packages/client/src/utils/buttonActions.js @@ -147,7 +147,7 @@ const fetchRowHandler = async action => { if (tableId && rowId) { try { - const row = await API.fetchRow({ tableId, rowId }) + const row = await API.fetchRow(tableId, rowId) return { row } } catch (error) { @@ -192,7 +192,7 @@ const deleteRowHandler = async action => { return false } - const resp = await API.deleteRows({ tableId, rows: requestConfig }) + const resp = await API.deleteRows(tableId, requestConfig) if (!notificationOverride) { notificationStore.actions.success( @@ -378,10 +378,8 @@ const exportDataHandler = async action => { if (typeof rows[0] !== "string") { rows = rows.map(row => row._id) } - const data = await API.exportRows({ - tableId, + const data = await API.exportRows(tableId, type, { rows, - format: type, columns: columns?.map(column => column.name || column), delimiter, customHeaders, diff --git a/packages/frontend-core/src/api/rows.js b/packages/frontend-core/src/api/rows.ts similarity index 51% rename from packages/frontend-core/src/api/rows.js rename to packages/frontend-core/src/api/rows.ts index 0a0d48da43..7c61a05246 100644 --- a/packages/frontend-core/src/api/rows.js +++ b/packages/frontend-core/src/api/rows.ts @@ -1,13 +1,41 @@ -export const buildRowEndpoints = API => ({ +import { + DeleteRow, + DeleteRows, + ExportRowsRequest, + ExportRowsResponse, + GetRowResponse, + PatchRowRequest, + PatchRowResponse, + Row, +} from "@budibase/types" +import { BaseAPIClient } from "./types" + +export interface RowEndpoints { + fetchRow: (tableId: string, rowId: string) => Promise + saveRow: ( + row: Row, + suppressErrors?: boolean + ) => Promise + patchRow: ( + row: PatchRowRequest, + suppressErrors?: boolean + ) => Promise + deleteRow: (sourceId: string, id: string) => Promise + deleteRows: (sourceId: string, rows: (Row | string)[]) => Promise + exportRows: ( + tableId: string, + format: string, + data: ExportRowsRequest + ) => Promise +} + +export const buildRowEndpoints = (API: BaseAPIClient): RowEndpoints => ({ /** * Fetches data about a certain row in a table. * @param tableId the ID of the table to fetch from * @param rowId the ID of the row to fetch */ - fetchRow: async ({ tableId, rowId }) => { - if (!tableId || !rowId) { - return null - } + fetchRow: async (tableId, rowId) => { return await API.get({ url: `/api/${tableId}/rows/${rowId}`, }) @@ -36,12 +64,8 @@ export const buildRowEndpoints = API => ({ * @param suppressErrors whether or not to suppress error notifications */ patchRow: async (row, suppressErrors = false) => { - const resourceId = row?._viewId || row?.tableId - if (!resourceId) { - return - } return await API.patch({ - url: `/api/${resourceId}/rows`, + url: `/api/${row.tableId}/rows`, body: row, suppressErrors, }) @@ -49,34 +73,31 @@ export const buildRowEndpoints = API => ({ /** * Deletes a row from a table. - * @param tableId the ID of the table or view to delete from + * @param sourceId the ID of the table or view to delete from * @param rowId the ID of the row to delete - * @param revId the rev of the row to delete */ - deleteRow: async ({ tableId, rowId, revId }) => { - if (!tableId || !rowId) { - return - } - return await API.delete({ - url: `/api/${tableId}/rows`, + deleteRow: async (sourceId, rowId) => { + return await API.delete({ + url: `/api/${sourceId}/rows`, body: { _id: rowId, - _rev: revId, }, }) }, /** * Deletes multiple rows from a table. - * @param tableId the table or view ID to delete the rows from + * @param sourceId the table or view ID to delete the rows from * @param rows the array of rows to delete */ - deleteRows: async ({ tableId, rows }) => { - rows?.forEach(row => { - delete row?._viewId + deleteRows: async (sourceId, rows) => { + rows?.forEach((row: Row | string) => { + if (typeof row === "object") { + delete row?._viewId + } }) - return await API.delete({ - url: `/api/${tableId}/rows`, + return await API.delete({ + url: `/api/${sourceId}/rows`, body: { rows, }, @@ -86,29 +107,13 @@ export const buildRowEndpoints = API => ({ /** * Exports rows. * @param tableId the table ID to export the rows from - * @param rows the array of rows to export * @param format the format to export (csv or json) - * @param columns which columns to export (all if undefined) - * @param delimiter how values should be separated in a CSV (default is comma) + * @param data the export options */ - exportRows: async ({ - tableId, - rows, - format, - columns, - search, - delimiter, - customHeaders, - }) => { + exportRows: async (tableId, format, data) => { return await API.post({ url: `/api/${tableId}/rows/exportRows?format=${format}`, - body: { - rows, - columns, - delimiter, - customHeaders, - ...search, - }, + body: data, parseResponse: async response => { return await response.text() }, diff --git a/packages/frontend-core/src/api/types.ts b/packages/frontend-core/src/api/types.ts index a2604a86fb..559bb6adfa 100644 --- a/packages/frontend-core/src/api/types.ts +++ b/packages/frontend-core/src/api/types.ts @@ -24,6 +24,7 @@ import { RelationshipEndpoints } from "./relationships" import { RoleEndpoints } from "./roles" import { RouteEndpoints } from "./routes" import { RowActionEndpoints } from "./rowActions" +import { RowEndpoints } from "./rows" export enum HTTPMethod { POST = "POST", @@ -118,4 +119,5 @@ export type APIClient = BaseAPIClient & QueryEndpoints & RelationshipEndpoints & RoleEndpoints & - RouteEndpoints & { rowActions: RowActionEndpoints; [key: string]: any } + RouteEndpoints & + RowEndpoints & { rowActions: RowActionEndpoints; [key: string]: any } diff --git a/packages/frontend-core/src/components/grid/stores/datasources/table.js b/packages/frontend-core/src/components/grid/stores/datasources/table.js index e415f5914b..847c4f00b7 100644 --- a/packages/frontend-core/src/components/grid/stores/datasources/table.js +++ b/packages/frontend-core/src/components/grid/stores/datasources/table.js @@ -19,10 +19,7 @@ export const createActions = context => { } const deleteRows = async rows => { - await API.deleteRows({ - tableId: get(datasource).tableId, - rows, - }) + await API.deleteRows(get(datasource).tableId, rows) } const isDatasourceValid = datasource => { diff --git a/packages/frontend-core/src/components/grid/stores/datasources/viewV2.js b/packages/frontend-core/src/components/grid/stores/datasources/viewV2.js index 4a4a91658c..4df707d1c8 100644 --- a/packages/frontend-core/src/components/grid/stores/datasources/viewV2.js +++ b/packages/frontend-core/src/components/grid/stores/datasources/viewV2.js @@ -24,10 +24,7 @@ export const createActions = context => { } const deleteRows = async rows => { - await API.deleteRows({ - tableId: get(datasource).id, - rows, - }) + await API.deleteRows(get(datasource).id, rows) } const getRow = async id => {