budibase/packages/client/src/api/tables.js

71 lines
1.5 KiB
JavaScript
Raw Normal View History

import API from "./api"
import { enrichRows } from "./rows"
/**
* Fetches a table definition.
* Since definitions cannot change at runtime, the result is cached.
*/
2021-05-11 12:24:16 +02:00
export const fetchTableDefinition = async tableId => {
return await API.get({ url: `/api/tables/${tableId}`, cache: true })
}
/**
* Fetches all rows from a table.
*/
2021-05-11 12:24:16 +02:00
export const fetchTableData = async tableId => {
const rows = await API.get({ url: `/api/${tableId}/rows` })
return await enrichRows(rows, tableId)
}
2021-02-01 22:02:54 +01:00
/**
* Perform a mango query against an internal table
* @param {String} tableId - id of the table to search
* @param {Object} search - Mango Compliant search object
* @param {Object} pagination - the pagination controls
2021-02-01 22:02:54 +01:00
*/
export const searchTableData = async ({ tableId, search, pagination }) => {
const output = await API.post({
2021-02-01 22:02:54 +01:00
url: `/api/${tableId}/rows/search`,
body: {
query: search,
pagination,
2021-02-01 22:02:54 +01:00
},
})
output.rows = await enrichRows(output.rows, tableId)
return output
2021-02-01 22:02:54 +01:00
}
/**
* Searches a table using Lucene.
*/
export const searchTable = async ({
tableId,
query,
raw,
bookmark,
limit,
sort,
sortOrder,
2021-05-11 12:24:16 +02:00
sortType,
}) => {
if (!tableId || (!query && !raw)) {
return
}
const res = await API.post({
url: `/api/search/${tableId}/rows`,
body: {
query,
raw,
bookmark,
limit,
sort,
sortOrder,
2021-05-11 12:24:16 +02:00
sortType,
},
})
return {
rows: await enrichRows(res?.rows, tableId),
bookmark: res.bookmark,
}
}