From 9fc605ac0633baf656d41aff0af36b6f6707eaeb Mon Sep 17 00:00:00 2001 From: Peter Clement Date: Fri, 4 Mar 2022 10:05:46 +0000 Subject: [PATCH] backend for client export of rows --- .../src/api/controllers/row/external.js | 20 +++++++++++++++++ .../server/src/api/controllers/row/index.js | 10 +++++++++ .../src/api/controllers/row/internal.js | 17 ++++++++++++++ packages/server/src/api/routes/row.js | 22 +++++++++++++++++++ 4 files changed, 69 insertions(+) diff --git a/packages/server/src/api/controllers/row/external.js b/packages/server/src/api/controllers/row/external.js index 66a1e30ca6..5f06118acc 100644 --- a/packages/server/src/api/controllers/row/external.js +++ b/packages/server/src/api/controllers/row/external.js @@ -152,6 +152,26 @@ exports.validate = async () => { return { valid: true } } +exports.exportRows = async ctx => { + const { datasourceId, tableName } = breakExternalTableId(ctx.params.tableId) + const db = getAppDB() + const datasource = await db.get(datasourceId) + if (!datasource || !datasource.entities) { + ctx.throw(400, "Datasource has not been configured for plus API.") + } + const tables = datasource.entities + const table = tables[tableName] + ctx.request.body = { + query: { + oneOf: { + [table.primaryDisplay]: ctx.request.body.map(id => breakRowIdField(id)[0]) + }, + }, + } + return exports.search(ctx) +} + + exports.fetchEnrichedRow = async ctx => { const id = ctx.params.rowId const tableId = ctx.params.tableId diff --git a/packages/server/src/api/controllers/row/index.js b/packages/server/src/api/controllers/row/index.js index 1d003ebd18..839c549735 100644 --- a/packages/server/src/api/controllers/row/index.js +++ b/packages/server/src/api/controllers/row/index.js @@ -137,3 +137,13 @@ exports.fetchEnrichedRow = async function (ctx) { ctx.throw(400, err) } } + +exports.export = async function (ctx) { + const tableId = getTableId(ctx) + try { + ctx.body = await pickApi(tableId).exportRows(ctx) + } catch (err) { + ctx.throw(400, err) + } + +} diff --git a/packages/server/src/api/controllers/row/internal.js b/packages/server/src/api/controllers/row/internal.js index 7650ac275f..faf2d8117c 100644 --- a/packages/server/src/api/controllers/row/internal.js +++ b/packages/server/src/api/controllers/row/internal.js @@ -362,6 +362,23 @@ exports.validate = async ctx => { }) } +exports.exportRows = async ctx => { + const db = getAppDB() + const table = await db.get(ctx.params.tableId) + const rowIds = ctx.request.body + let response = ( + await db.allDocs({ + include_docs: true, + keys: rowIds, + }) + ).rows.map(row => row.doc) + + let rows = await outputProcessing(table, response) + + return rows +} + + exports.fetchEnrichedRow = async ctx => { const db = getAppDB() const tableId = ctx.params.tableId diff --git a/packages/server/src/api/routes/row.js b/packages/server/src/api/routes/row.js index 2ceddc779a..7978bac3c7 100644 --- a/packages/server/src/api/routes/row.js +++ b/packages/server/src/api/routes/row.js @@ -252,4 +252,26 @@ router rowController.destroy ) + /** + * @api {post} /api/:tableId/rows/export Export Rows + * @apiName Export rows + * @apiGroup rows + * @apiPermission table write access + * @apiDescription This API can export a number of provided rows + * + * @apiParam {string} tableId The ID of the table the row is to be deleted from. + * + * @apiParam (Body) {object[]} [rows] The row IDs which are to be exported + * + * @apiSuccess {object[]|object} + */ + .post( + "/api/:tableId/rows/export", + paramResource("tableId"), + authorized(PermissionTypes.TABLE, PermissionLevels.WRITE), + usage, + rowController.export + ) + + module.exports = router