Infer table on search

This commit is contained in:
Adria Navarro 2023-07-19 18:08:26 +02:00
parent 36b82681ff
commit 6d973ce99a
6 changed files with 18 additions and 35 deletions

View File

@ -147,25 +147,25 @@ export async function search(ctx: any) {
} }
export async function searchView(ctx: Ctx<void, SearchResponse>) { export async function searchView(ctx: Ctx<void, SearchResponse>) {
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) { if (!view) {
ctx.throw(404, `View ${viewId} not found in table ${tableId}`) ctx.throw(404, `View ${viewId} not found`)
} }
ctx.status = 200 ctx.status = 200
ctx.body = await quotas.addQuery( ctx.body = await quotas.addQuery(
() => () =>
sdk.rows.search({ sdk.rows.search({
tableId, tableId: view.tableId,
query: view.query || {}, query: view.query || {},
sort: view.sort?.field, sort: view.sort?.field,
sortOrder: view.sort?.order, sortOrder: view.sort?.order,
sortType: view.sort?.type, sortType: view.sort?.type,
}), }),
{ {
datasourceId: tableId, datasourceId: view.tableId,
} }
) )
} }

View File

@ -147,7 +147,7 @@ router
rowController.search rowController.search
) )
.get( .get(
"/api/v2/views/:tableId/:viewId/search", "/api/v2/views/:viewId/search",
authorized(PermissionType.VIEW, PermissionLevel.READ), authorized(PermissionType.VIEW, PermissionLevel.READ),
rowController.searchView rowController.searchView
) )

View File

@ -716,10 +716,7 @@ describe("/rows", () => {
} }
const createViewResponse = await config.api.viewV2.create() const createViewResponse = await config.api.viewV2.create()
const response = await config.api.viewV2.search( const response = await config.api.viewV2.search(createViewResponse.id)
createViewResponse.tableId,
createViewResponse.id
)
expect(response.body.rows).toHaveLength(10) expect(response.body.rows).toHaveLength(10)
expect(response.body).toEqual({ expect(response.body).toEqual({
@ -753,10 +750,7 @@ describe("/rows", () => {
} }
) )
const response = await config.api.viewV2.search( const response = await config.api.viewV2.search(createViewResponse.id)
createViewResponse.tableId,
createViewResponse.id
)
expect(response.body.rows).toHaveLength(5) expect(response.body.rows).toHaveLength(5)
expect(response.body).toEqual({ expect(response.body).toEqual({
@ -846,10 +840,7 @@ describe("/rows", () => {
} }
) )
const response = await config.api.viewV2.search( const response = await config.api.viewV2.search(createViewResponse.id)
createViewResponse.tableId,
createViewResponse.id
)
expect(response.body.rows).toHaveLength(4) expect(response.body.rows).toHaveLength(4)
expect(response.body).toEqual({ expect(response.body).toEqual({

View File

@ -3,7 +3,7 @@ import * as viewController from "../controllers/view"
import * as rowController from "../controllers/row" import * as rowController from "../controllers/row"
import authorized from "../../middleware/authorized" import authorized from "../../middleware/authorized"
import { paramResource } from "../../middleware/resourceId" import { paramResource } from "../../middleware/resourceId"
import { DocumentType, SEPARATOR, permissions } from "@budibase/backend-core" import { permissions } from "@budibase/backend-core"
const router: Router = new Router() const router: Router = new Router()

View File

@ -4,10 +4,8 @@ import { View, ViewV2 } from "@budibase/types"
import sdk from "../../../sdk" import sdk from "../../../sdk"
import * as utils from "../../../db/utils" import * as utils from "../../../db/utils"
export async function get( export async function get(viewId: string): Promise<ViewV2 | undefined> {
tableId: string, const { tableId } = utils.extractViewInfoFromId(viewId)
viewId: string
): Promise<ViewV2 | undefined> {
const table = await sdk.tables.getTable(tableId) const table = await sdk.tables.getTable(tableId)
const views = Object.values(table.views!) const views = Object.values(table.views!)
const view = views.find(v => isV2(v) && v.id === viewId) as ViewV2 | undefined const view = views.find(v => isV2(v) && v.id === viewId) as ViewV2 | undefined
@ -41,12 +39,10 @@ export function isV2(view: View | ViewV2): view is ViewV2 {
export async function remove(viewId: string): Promise<void> { export async function remove(viewId: string): Promise<void> {
const db = context.getAppDB() const db = context.getAppDB()
const {tableId}=utils.extractViewInfoFromId(viewId) const view = await get(viewId)
const table = await sdk.tables.getTable(view?.tableId)
const table = await sdk.tables.getTable(tableId)
const view = await get(tableId, viewId)
if (!view) { 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] delete table.views![view?.name]

View File

@ -38,13 +38,9 @@ export class ViewV2API extends TestAPI {
.expect(expectStatus) .expect(expectStatus)
} }
search = async ( search = async (viewId: string, { expectStatus } = { expectStatus: 200 }) => {
tableId: string,
viewId: string,
{ expectStatus } = { expectStatus: 200 }
) => {
return this.request return this.request
.get(`/api/v2/views/${tableId}/${viewId}/search`) .get(`/api/v2/views/${viewId}/search`)
.set(this.config.defaultHeaders()) .set(this.config.defaultHeaders())
.expect("Content-Type", /json/) .expect("Content-Type", /json/)
.expect(expectStatus) .expect(expectStatus)