Type row endpoints

This commit is contained in:
Andrew Kingston 2024-12-03 11:50:23 +00:00
parent c6bad69cda
commit 2d46939fb1
No known key found for this signature in database
6 changed files with 61 additions and 68 deletions

View File

@ -63,21 +63,15 @@
} }
const exportFilteredData = async () => { const exportFilteredData = async () => {
let payload = { let payload = {}
tableId: view,
format: exportFormat,
search: {
paginate: false,
},
}
if (selectedRows?.length) { if (selectedRows?.length) {
payload.rows = selectedRows.map(row => row._id) payload.rows = selectedRows.map(row => row._id)
} }
if (sorting) { if (sorting) {
payload.search.sort = sorting.sortColumn payload.sort = sorting.sortColumn
payload.search.sortOrder = sorting.sortOrder payload.sortOrder = sorting.sortOrder
} }
return await API.exportRows(payload) return await API.exportRows(view, exportFormat, payload)
} }
const exportData = async () => { const exportData = async () => {

View File

@ -147,7 +147,7 @@ const fetchRowHandler = async action => {
if (tableId && rowId) { if (tableId && rowId) {
try { try {
const row = await API.fetchRow({ tableId, rowId }) const row = await API.fetchRow(tableId, rowId)
return { row } return { row }
} catch (error) { } catch (error) {
@ -192,7 +192,7 @@ const deleteRowHandler = async action => {
return false return false
} }
const resp = await API.deleteRows({ tableId, rows: requestConfig }) const resp = await API.deleteRows(tableId, requestConfig)
if (!notificationOverride) { if (!notificationOverride) {
notificationStore.actions.success( notificationStore.actions.success(
@ -378,10 +378,8 @@ const exportDataHandler = async action => {
if (typeof rows[0] !== "string") { if (typeof rows[0] !== "string") {
rows = rows.map(row => row._id) rows = rows.map(row => row._id)
} }
const data = await API.exportRows({ const data = await API.exportRows(tableId, type, {
tableId,
rows, rows,
format: type,
columns: columns?.map(column => column.name || column), columns: columns?.map(column => column.name || column),
delimiter, delimiter,
customHeaders, customHeaders,

View File

@ -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<GetRowResponse>
saveRow: (
row: Row,
suppressErrors?: boolean
) => Promise<PatchRowResponse | null>
patchRow: (
row: PatchRowRequest,
suppressErrors?: boolean
) => Promise<PatchRowResponse>
deleteRow: (sourceId: string, id: string) => Promise<void>
deleteRows: (sourceId: string, rows: (Row | string)[]) => Promise<void>
exportRows: (
tableId: string,
format: string,
data: ExportRowsRequest
) => Promise<string>
}
export const buildRowEndpoints = (API: BaseAPIClient): RowEndpoints => ({
/** /**
* Fetches data about a certain row in a table. * Fetches data about a certain row in a table.
* @param tableId the ID of the table to fetch from * @param tableId the ID of the table to fetch from
* @param rowId the ID of the row to fetch * @param rowId the ID of the row to fetch
*/ */
fetchRow: async ({ tableId, rowId }) => { fetchRow: async (tableId, rowId) => {
if (!tableId || !rowId) {
return null
}
return await API.get({ return await API.get({
url: `/api/${tableId}/rows/${rowId}`, url: `/api/${tableId}/rows/${rowId}`,
}) })
@ -36,12 +64,8 @@ export const buildRowEndpoints = API => ({
* @param suppressErrors whether or not to suppress error notifications * @param suppressErrors whether or not to suppress error notifications
*/ */
patchRow: async (row, suppressErrors = false) => { patchRow: async (row, suppressErrors = false) => {
const resourceId = row?._viewId || row?.tableId
if (!resourceId) {
return
}
return await API.patch({ return await API.patch({
url: `/api/${resourceId}/rows`, url: `/api/${row.tableId}/rows`,
body: row, body: row,
suppressErrors, suppressErrors,
}) })
@ -49,34 +73,31 @@ export const buildRowEndpoints = API => ({
/** /**
* Deletes a row from a table. * 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 rowId the ID of the row to delete
* @param revId the rev of the row to delete
*/ */
deleteRow: async ({ tableId, rowId, revId }) => { deleteRow: async (sourceId, rowId) => {
if (!tableId || !rowId) { return await API.delete<DeleteRow>({
return url: `/api/${sourceId}/rows`,
}
return await API.delete({
url: `/api/${tableId}/rows`,
body: { body: {
_id: rowId, _id: rowId,
_rev: revId,
}, },
}) })
}, },
/** /**
* Deletes multiple rows from a table. * 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 * @param rows the array of rows to delete
*/ */
deleteRows: async ({ tableId, rows }) => { deleteRows: async (sourceId, rows) => {
rows?.forEach(row => { rows?.forEach((row: Row | string) => {
delete row?._viewId if (typeof row === "object") {
delete row?._viewId
}
}) })
return await API.delete({ return await API.delete<DeleteRows>({
url: `/api/${tableId}/rows`, url: `/api/${sourceId}/rows`,
body: { body: {
rows, rows,
}, },
@ -86,29 +107,13 @@ export const buildRowEndpoints = API => ({
/** /**
* Exports rows. * Exports rows.
* @param tableId the table ID to export the rows from * @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 format the format to export (csv or json)
* @param columns which columns to export (all if undefined) * @param data the export options
* @param delimiter how values should be separated in a CSV (default is comma)
*/ */
exportRows: async ({ exportRows: async (tableId, format, data) => {
tableId,
rows,
format,
columns,
search,
delimiter,
customHeaders,
}) => {
return await API.post({ return await API.post({
url: `/api/${tableId}/rows/exportRows?format=${format}`, url: `/api/${tableId}/rows/exportRows?format=${format}`,
body: { body: data,
rows,
columns,
delimiter,
customHeaders,
...search,
},
parseResponse: async response => { parseResponse: async response => {
return await response.text() return await response.text()
}, },

View File

@ -24,6 +24,7 @@ import { RelationshipEndpoints } from "./relationships"
import { RoleEndpoints } from "./roles" import { RoleEndpoints } from "./roles"
import { RouteEndpoints } from "./routes" import { RouteEndpoints } from "./routes"
import { RowActionEndpoints } from "./rowActions" import { RowActionEndpoints } from "./rowActions"
import { RowEndpoints } from "./rows"
export enum HTTPMethod { export enum HTTPMethod {
POST = "POST", POST = "POST",
@ -118,4 +119,5 @@ export type APIClient = BaseAPIClient &
QueryEndpoints & QueryEndpoints &
RelationshipEndpoints & RelationshipEndpoints &
RoleEndpoints & RoleEndpoints &
RouteEndpoints & { rowActions: RowActionEndpoints; [key: string]: any } RouteEndpoints &
RowEndpoints & { rowActions: RowActionEndpoints; [key: string]: any }

View File

@ -19,10 +19,7 @@ export const createActions = context => {
} }
const deleteRows = async rows => { const deleteRows = async rows => {
await API.deleteRows({ await API.deleteRows(get(datasource).tableId, rows)
tableId: get(datasource).tableId,
rows,
})
} }
const isDatasourceValid = datasource => { const isDatasourceValid = datasource => {

View File

@ -24,10 +24,7 @@ export const createActions = context => {
} }
const deleteRows = async rows => { const deleteRows = async rows => {
await API.deleteRows({ await API.deleteRows(get(datasource).id, rows)
tableId: get(datasource).id,
rows,
})
} }
const getRow = async id => { const getRow = async id => {