2020-11-19 19:39:22 +01:00
|
|
|
import API from "./api"
|
2020-11-12 13:24:45 +01:00
|
|
|
import { enrichRows } from "./rows"
|
2020-11-12 10:07:09 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 => {
|
2021-05-26 18:38:49 +02:00
|
|
|
const res = await API.get({ url: `/api/tables/${tableId}`, cache: true })
|
|
|
|
|
|
|
|
// Wipe any HBS formulae, as these interfere with handlebars enrichment
|
|
|
|
Object.keys(res?.schema || {}).forEach(field => {
|
|
|
|
if (res.schema[field]?.type === "formula") {
|
|
|
|
delete res.schema[field].formula
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return res
|
2020-11-12 10:07:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches all rows from a table.
|
|
|
|
*/
|
2021-05-11 12:24:16 +02:00
|
|
|
export const fetchTableData = async tableId => {
|
2020-11-19 19:39:22 +01:00
|
|
|
const rows = await API.get({ url: `/api/${tableId}/rows` })
|
2020-11-12 13:24:45 +01:00
|
|
|
return await enrichRows(rows, tableId)
|
2020-11-12 10:07:09 +01:00
|
|
|
}
|
2021-02-01 22:02:54 +01:00
|
|
|
|
2021-04-30 17:29:53 +02:00
|
|
|
/**
|
|
|
|
* Searches a table using Lucene.
|
|
|
|
*/
|
|
|
|
export const searchTable = async ({
|
|
|
|
tableId,
|
|
|
|
query,
|
|
|
|
bookmark,
|
|
|
|
limit,
|
|
|
|
sort,
|
|
|
|
sortOrder,
|
2021-05-11 12:24:16 +02:00
|
|
|
sortType,
|
2021-05-13 13:26:18 +02:00
|
|
|
paginate,
|
2021-04-30 17:29:53 +02:00
|
|
|
}) => {
|
2021-05-13 17:33:19 +02:00
|
|
|
if (!tableId || !query) {
|
|
|
|
return {
|
|
|
|
rows: [],
|
|
|
|
}
|
2021-04-30 17:29:53 +02:00
|
|
|
}
|
|
|
|
const res = await API.post({
|
|
|
|
url: `/api/search/${tableId}/rows`,
|
|
|
|
body: {
|
|
|
|
query,
|
|
|
|
bookmark,
|
|
|
|
limit,
|
|
|
|
sort,
|
|
|
|
sortOrder,
|
2021-05-11 12:24:16 +02:00
|
|
|
sortType,
|
2021-05-13 13:26:18 +02:00
|
|
|
paginate,
|
2021-04-30 17:29:53 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
return {
|
2021-05-13 13:26:18 +02:00
|
|
|
...res,
|
2021-04-30 17:29:53 +02:00
|
|
|
rows: await enrichRows(res?.rows, tableId),
|
|
|
|
}
|
|
|
|
}
|