2020-08-24 17:26:00 +02:00
|
|
|
import api from "./api"
|
|
|
|
|
2020-10-12 15:21:15 +02:00
|
|
|
export default async function fetchData(datasource, store) {
|
2020-10-09 13:24:18 +02:00
|
|
|
const { type, name } = datasource
|
2020-08-24 17:26:00 +02:00
|
|
|
|
|
|
|
if (name) {
|
2020-10-13 10:58:08 +02:00
|
|
|
let rows = []
|
|
|
|
if (type === "table") {
|
2020-10-20 13:23:52 +02:00
|
|
|
rows = await fetchTableData()
|
2020-10-09 13:24:18 +02:00
|
|
|
} else if (type === "view") {
|
2020-10-20 13:23:52 +02:00
|
|
|
rows = await fetchViewData()
|
2020-10-09 13:24:18 +02:00
|
|
|
} else if (type === "link") {
|
2020-10-20 13:23:52 +02:00
|
|
|
rows = await fetchLinkedRowsData()
|
2020-10-09 13:24:18 +02:00
|
|
|
}
|
2020-10-06 19:01:23 +02:00
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
// Fetch table schema so we can check for linked rows
|
2020-11-04 16:56:57 +01:00
|
|
|
if (rows && rows.length && datasource.tableId) {
|
2020-11-04 13:43:56 +01:00
|
|
|
const schema = await fetchSchema(datasource.tableId)
|
|
|
|
const keys = Object.keys(schema)
|
2020-10-09 20:10:28 +02:00
|
|
|
rows.forEach(row => {
|
2020-10-06 19:01:23 +02:00
|
|
|
for (let key of keys) {
|
2020-11-04 13:43:56 +01:00
|
|
|
const type = schema[key].type
|
2020-10-20 13:23:52 +02:00
|
|
|
if (type === "link") {
|
2020-10-13 11:04:29 +02:00
|
|
|
row[`${key}_count`] = Array.isArray(row[key]) ? row[key].length : 0
|
2020-10-20 13:23:52 +02:00
|
|
|
} else if (type === "attachment") {
|
|
|
|
let url = null
|
|
|
|
if (Array.isArray(row[key]) && row[key][0] != null) {
|
|
|
|
url = row[key][0].url
|
|
|
|
}
|
|
|
|
row[`${key}_first`] = url
|
2020-10-06 19:01:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
return rows
|
2020-10-13 20:58:36 +02:00
|
|
|
} else {
|
|
|
|
return []
|
2020-10-06 19:01:23 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 19:49:23 +02:00
|
|
|
async function fetchTableData() {
|
2020-08-24 17:26:00 +02:00
|
|
|
if (!name.startsWith("all_")) {
|
2020-10-09 19:49:23 +02:00
|
|
|
throw new Error("Incorrect table convention - must begin with all_")
|
2020-08-24 17:26:00 +02:00
|
|
|
}
|
2020-10-09 19:49:23 +02:00
|
|
|
const tablesResponse = await api.get(`/api/views/${name}`)
|
|
|
|
return await tablesResponse.json()
|
2020-08-24 17:26:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchViewData() {
|
2020-10-16 16:26:08 +02:00
|
|
|
const { field, groupBy, calculation } = datasource
|
2020-08-24 17:26:00 +02:00
|
|
|
const params = new URLSearchParams()
|
|
|
|
|
2020-10-16 16:26:08 +02:00
|
|
|
if (calculation) {
|
2020-09-02 12:52:32 +02:00
|
|
|
params.set("field", field)
|
2020-10-16 16:26:08 +02:00
|
|
|
params.set("calculation", calculation)
|
2020-09-02 12:52:32 +02:00
|
|
|
}
|
2020-10-16 16:26:08 +02:00
|
|
|
|
|
|
|
if (groupBy) {
|
|
|
|
params.set("group", groupBy)
|
|
|
|
}
|
|
|
|
|
2020-08-24 17:26:00 +02:00
|
|
|
if (groupBy) params.set("group", groupBy)
|
2020-09-02 12:52:32 +02:00
|
|
|
|
|
|
|
let QUERY_VIEW_URL = field
|
|
|
|
? `/api/views/${name}?${params}`
|
|
|
|
: `/api/views/${name}`
|
2020-08-24 17:26:00 +02:00
|
|
|
|
|
|
|
const response = await api.get(QUERY_VIEW_URL)
|
|
|
|
return await response.json()
|
|
|
|
}
|
2020-10-09 13:24:18 +02:00
|
|
|
|
2020-10-13 10:58:08 +02:00
|
|
|
async function fetchLinkedRowsData() {
|
2020-10-12 15:21:15 +02:00
|
|
|
if (!store || !store.data || !store.data._id) {
|
2020-10-09 13:24:18 +02:00
|
|
|
return []
|
|
|
|
}
|
2020-10-13 10:58:08 +02:00
|
|
|
const QUERY_URL = `/api/${store.data.tableId}/${store.data._id}/enrich`
|
2020-10-09 13:24:18 +02:00
|
|
|
const response = await api.get(QUERY_URL)
|
2020-10-13 10:58:08 +02:00
|
|
|
const row = await response.json()
|
|
|
|
return row[datasource.fieldName]
|
2020-10-09 13:24:18 +02:00
|
|
|
}
|
2020-08-24 17:26:00 +02:00
|
|
|
}
|
2020-11-04 13:43:56 +01:00
|
|
|
|
|
|
|
export async function fetchSchema(id) {
|
|
|
|
const FETCH_TABLE_URL = `/api/tables/${id}`
|
|
|
|
const response = await api.get(FETCH_TABLE_URL)
|
|
|
|
return (await response.json()).schema
|
|
|
|
}
|