From 6d973ce99a4a5d0b4e4b85b3fe492b87e7a6f760 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 19 Jul 2023 18:08:26 +0200 Subject: [PATCH] Infer table on search --- packages/server/src/api/controllers/row/index.ts | 10 +++++----- packages/server/src/api/routes/row.ts | 2 +- packages/server/src/api/routes/tests/row.spec.ts | 15 +++------------ packages/server/src/api/routes/view.ts | 2 +- packages/server/src/sdk/app/views/index.ts | 16 ++++++---------- .../server/src/tests/utilities/api/viewV2.ts | 8 ++------ 6 files changed, 18 insertions(+), 35 deletions(-) diff --git a/packages/server/src/api/controllers/row/index.ts b/packages/server/src/api/controllers/row/index.ts index 8a9947e707..8ba2f4b0ba 100644 --- a/packages/server/src/api/controllers/row/index.ts +++ b/packages/server/src/api/controllers/row/index.ts @@ -147,25 +147,25 @@ export async function search(ctx: any) { } export async function searchView(ctx: Ctx) { - const { tableId, viewId } = ctx.params + const { viewId } = ctx.params - const view = await sdk.views.get(tableId, viewId) + const view = await sdk.views.get(viewId) if (!view) { - ctx.throw(404, `View ${viewId} not found in table ${tableId}`) + ctx.throw(404, `View ${viewId} not found`) } ctx.status = 200 ctx.body = await quotas.addQuery( () => sdk.rows.search({ - tableId, + tableId: view.tableId, query: view.query || {}, sort: view.sort?.field, sortOrder: view.sort?.order, sortType: view.sort?.type, }), { - datasourceId: tableId, + datasourceId: view.tableId, } ) } diff --git a/packages/server/src/api/routes/row.ts b/packages/server/src/api/routes/row.ts index 1f71d3c1ae..5fdc02b7a7 100644 --- a/packages/server/src/api/routes/row.ts +++ b/packages/server/src/api/routes/row.ts @@ -147,7 +147,7 @@ router rowController.search ) .get( - "/api/v2/views/:tableId/:viewId/search", + "/api/v2/views/:viewId/search", authorized(PermissionType.VIEW, PermissionLevel.READ), rowController.searchView ) diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index b082bacea0..24305ab261 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -716,10 +716,7 @@ describe("/rows", () => { } const createViewResponse = await config.api.viewV2.create() - const response = await config.api.viewV2.search( - createViewResponse.tableId, - createViewResponse.id - ) + const response = await config.api.viewV2.search(createViewResponse.id) expect(response.body.rows).toHaveLength(10) expect(response.body).toEqual({ @@ -753,10 +750,7 @@ describe("/rows", () => { } ) - const response = await config.api.viewV2.search( - createViewResponse.tableId, - createViewResponse.id - ) + const response = await config.api.viewV2.search(createViewResponse.id) expect(response.body.rows).toHaveLength(5) expect(response.body).toEqual({ @@ -846,10 +840,7 @@ describe("/rows", () => { } ) - const response = await config.api.viewV2.search( - createViewResponse.tableId, - createViewResponse.id - ) + const response = await config.api.viewV2.search(createViewResponse.id) expect(response.body.rows).toHaveLength(4) expect(response.body).toEqual({ diff --git a/packages/server/src/api/routes/view.ts b/packages/server/src/api/routes/view.ts index ca93ec4de4..7a79d2dc44 100644 --- a/packages/server/src/api/routes/view.ts +++ b/packages/server/src/api/routes/view.ts @@ -3,7 +3,7 @@ import * as viewController from "../controllers/view" import * as rowController from "../controllers/row" import authorized from "../../middleware/authorized" import { paramResource } from "../../middleware/resourceId" -import { DocumentType, SEPARATOR, permissions } from "@budibase/backend-core" +import { permissions } from "@budibase/backend-core" const router: Router = new Router() diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index e2c37db562..ea8f5dd591 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -4,10 +4,8 @@ import { View, ViewV2 } from "@budibase/types" import sdk from "../../../sdk" import * as utils from "../../../db/utils" -export async function get( - tableId: string, - viewId: string -): Promise { +export async function get(viewId: string): Promise { + const { tableId } = utils.extractViewInfoFromId(viewId) const table = await sdk.tables.getTable(tableId) const views = Object.values(table.views!) const view = views.find(v => isV2(v) && v.id === viewId) as ViewV2 | undefined @@ -38,15 +36,13 @@ export function isV2(view: View | ViewV2): view is ViewV2 { return (view as ViewV2).version === 2 } -export async function remove( viewId: string): Promise { +export async function remove(viewId: string): Promise { const db = context.getAppDB() - const {tableId}=utils.extractViewInfoFromId(viewId) - - const table = await sdk.tables.getTable(tableId) - const view = await get(tableId, viewId) + const view = await get(viewId) + const table = await sdk.tables.getTable(view?.tableId) if (!view) { - throw new HTTPError(`View ${viewId} not found in table ${tableId}`, 404) + throw new HTTPError(`View ${viewId} not found`, 404) } delete table.views![view?.name] diff --git a/packages/server/src/tests/utilities/api/viewV2.ts b/packages/server/src/tests/utilities/api/viewV2.ts index fe00f50dfe..33ec9493c6 100644 --- a/packages/server/src/tests/utilities/api/viewV2.ts +++ b/packages/server/src/tests/utilities/api/viewV2.ts @@ -38,13 +38,9 @@ export class ViewV2API extends TestAPI { .expect(expectStatus) } - search = async ( - tableId: string, - viewId: string, - { expectStatus } = { expectStatus: 200 } - ) => { + search = async (viewId: string, { expectStatus } = { expectStatus: 200 }) => { return this.request - .get(`/api/v2/views/${tableId}/${viewId}/search`) + .get(`/api/v2/views/${viewId}/search`) .set(this.config.defaultHeaders()) .expect("Content-Type", /json/) .expect(expectStatus)