2021-06-11 19:56:30 +02:00
|
|
|
const CouchDB = require("../../../db")
|
2021-06-14 20:05:39 +02:00
|
|
|
const { makeExternalQuery } = require("./utils")
|
|
|
|
const { DataSourceOperation, SortDirection } = require("../../../constants")
|
|
|
|
|
|
|
|
async function buildIDFilter(id) {
|
|
|
|
if (!id) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
// TODO: work out how to use the schema to get filter
|
|
|
|
return {
|
|
|
|
equal: {
|
|
|
|
id: id,
|
2021-06-14 20:07:13 +02:00
|
|
|
},
|
2021-06-14 20:05:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-14 20:07:13 +02:00
|
|
|
async function handleRequest(
|
|
|
|
appId,
|
|
|
|
operation,
|
|
|
|
tableId,
|
|
|
|
{ id, row, filters, sort, paginate }
|
|
|
|
) {
|
2021-06-14 20:05:39 +02:00
|
|
|
let [datasourceId, tableName] = tableId.split("/")
|
|
|
|
let idFilter = buildIDFilter(id)
|
|
|
|
let json = {
|
|
|
|
endpoint: {
|
|
|
|
datasourceId,
|
|
|
|
entityId: tableName,
|
|
|
|
operation,
|
|
|
|
},
|
|
|
|
filters: {
|
|
|
|
...filters,
|
|
|
|
...idFilter,
|
|
|
|
},
|
|
|
|
sort,
|
|
|
|
paginate,
|
|
|
|
body: row,
|
|
|
|
}
|
|
|
|
return makeExternalQuery(appId, json)
|
|
|
|
}
|
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
|
|
|
|
const id = inputs._id
|
|
|
|
// don't save the ID to db
|
|
|
|
delete inputs._id
|
2021-06-14 20:07:13 +02:00
|
|
|
ctx.body = await handleRequest(appId, DataSourceOperation.UPDATE, tableId, {
|
|
|
|
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
|
|
|
|
if (inputs._id) {
|
|
|
|
return exports.patch(ctx)
|
|
|
|
}
|
|
|
|
const tableId = ctx.params.tableId
|
2021-06-14 20:07:13 +02:00
|
|
|
ctx.body = await handleRequest(appId, DataSourceOperation.CREATE, tableId, {
|
|
|
|
row: inputs,
|
|
|
|
})
|
2021-06-11 19:56:30 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 20:07:13 +02:00
|
|
|
exports.fetchView = async ctx => {
|
2021-06-14 20:05:39 +02:00
|
|
|
// TODO: don't know what this does for external
|
2021-06-11 19:56:30 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 20:07:13 +02:00
|
|
|
exports.fetchTableRows = async ctx => {
|
2021-06-14 20:05:39 +02:00
|
|
|
// TODO: this is a basic read?
|
2021-06-11 19:56:30 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 20:07:13 +02:00
|
|
|
exports.find = async ctx => {
|
2021-06-14 20:05:39 +02:00
|
|
|
// TODO: single find
|
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-14 20:07:13 +02:00
|
|
|
ctx.body = await handleRequest(appId, DataSourceOperation.DELETE, tableId, {
|
|
|
|
id: ctx.request.body._id,
|
|
|
|
})
|
2021-06-11 19:56:30 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 20:07:13 +02:00
|
|
|
exports.bulkDestroy = async ctx => {
|
2021-06-14 20:05:39 +02:00
|
|
|
// TODO: iterate through rows, build a large OR filter?
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
let paginateObj = {}
|
|
|
|
if (paginate) {
|
|
|
|
paginateObj = {
|
|
|
|
limit: params.limit,
|
|
|
|
// todo: need to handle bookmarks
|
|
|
|
page: params.bookmark,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-14 20:07:13 +02:00
|
|
|
ctx.body = await handleRequest(appId, DataSourceOperation.READ, tableId, {
|
|
|
|
filters: query,
|
|
|
|
sort,
|
|
|
|
paginate: paginateObj,
|
|
|
|
})
|
2021-06-11 19:56:30 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 20:07:13 +02:00
|
|
|
exports.validate = async ctx => {
|
2021-06-14 20:05:39 +02:00
|
|
|
// can't validate external right now - maybe in future
|
|
|
|
ctx.body = { valid: true }
|
2021-06-11 19:56:30 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 20:07:13 +02:00
|
|
|
exports.fetchEnrichedRow = async ctx => {
|
2021-06-11 19:56:30 +02:00
|
|
|
// TODO: should this join?
|
2021-06-14 20:05:39 +02:00
|
|
|
const appId = ctx.appId
|
2021-06-11 19:56:30 +02:00
|
|
|
ctx.body = {}
|
|
|
|
}
|