From 5cfddabac76b50e8698e39d1403edc9a12f0c06e Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 31 Jul 2023 10:21:44 +0200 Subject: [PATCH] Move row view controllers in their own file --- .../server/src/api/controllers/row/index.ts | 82 +----------------- .../server/src/api/controllers/row/views.ts | 86 +++++++++++++++++++ packages/server/src/api/routes/row.ts | 36 ++++++-- 3 files changed, 118 insertions(+), 86 deletions(-) create mode 100644 packages/server/src/api/controllers/row/views.ts diff --git a/packages/server/src/api/controllers/row/index.ts b/packages/server/src/api/controllers/row/index.ts index 41f5f1ca89..7c8af846b8 100644 --- a/packages/server/src/api/controllers/row/index.ts +++ b/packages/server/src/api/controllers/row/index.ts @@ -11,10 +11,6 @@ import { Row, PatchRowRequest, PatchRowResponse, - SearchResponse, - SortOrder, - SortType, - ViewV2, } from "@budibase/types" import * as utils from "./utils" import { gridSocket } from "../../../websockets" @@ -23,6 +19,7 @@ import { fixRow } from "../public/rows" import sdk from "../../../sdk" import * as exporters from "../view/exporters" import { apiFileReturn } from "../../../utilities/fileSystem" +export * as views from "./views" function pickApi(tableId: any) { if (isExternalTable(tableId)) { @@ -222,83 +219,6 @@ export async function search(ctx: any) { }) } -function getSortOptions( - ctx: Ctx, - view: ViewV2 -): - | { - sort: string - sortOrder?: SortOrder - sortType?: SortType - } - | undefined { - const { sort_column, sort_order, sort_type } = ctx.query - if (Array.isArray(sort_column)) { - ctx.throw(400, "sort_column cannot be an array") - } - if (Array.isArray(sort_order)) { - ctx.throw(400, "sort_order cannot be an array") - } - if (Array.isArray(sort_type)) { - ctx.throw(400, "sort_type cannot be an array") - } - - if (sort_column) { - return { - sort: sort_column, - sortOrder: sort_order as SortOrder, - sortType: sort_type as SortType, - } - } - if (view.sort) { - return { - sort: view.sort.field, - sortOrder: view.sort.order, - sortType: view.sort.type, - } - } - - return -} - -export async function searchView(ctx: Ctx) { - const { viewId } = ctx.params - - const view = await sdk.views.get(viewId) - if (!view) { - ctx.throw(404, `View ${viewId} not found`) - } - - if (view.version !== 2) { - ctx.throw(400, `This method only supports viewsV2`) - } - - const table = await sdk.tables.getTable(view?.tableId) - - const viewFields = - (view.columns && - Object.entries(view.columns).length && - Object.keys(sdk.views.enrichSchema(view, table.schema).schema)) || - undefined - - ctx.status = 200 - const result = await quotas.addQuery( - () => - sdk.rows.search({ - tableId: view.tableId, - query: view.query || {}, - fields: viewFields, - ...getSortOptions(ctx, view), - }), - { - datasourceId: view.tableId, - } - ) - - result.rows.forEach(r => (r._viewId = view.id)) - ctx.body = result -} - export async function validate(ctx: Ctx) { const tableId = utils.getTableId(ctx) // external tables are hard to validate currently diff --git a/packages/server/src/api/controllers/row/views.ts b/packages/server/src/api/controllers/row/views.ts new file mode 100644 index 0000000000..2560002ead --- /dev/null +++ b/packages/server/src/api/controllers/row/views.ts @@ -0,0 +1,86 @@ +import { quotas } from "@budibase/pro" +import { + Ctx, + SearchResponse, + SortOrder, + SortType, + ViewV2, +} from "@budibase/types" +import sdk from "../../../sdk" + +export async function searchView(ctx: Ctx) { + const { viewId } = ctx.params + + const view = await sdk.views.get(viewId) + if (!view) { + ctx.throw(404, `View ${viewId} not found`) + } + + if (view.version !== 2) { + ctx.throw(400, `This method only supports viewsV2`) + } + + const table = await sdk.tables.getTable(view?.tableId) + + const viewFields = + (view.columns && + Object.entries(view.columns).length && + Object.keys(sdk.views.enrichSchema(view, table.schema).schema)) || + undefined + + ctx.status = 200 + const result = await quotas.addQuery( + () => + sdk.rows.search({ + tableId: view.tableId, + query: view.query || {}, + fields: viewFields, + ...getSortOptions(ctx, view), + }), + { + datasourceId: view.tableId, + } + ) + + result.rows.forEach(r => (r._viewId = view.id)) + ctx.body = result +} + +function getSortOptions( + ctx: Ctx, + view: ViewV2 +): + | { + sort: string + sortOrder?: SortOrder + sortType?: SortType + } + | undefined { + const { sort_column, sort_order, sort_type } = ctx.query + if (Array.isArray(sort_column)) { + ctx.throw(400, "sort_column cannot be an array") + } + if (Array.isArray(sort_order)) { + ctx.throw(400, "sort_order cannot be an array") + } + if (Array.isArray(sort_type)) { + ctx.throw(400, "sort_type cannot be an array") + } + + if (sort_column) { + return { + sort: sort_column, + sortOrder: sort_order as SortOrder, + sortType: sort_type as SortType, + } + } + if (view.sort) { + return { + sort: view.sort.field, + sortOrder: view.sort.order, + sortType: view.sort.type, + } + } + + return +} diff --git a/packages/server/src/api/routes/row.ts b/packages/server/src/api/routes/row.ts index 5fdc02b7a7..a68a9065c7 100644 --- a/packages/server/src/api/routes/row.ts +++ b/packages/server/src/api/routes/row.ts @@ -146,11 +146,6 @@ router authorized(PermissionType.TABLE, PermissionLevel.READ), rowController.search ) - .get( - "/api/v2/views/:viewId/search", - authorized(PermissionType.VIEW, PermissionLevel.READ), - rowController.searchView - ) /** * @api {post} /api/:tableId/rows Creates a new row * @apiName Creates a new row @@ -268,4 +263,35 @@ router rowController.exportRows ) +router + .get( + "/api/v2/views/:viewId/search", + authorized(PermissionType.VIEW, PermissionLevel.READ), + rowController.views.searchView + ) + /** + * @api {post} /api/:tableId/rows Creates a new row + * @apiName Creates a new row + * @apiGroup rows + * @apiPermission table write access + * @apiDescription This API will create a new row based on the supplied body. If the + * body includes an "_id" field then it will update an existing row if the field + * links to one. Please note that "_id", "_rev" and "tableId" are fields that are + * already used by Budibase tables and cannot be used for columns. + * + * @apiParam {string} tableId The ID of the table to save a row to. + * + * @apiParam (Body) {string} [_id] If the row exists already then an ID for the row must be provided. + * @apiParam (Body) {string} [_rev] If working with an existing row for an internal table its revision + * must also be provided. + * @apiParam (Body) {string} _viewId The ID of the view should be specified in the row body itself. + * @apiParam (Body) {string} tableId The ID of the table should also be specified in the row body itself. + * @apiParam (Body) {any} [any] Any field supplied in the body will be assessed to see if it matches + * a column in the specified table. All other fields will be dropped and not stored. + * + * @apiSuccess {string} _id The ID of the row that was just saved, if it was just created this + * is the rows new ID. + * @apiSuccess {string} [_rev] If saving to an internal table a revision will also be returned. + * @apiSuccess {object} body The contents of the row that was saved will be returned as well. + */ export default router