|
|
|
@ -10,11 +10,16 @@ async function buildIDFilter(id) { |
|
|
|
return { |
|
|
|
equal: { |
|
|
|
id: id, |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
async function handleRequest(appId, operation, tableId, { id, row, filters, sort, paginate }) { |
|
|
|
async function handleRequest( |
|
|
|
appId, |
|
|
|
operation, |
|
|
|
tableId, |
|
|
|
{ id, row, filters, sort, paginate } |
|
|
|
) { |
|
|
|
let [datasourceId, tableName] = tableId.split("/") |
|
|
|
let idFilter = buildIDFilter(id) |
|
|
|
let json = { |
|
|
|
@ -34,49 +39,56 @@ async function handleRequest(appId, operation, tableId, { id, row, filters, sort |
|
|
|
return makeExternalQuery(appId, json) |
|
|
|
} |
|
|
|
|
|
|
|
exports.patch = async (ctx) => { |
|
|
|
exports.patch = async ctx => { |
|
|
|
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 |
|
|
|
ctx.body = await handleRequest(appId, DataSourceOperation.UPDATE, tableId, { id, row: inputs }) |
|
|
|
ctx.body = await handleRequest(appId, DataSourceOperation.UPDATE, tableId, { |
|
|
|
id, |
|
|
|
row: inputs, |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
exports.save = async (ctx) => { |
|
|
|
exports.save = async ctx => { |
|
|
|
const appId = ctx.appId |
|
|
|
const inputs = ctx.request.body |
|
|
|
if (inputs._id) { |
|
|
|
return exports.patch(ctx) |
|
|
|
} |
|
|
|
const tableId = ctx.params.tableId |
|
|
|
ctx.body = await handleRequest(appId, DataSourceOperation.CREATE, tableId, { row: inputs }) |
|
|
|
ctx.body = await handleRequest(appId, DataSourceOperation.CREATE, tableId, { |
|
|
|
row: inputs, |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
exports.fetchView = async (ctx) => { |
|
|
|
exports.fetchView = async ctx => { |
|
|
|
// TODO: don't know what this does for external
|
|
|
|
} |
|
|
|
|
|
|
|
exports.fetchTableRows = async (ctx) => { |
|
|
|
exports.fetchTableRows = async ctx => { |
|
|
|
// TODO: this is a basic read?
|
|
|
|
} |
|
|
|
|
|
|
|
exports.find = async (ctx) => { |
|
|
|
exports.find = async ctx => { |
|
|
|
// TODO: single find
|
|
|
|
} |
|
|
|
|
|
|
|
exports.destroy = async (ctx) => { |
|
|
|
exports.destroy = async ctx => { |
|
|
|
const appId = ctx.appId |
|
|
|
const tableId = ctx.params.tableId |
|
|
|
ctx.body = await handleRequest(appId, DataSourceOperation.DELETE, tableId, { id: ctx.request.body._id }) |
|
|
|
ctx.body = await handleRequest(appId, DataSourceOperation.DELETE, tableId, { |
|
|
|
id: ctx.request.body._id, |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
exports.bulkDestroy = async (ctx) => { |
|
|
|
exports.bulkDestroy = async ctx => { |
|
|
|
// TODO: iterate through rows, build a large OR filter?
|
|
|
|
} |
|
|
|
|
|
|
|
exports.search = async (ctx) => { |
|
|
|
exports.search = async ctx => { |
|
|
|
const appId = ctx.appId |
|
|
|
const tableId = ctx.params.tableId |
|
|
|
const { paginate, query, ...params } = ctx.request.body |
|
|
|
@ -90,25 +102,27 @@ exports.search = async (ctx) => { |
|
|
|
} |
|
|
|
let sort |
|
|
|
if (params.sort) { |
|
|
|
const direction = |
|
|
|
params.sortOrder === "descending" |
|
|
|
? SortDirection.DESCENDING |
|
|
|
: SortDirection.ASCENDING |
|
|
|
sort = { |
|
|
|
[params.sort]: params.sortOrder === "descending" ? SortDirection.DESCENDING : SortDirection.ASCENDING |
|
|
|
[params.sort]: direction, |
|
|
|
} |
|
|
|
} |
|
|
|
ctx.body = await handleRequest(appId, DataSourceOperation.READ, tableId, |
|
|
|
{ |
|
|
|
filters: query, |
|
|
|
sort, |
|
|
|
paginate: paginateObj, |
|
|
|
} |
|
|
|
) |
|
|
|
ctx.body = await handleRequest(appId, DataSourceOperation.READ, tableId, { |
|
|
|
filters: query, |
|
|
|
sort, |
|
|
|
paginate: paginateObj, |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
exports.validate = async (ctx) => { |
|
|
|
exports.validate = async ctx => { |
|
|
|
// can't validate external right now - maybe in future
|
|
|
|
ctx.body = { valid: true } |
|
|
|
} |
|
|
|
|
|
|
|
exports.fetchEnrichedRow = async (ctx) => { |
|
|
|
exports.fetchEnrichedRow = async ctx => { |
|
|
|
// TODO: should this join?
|
|
|
|
const appId = ctx.appId |
|
|
|
ctx.body = {} |
|
|
|
|