1
0
Fork 0
mirror of synced 2024-06-03 02:55:14 +12:00
budibase/packages/server/src/api/controllers/row/external.js

253 lines
6.2 KiB
JavaScript
Raw Normal View History

const { makeExternalQuery } = require("./utils")
const { DataSourceOperation, SortDirection } = require("../../../constants")
const { getExternalTable } = require("../table/utils")
const {
breakExternalTableId,
generateRowIdField,
2021-06-17 05:39:59 +12:00
breakRowIdField,
} = require("../../../integrations/utils")
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])
}
row._id = generateRowIdField(idParts)
row.tableId = table._id
row._rev = "rev"
}
return rows
}
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
if (!id || !table) {
return filters
}
// if used as URL parameter it will have been joined
if (typeof id === "string") {
id = breakRowIdField(id)
}
const equal = {}
for (let field of primary) {
// work through the ID and get the parts
equal[field] = id.shift()
}
return {
equal,
}
}
2021-06-15 06:07:13 +12:00
async function handleRequest(
appId,
operation,
tableId,
{ id, row, filters, sort, paginate } = {}
2021-06-15 06:07:13 +12:00
) {
let { datasourceId, tableName } = breakExternalTableId(tableId)
const table = await getExternalTable(appId, datasourceId, tableName)
if (!table) {
throw `Unable to process query, table "${tableName}" not defined.`
}
// clean up row on ingress using schema
filters = buildFilters(id, filters, table)
row = inputProcessing(row, table)
if (
operation === DataSourceOperation.DELETE &&
(filters == null || Object.keys(filters).length === 0)
) {
throw "Deletion must be filtered"
}
let json = {
endpoint: {
datasourceId,
entityId: tableName,
operation,
},
2021-06-16 00:50:41 +12:00
resource: {
// not specifying any fields means "*"
fields: [],
},
filters,
sort,
paginate,
body: row,
}
// 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)
} else {
row = outputProcessing(response, table)[0]
return { row, table }
}
}
2021-06-15 06:07:13 +12:00
exports.patch = async ctx => {
const appId = ctx.appId
const inputs = ctx.request.body
const tableId = ctx.params.tableId
const id = breakRowIdField(inputs._id)
// don't save the ID to db
delete inputs._id
2021-06-16 00:50:41 +12:00
return handleRequest(appId, DataSourceOperation.UPDATE, tableId, {
2021-06-15 06:07:13 +12:00
id,
row: inputs,
})
}
2021-06-15 06:07:13 +12:00
exports.save = async ctx => {
const appId = ctx.appId
const inputs = ctx.request.body
const tableId = ctx.params.tableId
2021-06-16 00:50:41 +12:00
return handleRequest(appId, DataSourceOperation.CREATE, tableId, {
2021-06-15 06:07:13 +12:00
row: inputs,
})
}
2021-06-15 06:07:13 +12:00
exports.fetchView = async ctx => {
// there are no views in external data sources, shouldn't ever be called
// for now just fetch
ctx.params.tableId = ctx.params.viewName.split("all_")[1]
return exports.fetch(ctx)
}
exports.fetch = async ctx => {
const appId = ctx.appId
const tableId = ctx.params.tableId
2021-06-16 00:50:41 +12:00
return handleRequest(appId, DataSourceOperation.READ, tableId)
}
2021-06-15 06:07:13 +12:00
exports.find = async ctx => {
const appId = ctx.appId
const id = ctx.params.rowId
const tableId = ctx.params.tableId
2021-06-16 00:50:41 +12:00
return handleRequest(appId, DataSourceOperation.READ, tableId, {
id,
})
}
2021-06-15 06:07:13 +12:00
exports.destroy = async ctx => {
const appId = ctx.appId
const tableId = ctx.params.tableId
const id = ctx.request.body._id
const { row } = await handleRequest(appId, DataSourceOperation.DELETE, tableId, {
id,
2021-06-15 06:07:13 +12:00
})
return { response: { ok: true }, row }
}
2021-06-15 06:07:13 +12:00
exports.bulkDestroy = async ctx => {
const appId = ctx.appId
const { rows } = ctx.request.body
const tableId = ctx.params.tableId
let promises = []
for (let row of rows) {
2021-06-16 00:47:08 +12:00
promises.push(
handleRequest(appId, DataSourceOperation.DELETE, tableId, {
id: breakRowIdField(row._id),
2021-06-16 00:47:08 +12:00
})
)
}
await Promise.all(promises)
2021-06-16 00:50:41 +12:00
return { response: { ok: true }, rows }
}
2021-06-15 06:07:13 +12:00
exports.search = async ctx => {
const appId = ctx.appId
const tableId = ctx.params.tableId
const { paginate, query, ...params } = ctx.request.body
2021-06-18 02:56:41 +12:00
let { bookmark, limit } = params
if (!bookmark && paginate) {
bookmark = 1
}
let paginateObj = {}
2021-06-18 02:56:41 +12:00
if (paginate) {
paginateObj = {
2021-06-18 02:56:41 +12:00
// add one so we can track if there is another page
limit: limit,
page: bookmark,
}
2021-06-18 02:56:41 +12:00
} else if (params && limit) {
paginateObj = {
2021-06-18 02:56:41 +12:00
limit: limit,
}
}
let sort
if (params.sort) {
2021-06-15 06:07:13 +12:00
const direction =
params.sortOrder === "descending"
? SortDirection.DESCENDING
: SortDirection.ASCENDING
sort = {
2021-06-15 06:07:13 +12:00
[params.sort]: direction,
}
}
const rows = await handleRequest(appId, DataSourceOperation.READ, tableId, {
2021-06-15 06:07:13 +12:00
filters: query,
sort,
paginate: paginateObj,
})
2021-06-18 02:56:41 +12: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 * limit) + 1,
2021-06-18 02:56:41 +12:00
}
})
hasNextPage = nextRows.length > 0
}
// need wrapper object for bookmarks etc when paginating
2021-06-18 02:56:41 +12:00
return { rows, hasNextPage, bookmark: bookmark + 1 }
}
exports.validate = async () => {
// can't validate external right now - maybe in future
2021-06-16 00:50:41 +12:00
return { valid: true }
}
exports.fetchEnrichedRow = async () => {
// TODO: How does this work
2021-06-16 00:50:41 +12:00
throw "Not Implemented"
}