2021-06-14 20:05:39 +02:00
|
|
|
const { makeExternalQuery } = require("./utils")
|
|
|
|
const { DataSourceOperation, SortDirection } = require("../../../constants")
|
2021-06-16 17:27:33 +02:00
|
|
|
const { getExternalTable } = require("../table/utils")
|
2021-06-16 19:38:00 +02:00
|
|
|
const {
|
|
|
|
breakExternalTableId,
|
|
|
|
generateRowIdField,
|
2021-06-16 19:39:59 +02:00
|
|
|
breakRowIdField,
|
2021-06-16 19:38:00 +02:00
|
|
|
} = require("../../../integrations/utils")
|
2021-06-15 14:03:55 +02:00
|
|
|
|
2021-06-15 15:56:25 +02:00
|
|
|
function inputProcessing(row, table) {
|
|
|
|
if (!row) {
|
|
|
|
return row
|
|
|
|
}
|
|
|
|
let newRow = {}
|
|
|
|
for (let key of Object.keys(table.schema)) {
|
|
|
|
// currently excludes empty strings
|
|
|
|
if (row[key]) {
|
|
|
|
newRow[key] = row[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newRow
|
|
|
|
}
|
|
|
|
|
|
|
|
function outputProcessing(rows, table) {
|
|
|
|
// if no rows this is what is returned? Might be PG only
|
|
|
|
if (rows[0].read === true) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
const primary = table.primary
|
|
|
|
for (let row of rows) {
|
|
|
|
// build id array
|
|
|
|
let idParts = []
|
|
|
|
for (let field of primary) {
|
|
|
|
idParts.push(row[field])
|
|
|
|
}
|
2021-06-16 19:38:00 +02:00
|
|
|
row._id = generateRowIdField(idParts)
|
|
|
|
row.tableId = table._id
|
2021-06-15 15:56:25 +02:00
|
|
|
}
|
|
|
|
return rows
|
|
|
|
}
|
|
|
|
|
2021-06-16 19:38:00 +02:00
|
|
|
function buildFilters(id, filters, table) {
|
|
|
|
const primary = table.primary
|
|
|
|
if (filters) {
|
|
|
|
// need to map over the filters and make sure the _id field isn't present
|
|
|
|
for (let filter of Object.values(filters)) {
|
|
|
|
if (filter._id) {
|
|
|
|
const parts = breakRowIdField(filter._id)
|
|
|
|
for (let field of primary) {
|
|
|
|
filter[field] = parts.shift()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// make sure this field doesn't exist on any filter
|
|
|
|
delete filter._id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// there is no id, just use the user provided filters
|
2021-06-15 14:03:55 +02:00
|
|
|
if (!id || !table) {
|
2021-06-16 19:38:00 +02:00
|
|
|
return filters
|
2021-06-15 14:03:55 +02:00
|
|
|
}
|
|
|
|
// if used as URL parameter it will have been joined
|
|
|
|
if (typeof id === "string") {
|
2021-06-16 19:38:00 +02:00
|
|
|
id = breakRowIdField(id)
|
2021-06-15 14:03:55 +02:00
|
|
|
}
|
|
|
|
const equal = {}
|
|
|
|
for (let field of primary) {
|
|
|
|
// work through the ID and get the parts
|
|
|
|
equal[field] = id.shift()
|
2021-06-14 20:05:39 +02:00
|
|
|
}
|
|
|
|
return {
|
2021-06-15 14:03:55 +02:00
|
|
|
equal,
|
2021-06-14 20:05:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-14 20:07:13 +02:00
|
|
|
async function handleRequest(
|
|
|
|
appId,
|
|
|
|
operation,
|
|
|
|
tableId,
|
2021-06-15 14:03:55 +02:00
|
|
|
{ id, row, filters, sort, paginate } = {}
|
2021-06-14 20:07:13 +02:00
|
|
|
) {
|
2021-06-16 17:27:33 +02:00
|
|
|
let { datasourceId, tableName } = breakExternalTableId(tableId)
|
|
|
|
const table = await getExternalTable(appId, datasourceId, tableName)
|
2021-06-15 14:03:55 +02:00
|
|
|
if (!table) {
|
|
|
|
throw `Unable to process query, table "${tableName}" not defined.`
|
|
|
|
}
|
2021-06-15 15:56:25 +02:00
|
|
|
// clean up row on ingress using schema
|
2021-06-16 19:38:00 +02:00
|
|
|
filters = buildFilters(id, filters, table)
|
2021-06-15 15:56:25 +02:00
|
|
|
row = inputProcessing(row, table)
|
2021-06-16 18:50:22 +02:00
|
|
|
if (
|
|
|
|
operation === DataSourceOperation.DELETE &&
|
2021-06-17 15:42:30 +02:00
|
|
|
(filters == null || Object.keys(filters).length === 0)
|
2021-06-16 18:50:22 +02:00
|
|
|
) {
|
2021-06-16 19:39:15 +02:00
|
|
|
throw "Deletion must be filtered"
|
2021-06-15 15:56:25 +02:00
|
|
|
}
|
2021-06-14 20:05:39 +02:00
|
|
|
let json = {
|
|
|
|
endpoint: {
|
|
|
|
datasourceId,
|
|
|
|
entityId: tableName,
|
|
|
|
operation,
|
|
|
|
},
|
2021-06-15 14:50:41 +02:00
|
|
|
resource: {
|
|
|
|
// not specifying any fields means "*"
|
|
|
|
fields: [],
|
|
|
|
},
|
2021-06-16 19:38:00 +02:00
|
|
|
filters,
|
2021-06-14 20:05:39 +02:00
|
|
|
sort,
|
|
|
|
paginate,
|
|
|
|
body: row,
|
|
|
|
}
|
2021-06-15 15:56:25 +02:00
|
|
|
// can't really use response right now
|
|
|
|
const response = await makeExternalQuery(appId, json)
|
|
|
|
// we searched for rows in someway
|
|
|
|
if (operation === DataSourceOperation.READ && Array.isArray(response)) {
|
|
|
|
return outputProcessing(response, table)
|
2021-06-17 15:42:30 +02:00
|
|
|
} else {
|
|
|
|
row = outputProcessing(response, table)[0]
|
|
|
|
return { row, table }
|
2021-06-15 15:56:25 +02:00
|
|
|
}
|
2021-06-14 20:05:39 +02:00
|
|
|
}
|
2021-06-11 19:56:30 +02:00
|
|
|
|
2021-06-14 20:07:13 +02:00
|
|
|
exports.patch = async ctx => {
|
2021-06-14 20:05:39 +02:00
|
|
|
const appId = ctx.appId
|
|
|
|
const inputs = ctx.request.body
|
|
|
|
const tableId = ctx.params.tableId
|
2021-06-16 19:38:00 +02:00
|
|
|
const id = breakRowIdField(inputs._id)
|
2021-06-14 20:05:39 +02:00
|
|
|
// don't save the ID to db
|
|
|
|
delete inputs._id
|
2021-06-15 14:50:41 +02:00
|
|
|
return handleRequest(appId, DataSourceOperation.UPDATE, tableId, {
|
2021-06-14 20:07:13 +02:00
|
|
|
id,
|
|
|
|
row: inputs,
|
|
|
|
})
|
2021-06-11 19:56:30 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 20:07:13 +02:00
|
|
|
exports.save = async ctx => {
|
2021-06-14 20:05:39 +02:00
|
|
|
const appId = ctx.appId
|
|
|
|
const inputs = ctx.request.body
|
|
|
|
const tableId = ctx.params.tableId
|
2021-06-15 14:50:41 +02:00
|
|
|
return handleRequest(appId, DataSourceOperation.CREATE, tableId, {
|
2021-06-14 20:07:13 +02:00
|
|
|
row: inputs,
|
|
|
|
})
|
2021-06-11 19:56:30 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 20:07:13 +02:00
|
|
|
exports.fetchView = async ctx => {
|
2021-06-15 14:03:55 +02:00
|
|
|
// there are no views in external data sources, shouldn't ever be called
|
2021-06-15 14:20:25 +02:00
|
|
|
// for now just fetch
|
|
|
|
ctx.params.tableId = ctx.params.viewName.split("all_")[1]
|
|
|
|
return exports.fetch(ctx)
|
2021-06-11 19:56:30 +02:00
|
|
|
}
|
|
|
|
|
2021-06-15 14:03:55 +02:00
|
|
|
exports.fetch = async ctx => {
|
|
|
|
const appId = ctx.appId
|
|
|
|
const tableId = ctx.params.tableId
|
2021-06-15 14:50:41 +02:00
|
|
|
return handleRequest(appId, DataSourceOperation.READ, tableId)
|
2021-06-11 19:56:30 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 20:07:13 +02:00
|
|
|
exports.find = async ctx => {
|
2021-06-15 14:03:55 +02:00
|
|
|
const appId = ctx.appId
|
|
|
|
const id = ctx.params.rowId
|
|
|
|
const tableId = ctx.params.tableId
|
2021-06-15 14:50:41 +02:00
|
|
|
return handleRequest(appId, DataSourceOperation.READ, tableId, {
|
2021-06-15 14:03:55 +02:00
|
|
|
id,
|
|
|
|
})
|
2021-06-11 19:56:30 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 20:07:13 +02:00
|
|
|
exports.destroy = async ctx => {
|
2021-06-14 20:05:39 +02:00
|
|
|
const appId = ctx.appId
|
|
|
|
const tableId = ctx.params.tableId
|
2021-06-17 15:42:30 +02:00
|
|
|
const id = ctx.request.body._id
|
|
|
|
const { row } = await handleRequest(appId, DataSourceOperation.DELETE, tableId, {
|
|
|
|
id,
|
2021-06-14 20:07:13 +02:00
|
|
|
})
|
2021-06-17 15:42:30 +02:00
|
|
|
return { response: { ok: true }, row }
|
2021-06-11 19:56:30 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 20:07:13 +02:00
|
|
|
exports.bulkDestroy = async ctx => {
|
2021-06-15 14:03:55 +02:00
|
|
|
const appId = ctx.appId
|
|
|
|
const { rows } = ctx.request.body
|
|
|
|
const tableId = ctx.params.tableId
|
|
|
|
// TODO: this can probably be optimised to a single SQL statement in the future
|
|
|
|
let promises = []
|
|
|
|
for (let row of rows) {
|
2021-06-15 14:47:08 +02:00
|
|
|
promises.push(
|
|
|
|
handleRequest(appId, DataSourceOperation.DELETE, tableId, {
|
2021-06-16 19:38:00 +02:00
|
|
|
id: breakRowIdField(row._id),
|
2021-06-15 14:47:08 +02:00
|
|
|
})
|
|
|
|
)
|
2021-06-15 14:03:55 +02:00
|
|
|
}
|
|
|
|
await Promise.all(promises)
|
2021-06-15 14:50:41 +02:00
|
|
|
return { response: { ok: true }, rows }
|
2021-06-14 20:05:39 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 20:07:13 +02:00
|
|
|
exports.search = async ctx => {
|
2021-06-14 20:05:39 +02:00
|
|
|
const appId = ctx.appId
|
|
|
|
const tableId = ctx.params.tableId
|
|
|
|
const { paginate, query, ...params } = ctx.request.body
|
2021-06-17 16:56:41 +02:00
|
|
|
let { bookmark, limit } = params
|
|
|
|
if (!bookmark && paginate) {
|
|
|
|
bookmark = 1
|
|
|
|
}
|
2021-06-14 20:05:39 +02:00
|
|
|
let paginateObj = {}
|
2021-06-17 16:56:41 +02:00
|
|
|
|
2021-06-14 20:05:39 +02:00
|
|
|
if (paginate) {
|
|
|
|
paginateObj = {
|
2021-06-17 16:56:41 +02:00
|
|
|
// add one so we can track if there is another page
|
|
|
|
limit: limit,
|
|
|
|
page: bookmark,
|
2021-06-14 20:05:39 +02:00
|
|
|
}
|
2021-06-17 16:56:41 +02:00
|
|
|
} else if (params && limit) {
|
2021-06-16 19:38:00 +02:00
|
|
|
paginateObj = {
|
2021-06-17 16:56:41 +02:00
|
|
|
limit: limit,
|
2021-06-16 19:38:00 +02:00
|
|
|
}
|
2021-06-14 20:05:39 +02:00
|
|
|
}
|
|
|
|
let sort
|
|
|
|
if (params.sort) {
|
2021-06-14 20:07:13 +02:00
|
|
|
const direction =
|
|
|
|
params.sortOrder === "descending"
|
|
|
|
? SortDirection.DESCENDING
|
|
|
|
: SortDirection.ASCENDING
|
2021-06-14 20:05:39 +02:00
|
|
|
sort = {
|
2021-06-14 20:07:13 +02:00
|
|
|
[params.sort]: direction,
|
2021-06-14 20:05:39 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-16 17:27:33 +02:00
|
|
|
const rows = await handleRequest(appId, DataSourceOperation.READ, tableId, {
|
2021-06-14 20:07:13 +02:00
|
|
|
filters: query,
|
|
|
|
sort,
|
|
|
|
paginate: paginateObj,
|
|
|
|
})
|
2021-06-17 16:56:41 +02:00
|
|
|
let hasNextPage = false
|
|
|
|
if (paginate && rows.length === limit) {
|
|
|
|
const nextRows = await handleRequest(appId, DataSourceOperation.READ, tableId, {
|
|
|
|
filters: query,
|
|
|
|
sort,
|
|
|
|
paginate: {
|
|
|
|
limit: 1,
|
|
|
|
page: bookmark + 1,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
hasNextPage = nextRows.length > 0
|
|
|
|
}
|
2021-06-16 17:27:33 +02:00
|
|
|
// need wrapper object for bookmarks etc when paginating
|
2021-06-17 16:56:41 +02:00
|
|
|
return { rows, hasNextPage, bookmark: bookmark + 1 }
|
2021-06-11 19:56:30 +02:00
|
|
|
}
|
|
|
|
|
2021-06-16 17:27:33 +02:00
|
|
|
exports.validate = async () => {
|
2021-06-14 20:05:39 +02:00
|
|
|
// can't validate external right now - maybe in future
|
2021-06-15 14:50:41 +02:00
|
|
|
return { valid: true }
|
2021-06-11 19:56:30 +02:00
|
|
|
}
|
|
|
|
|
2021-06-17 00:27:38 +02:00
|
|
|
exports.fetchEnrichedRow = async () => {
|
2021-06-15 14:03:55 +02:00
|
|
|
// TODO: How does this work
|
2021-06-15 14:50:41 +02:00
|
|
|
throw "Not Implemented"
|
2021-06-11 19:56:30 +02:00
|
|
|
}
|