Move enrich views to sdk

This commit is contained in:
Adria Navarro 2023-07-24 12:44:00 +02:00
parent 396c4ad439
commit d2020fd6bc
2 changed files with 54 additions and 48 deletions

View File

@ -11,15 +11,11 @@ import {
FetchTablesResponse, FetchTablesResponse,
Table, Table,
TableResponse, TableResponse,
TableSchema,
TableViewsResponse,
UIFieldMetadata,
UserCtx, UserCtx,
} from "@budibase/types" } 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"
import _ from "lodash"
function pickApi({ tableId, table }: { tableId?: string; table?: Table }) { function pickApi({ tableId, table }: { tableId?: string; table?: Table }) {
if (table && !tableId) { if (table && !tableId) {
@ -53,56 +49,15 @@ export async function fetch(ctx: UserCtx<void, FetchTablesResponse>) {
} }
}) })
const response = [...internal, ...external].map(enrichTable) const response = [...internal, ...external].map(sdk.tables.enrichViewSchemas)
ctx.body = response ctx.body = response
} }
function enrichSchema(
tableSchema: TableSchema,
viewOverrides: Record<string, UIFieldMetadata>
) {
const result: TableSchema = {}
for (const [columnName, columnUIMetadata] of Object.entries(viewOverrides)) {
if (!columnUIMetadata.visible) {
continue
}
if (!tableSchema[columnName]) {
continue
}
result[columnName] = _.merge(tableSchema[columnName], columnUIMetadata)
}
return result
}
function enrichTable(table: Table): TableResponse {
const result: TableResponse = {
...table,
views: Object.values(table.views ?? []).reduce((p, v) => {
if (!sdk.views.isV2(v)) {
p[v.name] = v
} else {
p[v.name] = {
...v,
schema:
!v?.columns || !Object.entries(v?.columns).length
? table.schema
: enrichSchema(table.schema, v.columns),
}
}
return p
}, {} as TableViewsResponse),
}
return result
}
export async function find(ctx: UserCtx<void, TableResponse>) { export async function find(ctx: UserCtx<void, TableResponse>) {
const tableId = ctx.params.tableId const tableId = ctx.params.tableId
const table = await sdk.tables.getTable(tableId) const table = await sdk.tables.getTable(tableId)
ctx.body = enrichTable(table) ctx.body = sdk.tables.enrichViewSchemas(table)
} }
export async function save(ctx: UserCtx) { export async function save(ctx: UserCtx) {

View File

@ -5,9 +5,18 @@ import {
isExternalTable, isExternalTable,
isSQL, isSQL,
} from "../../../integrations/utils" } from "../../../integrations/utils"
import { Table, Database } from "@budibase/types" import {
Table,
Database,
TableResponse,
TableViewsResponse,
TableSchema,
UIFieldMetadata,
} from "@budibase/types"
import datasources from "../datasources" import datasources from "../datasources"
import { populateExternalTableSchemas, isEditableColumn } from "./validation" import { populateExternalTableSchemas, isEditableColumn } from "./validation"
import sdk from "../../../sdk"
import _ from "lodash"
async function getAllInternalTables(db?: Database): Promise<Table[]> { async function getAllInternalTables(db?: Database): Promise<Table[]> {
if (!db) { if (!db) {
@ -55,6 +64,47 @@ async function getTable(tableId: any): Promise<Table> {
} }
} }
function enrichSchema(
tableSchema: TableSchema,
viewOverrides: Record<string, UIFieldMetadata>
) {
const result: TableSchema = {}
for (const [columnName, columnUIMetadata] of Object.entries(viewOverrides)) {
if (!columnUIMetadata.visible) {
continue
}
if (!tableSchema[columnName]) {
continue
}
result[columnName] = _.merge(tableSchema[columnName], columnUIMetadata)
}
return result
}
function enrichViewSchemas(table: Table): TableResponse {
const result: TableResponse = {
...table,
views: Object.values(table.views ?? []).reduce((p, v) => {
if (!sdk.views.isV2(v)) {
p[v.name] = v
} else {
p[v.name] = {
...v,
schema:
!v?.columns || !Object.entries(v?.columns).length
? table.schema
: enrichSchema(table.schema, v.columns),
}
}
return p
}, {} as TableViewsResponse),
}
return result
}
export default { export default {
getAllInternalTables, getAllInternalTables,
getAllExternalTables, getAllExternalTables,
@ -62,4 +112,5 @@ export default {
getTable, getTable,
populateExternalTableSchemas, populateExternalTableSchemas,
isEditableColumn, isEditableColumn,
enrichViewSchemas,
} }