Type row endpoints
This commit is contained in:
parent
c6bad69cda
commit
2d46939fb1
|
@ -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 () => {
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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.
|
||||
* @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<DeleteRow>({
|
||||
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<DeleteRows>({
|
||||
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()
|
||||
},
|
|
@ -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 }
|
||||
|
|
|
@ -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 => {
|
||||
|
|
|
@ -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 => {
|
||||
|
|
Loading…
Reference in New Issue