Enrich view schemas

This commit is contained in:
Adria Navarro 2023-07-21 13:45:33 +02:00
parent a65e69e614
commit ed02aa4d8b
2 changed files with 41 additions and 15 deletions

View File

@ -7,7 +7,13 @@ import {
} from "../../../utilities/schema" } from "../../../utilities/schema"
import { isExternalTable, isSQL } from "../../../integrations/utils" import { isExternalTable, isSQL } from "../../../integrations/utils"
import { events } from "@budibase/backend-core" import { events } from "@budibase/backend-core"
import { FetchTablesResponse, Table, UserCtx } from "@budibase/types" import {
FetchTablesResponse,
Table,
TableResponse,
TableViewsResponse,
UserCtx,
} from "@budibase/types"
import sdk from "../../../sdk" import sdk from "../../../sdk"
import { jsonFromCsvString } from "../../../utilities/csv" import { jsonFromCsvString } from "../../../utilities/csv"
import { builderSocket } from "../../../websockets" import { builderSocket } from "../../../websockets"
@ -45,27 +51,47 @@ export async function fetch(ctx: UserCtx<void, FetchTablesResponse>) {
} }
}) })
const tables = [...internal, ...external] const response = [...internal, ...external].map(enrichTable)
ctx.body = response
}
for (const t of tables.filter(t => t.views)) { function enrichTable(table: Table): TableResponse {
for (const [viewName, view] of Object.entries(t.views!)) { const result: TableResponse = {
if (sdk.views.isV2(view)) { ...table,
t.views![viewName] = { views: Object.values(table.views ?? []).reduce((p, v) => {
...view, if (!sdk.views.isV2(v)) {
schema: !view?.columns?.length p[v.name] = v
? t.schema } else {
: _.pick(t.schema, ...view.columns), p[v.name] = {
...v,
schema: !v?.columns?.length
? table.schema
: _.pick(table.schema, ...v.columns),
} }
} }
return p
}, {} as TableViewsResponse),
}
for (const [viewName, view] of Object.entries(table.views!)) {
if (sdk.views.isV2(view)) {
table.views![viewName] = {
...view,
schema: !view?.columns?.length
? table.schema
: _.pick(table.schema, ...view.columns),
}
} }
} }
ctx.body = tables as FetchTablesResponse return result
} }
export async function find(ctx: UserCtx) { export async function find(ctx: UserCtx<void, TableResponse>) {
const tableId = ctx.params.tableId const tableId = ctx.params.tableId
ctx.body = await sdk.tables.getTable(tableId) const table = await sdk.tables.getTable(tableId)
ctx.body = enrichTable(table)
} }
export async function save(ctx: UserCtx) { export async function save(ctx: UserCtx) {

View File

@ -4,9 +4,9 @@ interface ViewV2Response extends ViewV2 {
schema: TableSchema schema: TableSchema
} }
type TableViewsResponse = { [key: string]: View | ViewV2Response } export type TableViewsResponse = { [key: string]: View | ViewV2Response }
interface TableResponse extends Table { export interface TableResponse extends Table {
views?: TableViewsResponse views?: TableViewsResponse
} }