Add dedicated endpoint for fetching view definitions

This commit is contained in:
Andrew Kingston 2023-08-15 17:07:14 +01:00
parent a6da5fd2bb
commit c115a87cd6
5 changed files with 38 additions and 10 deletions

View File

@ -1,4 +1,13 @@
export const buildViewV2Endpoints = API => ({ export const buildViewV2Endpoints = API => ({
/**
* Create a new view
* @param viewId the ID of the view to fetch
*/
fetchDefinition: async viewId => {
return await API.get({
url: `/api/v2/views/${viewId}`,
})
},
/** /**
* Create a new view * Create a new view
* @param view the view object * @param view the view object

View File

@ -18,16 +18,13 @@ export default class ViewV2Fetch extends DataFetch {
} }
async getDefinition(datasource) { async getDefinition(datasource) {
if (!datasource?.id) {
return null
}
try { try {
const table = await this.API.fetchTableDefinition(datasource.tableId) const res = await this.API.viewV2.fetchDefinition(datasource.id)
return Object.values(table.views || {}).find( return res?.data
view => view.id === datasource.id
)
} catch (error) { } catch (error) {
this.store.update(state => ({
...state,
error,
}))
return null return null
} }
} }

View File

@ -39,6 +39,12 @@ async function parseSchema(view: CreateViewRequest) {
return finalViewSchema return finalViewSchema
} }
export async function get(ctx: Ctx<void, ViewResponse>) {
ctx.body = {
data: await sdk.views.get(ctx.params.viewId, { enriched: true })
}
}
export async function create(ctx: Ctx<CreateViewRequest, ViewResponse>) { export async function create(ctx: Ctx<CreateViewRequest, ViewResponse>) {
const view = ctx.request.body const view = ctx.request.body
const { tableId } = view const { tableId } = view

View File

@ -8,6 +8,15 @@ import { permissions } from "@budibase/backend-core"
const router: Router = new Router() const router: Router = new Router()
router router
.get(
"/api/v2/views/:viewId",
paramResource("viewId"),
authorized(
permissions.PermissionType.TABLE,
permissions.PermissionLevel.READ
),
viewController.v2.get
)
.post( .post(
"/api/v2/views", "/api/v2/views",
authorized(permissions.BUILDER), authorized(permissions.BUILDER),

View File

@ -5,7 +5,10 @@ import { cloneDeep } from "lodash"
import sdk from "../../../sdk" import sdk from "../../../sdk"
import * as utils from "../../../db/utils" import * as utils from "../../../db/utils"
export async function get(viewId: string): Promise<ViewV2> { export async function get(
viewId: string,
opts?: { enriched: boolean }
): Promise<ViewV2> {
const { tableId } = utils.extractViewInfoFromID(viewId) const { tableId } = utils.extractViewInfoFromID(viewId)
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!)
@ -13,7 +16,11 @@ export async function get(viewId: string): Promise<ViewV2> {
if (!found) { if (!found) {
throw new Error("No view found") throw new Error("No view found")
} }
return found as ViewV2 if (opts?.enriched) {
return enrichSchema(found, table.schema) as ViewV2
} else {
return found as ViewV2
}
} }
export async function create( export async function create(