forked from tsai/budibase
13 changed files with 153 additions and 73 deletions
@ -1,38 +1,115 @@ |
|||
const CouchDB = require("../../../db") |
|||
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, |
|||
} |
|||
} |
|||
} |
|||
|
|||
async function handleRequest(appId, operation, tableId, { id, row, filters, sort, paginate }) { |
|||
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) |
|||
} |
|||
|
|||
exports.patch = async (ctx) => { |
|||
ctx.body = {} |
|||
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 }) |
|||
} |
|||
|
|||
exports.save = async (ctx) => { |
|||
ctx.body = {} |
|||
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 }) |
|||
} |
|||
|
|||
exports.fetchView = async (ctx) => { |
|||
ctx.body = {} |
|||
// TODO: don't know what this does for external
|
|||
} |
|||
|
|||
exports.fetchTableRows = async (ctx) => { |
|||
ctx.body = {} |
|||
// TODO: this is a basic read?
|
|||
} |
|||
|
|||
exports.find = async (ctx) => { |
|||
ctx.body = {} |
|||
// TODO: single find
|
|||
} |
|||
|
|||
exports.destroy = async (ctx) => { |
|||
ctx.body = {} |
|||
const appId = ctx.appId |
|||
const tableId = ctx.params.tableId |
|||
ctx.body = await handleRequest(appId, DataSourceOperation.DELETE, tableId, { id: ctx.request.body._id }) |
|||
} |
|||
|
|||
exports.bulkDestroy = async (ctx) => { |
|||
ctx.body = {} |
|||
// TODO: iterate through rows, build a large OR filter?
|
|||
} |
|||
|
|||
exports.search = async (ctx) => { |
|||
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) { |
|||
sort = { |
|||
[params.sort]: params.sortOrder === "descending" ? SortDirection.DESCENDING : SortDirection.ASCENDING |
|||
} |
|||
} |
|||
ctx.body = await handleRequest(appId, DataSourceOperation.READ, tableId, |
|||
{ |
|||
filters: query, |
|||
sort, |
|||
paginate: paginateObj, |
|||
} |
|||
) |
|||
} |
|||
|
|||
exports.validate = async (ctx) => { |
|||
ctx.body = {} |
|||
// can't validate external right now - maybe in future
|
|||
ctx.body = { valid: true } |
|||
} |
|||
|
|||
exports.fetchEnrichedRow = async (ctx) => { |
|||
// TODO: should this join?
|
|||
const appId = ctx.appId |
|||
ctx.body = {} |
|||
} |
|||
|
|||
@ -1,26 +0,0 @@ |
|||
const { fullSearch, paginatedSearch } = require("./utils") |
|||
const CouchDB = require("../../../db") |
|||
const { outputProcessing } = require("../../../utilities/rowProcessor") |
|||
|
|||
exports.rowSearch = async ctx => { |
|||
const appId = ctx.appId |
|||
const { tableId } = ctx.params |
|||
const db = new CouchDB(appId) |
|||
const { paginate, query, ...params } = ctx.request.body |
|||
params.tableId = tableId |
|||
|
|||
let response |
|||
if (paginate) { |
|||
response = await paginatedSearch(appId, query, params) |
|||
} else { |
|||
response = await fullSearch(appId, query, params) |
|||
} |
|||
|
|||
// Enrich search results with relationships
|
|||
if (response.rows && response.rows.length) { |
|||
const table = await db.get(tableId) |
|||
response.rows = await outputProcessing(appId, table, response.rows) |
|||
} |
|||
|
|||
ctx.body = response |
|||
} |
|||
@ -1,19 +0,0 @@ |
|||
const Router = require("@koa/router") |
|||
const controller = require("../controllers/search") |
|||
const { |
|||
PermissionTypes, |
|||
PermissionLevels, |
|||
} = require("@budibase/auth/permissions") |
|||
const authorized = require("../../middleware/authorized") |
|||
const { paramResource } = require("../../middleware/resourceId") |
|||
|
|||
const router = Router() |
|||
|
|||
router.post( |
|||
"/api/search/:tableId/rows", |
|||
paramResource("tableId"), |
|||
authorized(PermissionTypes.TABLE, PermissionLevels.READ), |
|||
controller.rowSearch |
|||
) |
|||
|
|||
module.exports = router |
|||
Loading…
Reference in new issue