Adding fetch endpoint for views (will be used by search for views in Public API).
This commit is contained in:
parent
6fe4f16845
commit
c055e2ae72
|
@ -12,6 +12,7 @@ import {
|
||||||
RelationSchemaField,
|
RelationSchemaField,
|
||||||
ViewFieldMetadata,
|
ViewFieldMetadata,
|
||||||
CalculationType,
|
CalculationType,
|
||||||
|
ViewFetchResponseEnriched,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { builderSocket, gridSocket } from "../../../websockets"
|
import { builderSocket, gridSocket } from "../../../websockets"
|
||||||
import { helpers } from "@budibase/shared-core"
|
import { helpers } from "@budibase/shared-core"
|
||||||
|
@ -119,6 +120,12 @@ export async function get(ctx: Ctx<void, ViewResponseEnriched>) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function fetch(ctx: Ctx<void, ViewFetchResponseEnriched>) {
|
||||||
|
ctx.body = {
|
||||||
|
data: await sdk.views.getAllEnriched(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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
|
||||||
|
|
|
@ -8,6 +8,11 @@ import { permissions } from "@budibase/backend-core"
|
||||||
const router: Router = new Router()
|
const router: Router = new Router()
|
||||||
|
|
||||||
router
|
router
|
||||||
|
.get(
|
||||||
|
"/api/v2/views",
|
||||||
|
authorized(permissions.BUILDER),
|
||||||
|
viewController.v2.fetch
|
||||||
|
)
|
||||||
.get(
|
.get(
|
||||||
"/api/v2/views/:viewId",
|
"/api/v2/views/:viewId",
|
||||||
authorizedResource(
|
authorizedResource(
|
||||||
|
|
|
@ -27,6 +27,7 @@ import { isExternalTableID } from "../../../integrations/utils"
|
||||||
import * as internal from "./internal"
|
import * as internal from "./internal"
|
||||||
import * as external from "./external"
|
import * as external from "./external"
|
||||||
import sdk from "../../../sdk"
|
import sdk from "../../../sdk"
|
||||||
|
import { ensureQueryUISet } from "./utils"
|
||||||
|
|
||||||
function pickApi(tableId: any) {
|
function pickApi(tableId: any) {
|
||||||
if (isExternalTableID(tableId)) {
|
if (isExternalTableID(tableId)) {
|
||||||
|
@ -45,6 +46,24 @@ export async function getEnriched(viewId: string): Promise<ViewV2Enriched> {
|
||||||
return pickApi(tableId).getEnriched(viewId)
|
return pickApi(tableId).getEnriched(viewId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getAllEnriched(): Promise<ViewV2Enriched[]> {
|
||||||
|
const tables = await sdk.tables.getAllTables()
|
||||||
|
let views: ViewV2Enriched[] = []
|
||||||
|
for (let table of tables) {
|
||||||
|
if (!table.views || Object.keys(table.views).length === 0) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
const v2Views = Object.values(table.views).filter(isV2)
|
||||||
|
const enrichedViews = await Promise.all(
|
||||||
|
v2Views.map(view =>
|
||||||
|
enrichSchema(ensureQueryUISet(view), table.schema, tables)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
views = views.concat(enrichedViews)
|
||||||
|
}
|
||||||
|
return views
|
||||||
|
}
|
||||||
|
|
||||||
export async function getTable(view: string | ViewV2): Promise<Table> {
|
export async function getTable(view: string | ViewV2): Promise<Table> {
|
||||||
const viewId = typeof view === "string" ? view : view.id
|
const viewId = typeof view === "string" ? view : view.id
|
||||||
const cached = context.getTableForView(viewId)
|
const cached = context.getTableForView(viewId)
|
||||||
|
@ -303,13 +322,19 @@ export function allowedFields(
|
||||||
|
|
||||||
export async function enrichSchema(
|
export async function enrichSchema(
|
||||||
view: ViewV2,
|
view: ViewV2,
|
||||||
tableSchema: TableSchema
|
tableSchema: TableSchema,
|
||||||
|
tables?: Table[]
|
||||||
): Promise<ViewV2Enriched> {
|
): Promise<ViewV2Enriched> {
|
||||||
async function populateRelTableSchema(
|
async function populateRelTableSchema(
|
||||||
tableId: string,
|
tableId: string,
|
||||||
viewFields: Record<string, RelationSchemaField>
|
viewFields: Record<string, RelationSchemaField>
|
||||||
) {
|
) {
|
||||||
const relTable = await sdk.tables.getTable(tableId)
|
let relTable = tables
|
||||||
|
? tables?.find(t => t._id === tableId)
|
||||||
|
: await sdk.tables.getTable(tableId)
|
||||||
|
if (!relTable) {
|
||||||
|
throw new Error("Cannot enrich relationship, table not found")
|
||||||
|
}
|
||||||
const result: Record<string, ViewV2ColumnEnriched> = {}
|
const result: Record<string, ViewV2ColumnEnriched> = {}
|
||||||
for (const relTableFieldName of Object.keys(relTable.schema)) {
|
for (const relTableFieldName of Object.keys(relTable.schema)) {
|
||||||
const relTableField = relTable.schema[relTableFieldName]
|
const relTableField = relTable.schema[relTableFieldName]
|
||||||
|
|
|
@ -9,6 +9,10 @@ export interface ViewResponseEnriched {
|
||||||
data: ViewV2Enriched
|
data: ViewV2Enriched
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ViewFetchResponseEnriched {
|
||||||
|
data: ViewV2Enriched[]
|
||||||
|
}
|
||||||
|
|
||||||
export interface CreateViewRequest extends Omit<ViewV2, "version" | "id"> {}
|
export interface CreateViewRequest extends Omit<ViewV2, "version" | "id"> {}
|
||||||
|
|
||||||
export interface UpdateViewRequest extends ViewV2 {}
|
export interface UpdateViewRequest extends ViewV2 {}
|
||||||
|
|
Loading…
Reference in New Issue