Merge pull request #14473 from Budibase/BUDI-8564/picker-only-for-views

Picker only for views
This commit is contained in:
Adria Navarro 2024-08-30 11:23:56 +02:00 committed by GitHub
commit 7b2aebd467
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
29 changed files with 338 additions and 196 deletions

View File

@ -18,8 +18,6 @@
import GridEditColumnModal from "components/backend/DataTable/modals/grid/GridEditColumnModal.svelte"
import GridUsersTableButton from "components/backend/DataTable/modals/grid/GridUsersTableButton.svelte"
import { DB_TYPE_EXTERNAL } from "constants/backend"
import { isEnabled } from "helpers/featureFlags"
import { FeatureFlag } from "@budibase/types"
const userSchemaOverrides = {
firstName: { displayName: "First name", disabled: true },
@ -68,7 +66,6 @@
canDeleteRows={!isUsersTable}
canEditRows={!isUsersTable || !$appStore.features.disableUserMetadata}
canEditColumns={!isUsersTable || !$appStore.features.disableUserMetadata}
canSetRelationshipSchemas={isEnabled(FeatureFlag.ENRICHED_RELATIONSHIPS)}
schemaOverrides={isUsersTable ? userSchemaOverrides : null}
showAvatars={false}
on:updatedatasource={handleGridTableUpdate}

View File

@ -123,7 +123,7 @@
const table = await cache.actions.getTable(relationshipField.tableId)
relationshipPanelColumns = Object.entries(
relationshipField?.schema || {}
relationshipField?.columns || {}
).map(([name, column]) => {
return {
name: name,

View File

@ -62,12 +62,12 @@ export const deriveStores = context => {
}
if ($subSchemaMutations[field]) {
enrichedSchema[field].schema ??= {}
enrichedSchema[field].columns ??= {}
for (const [fieldName, mutation] of Object.entries(
$subSchemaMutations[field]
)) {
enrichedSchema[field].schema[fieldName] = {
...enrichedSchema[field].schema[fieldName],
enrichedSchema[field].columns[fieldName] = {
...enrichedSchema[field].columns[fieldName],
...mutation,
}
}
@ -239,12 +239,12 @@ export const createActions = context => {
...$schemaMutations[column],
}
if ($subSchemaMutations[column]) {
newSchema[column].schema ??= {}
newSchema[column].columns ??= {}
for (const [fieldName, mutation] of Object.entries(
$subSchemaMutations[column]
)) {
newSchema[column].schema[fieldName] = {
...newSchema[column].schema[fieldName],
newSchema[column].columns[fieldName] = {
...newSchema[column].columns[fieldName],
...mutation,
}
}

View File

@ -38,7 +38,7 @@ export async function handleRequest<T extends Operation>(
}
export async function patch(ctx: UserCtx<PatchRowRequest, PatchRowResponse>) {
const { tableId } = utils.getSourceId(ctx)
const { tableId, viewId } = utils.getSourceId(ctx)
const { _id, ...rowData } = ctx.request.body
const table = await sdk.tables.getTable(tableId)
@ -77,6 +77,7 @@ export async function patch(ctx: UserCtx<PatchRowRequest, PatchRowResponse>) {
outputProcessing(table, row, {
squash: true,
preserveLinks: true,
fromViewId: viewId,
}),
outputProcessing(table, beforeRow, {
squash: true,
@ -163,14 +164,10 @@ export async function fetchEnrichedRow(ctx: UserCtx) {
},
includeSqlRelationships: IncludeRelationship.INCLUDE,
})
row[fieldName] = await outputProcessing<Row[]>(
linkedTable,
relatedRows.rows,
{
squash: true,
preserveLinks: true,
}
)
row[fieldName] = await outputProcessing(linkedTable, relatedRows.rows, {
squash: true,
preserveLinks: true,
})
}
return row
}

View File

@ -71,8 +71,10 @@ export async function patch(
}
export const save = async (ctx: UserCtx<Row, Row>) => {
const { tableId, viewId } = utils.getSourceId(ctx)
const sourceId = viewId || tableId
const appId = ctx.appId
const { tableId } = utils.getSourceId(ctx)
const body = ctx.request.body
// user metadata doesn't exist yet - don't allow creation
@ -85,9 +87,9 @@ export const save = async (ctx: UserCtx<Row, Row>) => {
return patch(ctx as UserCtx<PatchRowRequest, PatchRowResponse>)
}
const { row, table, squashed } = tableId.includes("datasource_plus")
? await sdk.rows.save(tableId, ctx.request.body, ctx.user?._id)
? await sdk.rows.save(sourceId, ctx.request.body, ctx.user?._id)
: await quotas.addRow(() =>
sdk.rows.save(tableId, ctx.request.body, ctx.user?._id)
sdk.rows.save(sourceId, ctx.request.body, ctx.user?._id)
)
ctx.status = 200
ctx.eventEmitter && ctx.eventEmitter.emitRow(`row:save`, appId, row, table)
@ -115,10 +117,12 @@ export async function fetch(ctx: any) {
}
export async function find(ctx: UserCtx<void, GetRowResponse>) {
const { tableId } = utils.getSourceId(ctx)
const { tableId, viewId } = utils.getSourceId(ctx)
const sourceId = viewId || tableId
const rowId = ctx.params.rowId
ctx.body = await sdk.rows.find(tableId, rowId)
const response = await sdk.rows.find(sourceId, rowId)
ctx.body = response
}
function isDeleteRows(input: any): input is DeleteRows {

View File

@ -23,7 +23,7 @@ import { getLinkedTableIDs } from "../../../db/linkedRows/linkUtils"
import { flatten } from "lodash"
export async function patch(ctx: UserCtx<PatchRowRequest, PatchRowResponse>) {
const { tableId } = utils.getSourceId(ctx)
const { tableId, viewId } = utils.getSourceId(ctx)
const inputs = ctx.request.body
const isUserTable = tableId === InternalTables.USER_METADATA
let oldRow
@ -90,6 +90,7 @@ export async function patch(ctx: UserCtx<PatchRowRequest, PatchRowResponse>) {
const result = await finaliseRow(table, row, {
oldTable: dbTable,
updateFormula: true,
fromViewId: viewId,
})
return { ...result, oldRow }

View File

@ -123,7 +123,11 @@ export async function updateAllFormulasInTable(table: Table) {
export async function finaliseRow(
table: Table,
row: Row,
{ oldTable, updateFormula }: { oldTable?: Table; updateFormula: boolean } = {
{
oldTable,
updateFormula,
fromViewId,
}: { oldTable?: Table; updateFormula: boolean; fromViewId?: string } = {
updateFormula: true,
}
) {
@ -154,9 +158,8 @@ export async function finaliseRow(
if (updateFormula) {
await updateRelatedFormula(table, enrichedRow)
}
const squashed = await linkRows.squashLinksToPrimaryDisplay(
table,
enrichedRow
)
const squashed = await linkRows.squashLinks(table, enrichedRow, {
fromViewId,
})
return { row: enrichedRow, squashed, table }
}

View File

@ -71,8 +71,11 @@ export async function searchView(
})
const searchOptions: RequiredKeys<SearchViewRowRequest> &
RequiredKeys<Pick<RowSearchParams, "tableId" | "query" | "fields">> = {
RequiredKeys<
Pick<RowSearchParams, "tableId" | "viewId" | "query" | "fields">
> = {
tableId: view.tableId,
viewId: view.id,
query: enrichedQuery,
fields: viewFields,
...getSortOptions(body, view),

View File

@ -95,10 +95,7 @@ export async function find(ctx: UserCtx<void, TableResponse>) {
const tableId = ctx.params.tableId
const table = await sdk.tables.getTable(tableId)
const result = await sdk.tables.enrichViewSchemas({
...table,
schema: await sdk.tables.enrichRelationshipSchema(table.schema),
})
const result = await sdk.tables.enrichViewSchemas(table)
ctx.body = result
}

View File

@ -22,8 +22,8 @@ async function parseSchema(view: CreateViewRequest) {
let fieldRelatedSchema:
| Record<string, RequiredKeys<RelationSchemaField>>
| undefined
if (schemaValue.schema) {
fieldRelatedSchema = Object.entries(schemaValue.schema).reduce<
if (schemaValue.columns) {
fieldRelatedSchema = Object.entries(schemaValue.columns).reduce<
NonNullable<typeof fieldRelatedSchema>
>((acc, [key, fieldSchema]) => {
acc[key] = {
@ -36,7 +36,7 @@ async function parseSchema(view: CreateViewRequest) {
const fieldSchema: RequiredKeys<
ViewFieldMetadata & {
schema: typeof fieldRelatedSchema
columns: typeof fieldRelatedSchema
}
> = {
order: schemaValue.order,
@ -44,7 +44,7 @@ async function parseSchema(view: CreateViewRequest) {
visible: schemaValue.visible,
readonly: schemaValue.readonly,
icon: schemaValue.icon,
schema: fieldRelatedSchema,
columns: fieldRelatedSchema,
}
Object.entries(fieldSchema)
.filter(([, val]) => val === undefined)

View File

@ -41,6 +41,7 @@ import {
JsonFieldSubType,
RowExportFormat,
FeatureFlag,
RelationSchemaField,
} from "@budibase/types"
import { generator, mocks } from "@budibase/backend-core/tests"
import _, { merge } from "lodash"
@ -1233,7 +1234,7 @@ describe.each([
})
it.each([{ not: "valid" }, { rows: 123 }, "invalid"])(
"Should ignore malformed/invalid delete request: %s",
"should ignore malformed/invalid delete request: %s",
async (request: any) => {
const rowUsage = await getRowUsage()
@ -2434,6 +2435,7 @@ describe.each([
!isOracle &&
describe("relationships", () => {
let tableId: string
let viewId: string
let auxData: Row[] = []
@ -2520,7 +2522,6 @@ describe.each([
tableId: auxTableId,
fieldName: "fk_relWithEmptySchema",
constraints: { presence: true },
schema: {},
},
relWithFullSchema: {
name: "relWithFullSchema",
@ -2529,10 +2530,6 @@ describe.each([
tableId: auxTableId,
fieldName: "fk_relWithFullSchema",
constraints: { presence: true },
schema: Object.keys(auxTable.schema).reduce(
(acc, c) => ({ ...acc, [c]: { visible: true } }),
{}
),
},
relWithHalfSchema: {
name: "relWithHalfSchema",
@ -2541,10 +2538,6 @@ describe.each([
tableId: auxTableId,
fieldName: "fk_relWithHalfSchema",
constraints: { presence: true },
schema: {
name: { visible: true },
age: { visible: false, readonly: true },
},
},
relWithIllegalSchema: {
name: "relWithIllegalSchema",
@ -2553,16 +2546,50 @@ describe.each([
tableId: auxTableId,
fieldName: "fk_relWithIllegalSchema",
constraints: { presence: true },
schema: {
name: { visible: true },
address: { visible: true },
unexisting: { visible: true },
},
},
},
})
)
tableId = table._id!
const view = await config.api.viewV2.create({
name: generator.guid(),
tableId,
schema: {
title: {
visible: true,
},
relWithNoSchema: {
visible: true,
},
relWithEmptySchema: {
visible: true,
columns: {},
},
relWithFullSchema: {
visible: true,
columns: Object.keys(auxTable.schema).reduce<
Record<string, RelationSchemaField>
>((acc, c) => ({ ...acc, [c]: { visible: true } }), {}),
},
relWithHalfSchema: {
visible: true,
columns: {
name: { visible: true },
age: { visible: false, readonly: true },
},
},
relWithIllegalSchema: {
visible: true,
columns: {
name: { visible: true },
address: { visible: true },
unexisting: { visible: true },
},
},
},
})
viewId = view.id
})
afterAll(() => {
@ -2570,38 +2597,16 @@ describe.each([
})
const testScenarios: [string, (row: Row) => Promise<Row> | Row][] = [
["get row", (row: Row) => config.api.row.get(tableId, row._id!)],
["get row", (row: Row) => config.api.row.get(viewId, row._id!)],
[
"fetch",
"from view search",
async (row: Row) => {
const rows = await config.api.row.fetch(tableId)
return rows.find(r => r._id === row._id)
},
],
[
"search",
async (row: Row) => {
const { rows } = await config.api.row.search(tableId)
return rows.find(r => r._id === row._id)
},
],
[
"from view",
async (row: Row) => {
const table = await config.api.table.get(tableId)
const view = await config.api.viewV2.create({
name: generator.guid(),
tableId,
schema: Object.keys(table.schema).reduce(
(acc, c) => ({ ...acc, [c]: { visible: true } }),
{}
),
})
const { rows } = await config.api.viewV2.search(view.id)
const { rows } = await config.api.viewV2.search(viewId)
return rows.find(r => r._id === row._id!)
},
],
["from original saved row", (row: Row) => row],
["from updated row", (row: Row) => config.api.row.save(viewId, row)],
]
it.each(testScenarios)(
@ -2609,7 +2614,7 @@ describe.each([
async (__, retrieveDelegate) => {
const otherRows = _.sampleSize(auxData, 5)
const row = await config.api.row.save(tableId, {
const row = await config.api.row.save(viewId, {
title: generator.word(),
relWithNoSchema: [otherRows[0]],
relWithEmptySchema: [otherRows[1]],
@ -2673,7 +2678,7 @@ describe.each([
async () => {
const otherRows = _.sampleSize(auxData, 5)
const row = await config.api.row.save(tableId, {
const row = await config.api.row.save(viewId, {
title: generator.word(),
relWithNoSchema: [otherRows[0]],
relWithEmptySchema: [otherRows[1]],
@ -2723,6 +2728,75 @@ describe.each([
)
}
)
it.each([
[
"from table fetch",
async (row: Row) => {
const rows = await config.api.row.fetch(tableId)
return rows.find(r => r._id === row._id!)
},
],
[
"from table search",
async (row: Row) => {
const { rows } = await config.api.row.search(tableId)
return rows.find(r => r._id === row._id!)
},
],
])(
"does not enrich when fetching from the table (via %s)",
async (__, retrieveDelegate) => {
const otherRows = _.sampleSize(auxData, 5)
const row = await config.api.row.save(viewId, {
title: generator.word(),
relWithNoSchema: [otherRows[0]],
relWithEmptySchema: [otherRows[1]],
relWithFullSchema: [otherRows[2]],
relWithHalfSchema: [otherRows[3]],
relWithIllegalSchema: [otherRows[4]],
})
const retrieved = await retrieveDelegate(row)
expect(retrieved).toEqual(
expect.objectContaining({
title: row.title,
relWithNoSchema: [
{
_id: otherRows[0]._id,
primaryDisplay: otherRows[0].name,
},
],
relWithEmptySchema: [
{
_id: otherRows[1]._id,
primaryDisplay: otherRows[1].name,
},
],
relWithFullSchema: [
{
_id: otherRows[2]._id,
primaryDisplay: otherRows[2].name,
},
],
relWithHalfSchema: [
{
_id: otherRows[3]._id,
primaryDisplay: otherRows[3].name,
},
],
relWithIllegalSchema: [
{
_id: otherRows[4]._id,
primaryDisplay: otherRows[4].name,
},
],
})
)
}
)
})
describe("Formula fields", () => {

View File

@ -20,6 +20,8 @@ import {
Row,
Table,
TableSchema,
ViewFieldMetadata,
ViewV2,
} from "@budibase/types"
import sdk from "../../sdk"
@ -242,16 +244,34 @@ function getPrimaryDisplayValue(row: Row, table?: Table) {
}
}
export type SquashTableFields = Record<string, { visibleFieldNames: string[] }>
/**
* This function will take the given enriched rows and squash the links to only contain the primary display field.
* @param table The table from which the rows originated.
* @param enriched The pre-enriched rows (full docs) which are to be squashed.
* @param squashFields Per link column (key) define which columns are allowed while squashing.
* @returns The rows after having their links squashed to only contain the ID and primary display.
*/
export async function squashLinksToPrimaryDisplay(
export async function squashLinks<T = Row[] | Row>(
table: Table,
enriched: Row[] | Row
) {
enriched: T,
options?: {
fromViewId?: string
}
): Promise<T> {
const allowRelationshipSchemas = await features.flags.isEnabled(
FeatureFlag.ENRICHED_RELATIONSHIPS
)
let viewSchema: Record<string, ViewFieldMetadata> = {}
if (options?.fromViewId && allowRelationshipSchemas) {
const view = Object.values(table.views || {}).find(
(v): v is ViewV2 => sdk.views.isV2(v) && v.id === options?.fromViewId
)
viewSchema = view?.schema || {}
}
// will populate this as we find them
const linkedTables = [table]
const isArray = Array.isArray(enriched)
@ -259,11 +279,7 @@ export async function squashLinksToPrimaryDisplay(
for (const row of enrichedArray) {
// this only fetches the table if its not already in array
const rowTable = await getLinkedTable(row.tableId!, linkedTables)
const safeSchema =
(rowTable?.schema &&
(await sdk.tables.enrichRelationshipSchema(rowTable.schema))) ||
{}
for (let [column, schema] of Object.entries(safeSchema)) {
for (let [column, schema] of Object.entries(rowTable.schema)) {
if (schema.type !== FieldType.LINK || !Array.isArray(row[column])) {
continue
}
@ -275,13 +291,27 @@ export async function squashLinksToPrimaryDisplay(
const obj: any = { _id: link._id }
obj.primaryDisplay = getPrimaryDisplayValue(link, linkedTable)
const allowRelationshipSchemas = await features.flags.isEnabled(
FeatureFlag.ENRICHED_RELATIONSHIPS
)
if (schema.schema && allowRelationshipSchemas) {
for (const relField of Object.entries(schema.schema)
.filter(([_, field]) => field.visible !== false)
.map(([fieldKey]) => fieldKey)) {
if (viewSchema[column]?.columns) {
const squashFields = Object.entries(viewSchema[column].columns)
.filter(([columnName, viewColumnConfig]) => {
const tableColumn = linkedTable.schema[columnName]
if (!tableColumn) {
return false
}
if (
[FieldType.LINK, FieldType.FORMULA].includes(tableColumn.type)
) {
return false
}
return (
tableColumn.visible !== false &&
viewColumnConfig.visible !== false
)
})
.map(([columnName]) => columnName)
for (const relField of squashFields) {
obj[relField] = link[relField]
}
}

View File

@ -28,7 +28,7 @@ export default async (ctx: Ctx<Row, Row>, next: Next) => {
}
// have to mutate the koa context, can't return
export async function trimNonViewFields(
async function trimNonViewFields(
row: Row,
view: ViewV2,
permission: "WRITE" | "READ"

View File

@ -9,6 +9,7 @@ import {
} from "../../../utilities/rowProcessor"
import cloneDeep from "lodash/fp/cloneDeep"
import isEqual from "lodash/fp/isEqual"
import { tryExtractingTableAndViewId } from "./utils"
export async function getRow(
tableId: string,
@ -26,10 +27,11 @@ export async function getRow(
}
export async function save(
tableId: string,
tableOrViewId: string,
inputs: Row,
userId: string | undefined
) {
const { tableId, viewId } = tryExtractingTableAndViewId(tableOrViewId)
const table = await sdk.tables.getTable(tableId)
const { table: updatedTable, row } = await inputProcessing(
userId,
@ -63,6 +65,7 @@ export async function save(
row: await outputProcessing(table, row, {
preserveLinks: true,
squash: true,
fromViewId: viewId,
}),
}
} else {
@ -70,7 +73,9 @@ export async function save(
}
}
export async function find(tableId: string, rowId: string): Promise<Row> {
export async function find(tableOrViewId: string, rowId: string): Promise<Row> {
const { tableId, viewId } = tryExtractingTableAndViewId(tableOrViewId)
const row = await getRow(tableId, rowId, {
relationships: true,
})
@ -84,5 +89,6 @@ export async function find(tableId: string, rowId: string): Promise<Row> {
return await outputProcessing(table, row, {
squash: true,
preserveLinks: true,
fromViewId: viewId,
})
}

View File

@ -10,12 +10,14 @@ import {
import * as linkRows from "../../../db/linkedRows"
import { InternalTables } from "../../../db/utils"
import { getFullUser } from "../../../utilities/users"
import { tryExtractingTableAndViewId } from "./utils"
export async function save(
tableId: string,
tableOrViewId: string,
inputs: Row,
userId: string | undefined
) {
const { tableId, viewId } = tryExtractingTableAndViewId(tableOrViewId)
inputs.tableId = tableId
if (!inputs._rev && !inputs._id) {
@ -50,14 +52,17 @@ export async function save(
return finaliseRow(table, row, {
oldTable: dbTable,
updateFormula: true,
fromViewId: viewId,
})
}
export async function find(tableId: string, rowId: string): Promise<Row> {
export async function find(tableOrViewId: string, rowId: string): Promise<Row> {
const { tableId, viewId } = tryExtractingTableAndViewId(tableOrViewId)
const table = await sdk.tables.getTable(tableId)
let row = await findRow(tableId, rowId)
row = await outputProcessing(table, row)
row = await outputProcessing(table, row, { squash: true, fromViewId: viewId })
return row
}

View File

@ -1,6 +1,10 @@
import { db as dbCore, context } from "@budibase/backend-core"
import { Database, Row } from "@budibase/types"
import { getRowParams } from "../../../db/utils"
import {
extractViewInfoFromID,
getRowParams,
isViewID,
} from "../../../db/utils"
import { isExternalTableID } from "../../../integrations/utils"
import * as internal from "./internal"
import * as external from "./external"
@ -20,7 +24,12 @@ export async function getAllInternalRows(appId?: string) {
return response.rows.map(row => row.doc) as Row[]
}
function pickApi(tableId: any) {
function pickApi(tableOrViewId: string) {
let tableId = tableOrViewId
if (isViewID(tableOrViewId)) {
tableId = extractViewInfoFromID(tableOrViewId).tableId
}
if (isExternalTableID(tableId)) {
return external
}
@ -28,13 +37,13 @@ function pickApi(tableId: any) {
}
export async function save(
tableId: string,
tableOrViewId: string,
row: Row,
userId: string | undefined
) {
return pickApi(tableId).save(tableId, row, userId)
return pickApi(tableOrViewId).save(tableOrViewId, row, userId)
}
export async function find(tableId: string, rowId: string) {
return pickApi(tableId).find(tableId, rowId)
export async function find(tableOrViewId: string, rowId: string) {
return pickApi(tableOrViewId).find(tableOrViewId, rowId)
}

View File

@ -112,9 +112,10 @@ export async function search(
: Promise.resolve(undefined),
])
let processed = await outputProcessing<Row[]>(table, rows, {
let processed = await outputProcessing(table, rows, {
preserveLinks: true,
squash: true,
fromViewId: options.viewId,
})
let hasNextPage = false
@ -260,7 +261,7 @@ export async function fetch(tableId: string): Promise<Row[]> {
includeSqlRelationships: IncludeRelationship.INCLUDE,
})
const table = await sdk.tables.getTable(tableId)
return await outputProcessing<Row[]>(table, response.rows, {
return await outputProcessing(table, response.rows, {
preserveLinks: true,
squash: true,
})

View File

@ -61,7 +61,7 @@ export async function exportRows(
})
).rows.map(row => row.doc!)
result = await outputProcessing<Row[]>(table, response)
result = await outputProcessing(table, response)
} else if (query) {
let searchResponse = await sdk.rows.search({
tableId,

View File

@ -59,7 +59,10 @@ export async function search(
response.rows = response.rows.map((r: any) => pick(r, fields))
}
response.rows = await outputProcessing(table, response.rows)
response.rows = await outputProcessing(table, response.rows, {
squash: true,
fromViewId: options.viewId,
})
}
return response

View File

@ -379,9 +379,10 @@ export async function search(
}
// get the rows
let finalRows = await outputProcessing<Row[]>(table, processed, {
let finalRows = await outputProcessing(table, processed, {
preserveLinks: true,
squash: true,
fromViewId: options.viewId,
})
// check if we need to pick specific rows out

View File

@ -17,7 +17,11 @@ import {
import { makeExternalQuery } from "../../../integrations/base/query"
import { Format } from "../../../api/controllers/view/exporters"
import sdk from "../.."
import { isRelationshipColumn } from "../../../db/utils"
import {
extractViewInfoFromID,
isRelationshipColumn,
isViewID,
} from "../../../db/utils"
import { isSQL } from "../../../integrations/utils"
const SQL_CLIENT_SOURCE_MAP: Record<SourceName, SqlClient | undefined> = {
@ -317,3 +321,14 @@ function validateTimeOnlyField(
export function isArrayFilter(operator: any): operator is ArrayOperator {
return Object.values(ArrayOperator).includes(operator)
}
export function tryExtractingTableAndViewId(tableOrViewId: string) {
if (isViewID(tableOrViewId)) {
return {
tableId: extractViewInfoFromID(tableOrViewId).tableId,
viewId: tableOrViewId,
}
}
return { tableId: tableOrViewId }
}

View File

@ -7,13 +7,9 @@ import {
} from "../../../integrations/utils"
import {
Database,
FieldType,
INTERNAL_TABLE_SOURCE_ID,
RelationSchemaField,
RelationshipFieldMetadata,
Table,
TableResponse,
TableSchema,
TableSourceType,
TableViewsResponse,
} from "@budibase/types"
@ -146,53 +142,6 @@ export async function getTables(tableIds: string[]): Promise<Table[]> {
return processTables(tables)
}
export async function enrichRelationshipSchema(
schema: TableSchema
): Promise<TableSchema> {
const tableCache: Record<string, Table> = {}
async function populateRelTableSchema(field: RelationshipFieldMetadata) {
if (!tableCache[field.tableId]) {
tableCache[field.tableId] = await sdk.tables.getTable(field.tableId)
}
const relTable = tableCache[field.tableId]
const fieldSchema = field.schema || {}
const resultSchema: Record<string, RelationSchemaField> = {}
for (const relTableFieldName of Object.keys(relTable.schema)) {
const relTableField = relTable.schema[relTableFieldName]
if ([FieldType.LINK, FieldType.FORMULA].includes(relTableField.type)) {
continue
}
if (relTableField.visible === false) {
continue
}
const isVisible = !!fieldSchema[relTableFieldName]?.visible
const isReadonly = !!fieldSchema[relTableFieldName]?.readonly
resultSchema[relTableFieldName] = {
visible: isVisible,
readonly: isReadonly,
}
}
field.schema = resultSchema
}
const result: TableSchema = {}
for (const fieldName of Object.keys(schema)) {
const field = { ...schema[fieldName] }
if (field.type === FieldType.LINK) {
await populateRelTableSchema(field)
}
result[fieldName] = field
}
return result
}
export async function enrichViewSchemas(table: Table): Promise<TableResponse> {
const views = []
for (const view of Object.values(table.views ?? [])) {

View File

@ -1,5 +1,8 @@
import {
FieldType,
RelationSchemaField,
RenameColumn,
Table,
TableSchema,
View,
ViewFieldMetadata,
@ -162,23 +165,61 @@ export async function enrichSchema(
view: ViewV2,
tableSchema: TableSchema
): Promise<ViewV2Enriched> {
let schema: TableSchema = {}
const anyViewOrder = Object.values(view.schema || {}).some(
ui => ui.order != null
)
const tableCache: Record<string, Table> = {}
async function populateRelTableSchema(
tableId: string,
viewFields: Record<string, RelationSchemaField>
) {
if (!tableCache[tableId]) {
tableCache[tableId] = await sdk.tables.getTable(tableId)
}
const relTable = tableCache[tableId]
const result: Record<string, RelationSchemaField> = {}
for (const relTableFieldName of Object.keys(relTable.schema)) {
const relTableField = relTable.schema[relTableFieldName]
if ([FieldType.LINK, FieldType.FORMULA].includes(relTableField.type)) {
continue
}
if (relTableField.visible === false) {
continue
}
const isVisible = !!viewFields[relTableFieldName]?.visible
const isReadonly = !!viewFields[relTableFieldName]?.readonly
result[relTableFieldName] = {
visible: isVisible,
readonly: isReadonly,
}
}
return result
}
let schema: ViewV2Enriched["schema"] = {}
const viewSchema = view.schema || {}
const anyViewOrder = Object.values(viewSchema).some(ui => ui.order != null)
for (const key of Object.keys(tableSchema).filter(
key => tableSchema[key].visible !== false
k => tableSchema[k].visible !== false
)) {
// if nothing specified in view, then it is not visible
const ui = view.schema?.[key] || { visible: false }
const ui = viewSchema[key] || { visible: false }
schema[key] = {
...tableSchema[key],
...ui,
order: anyViewOrder ? ui?.order ?? undefined : tableSchema[key].order,
}
}
schema = await sdk.tables.enrichRelationshipSchema(schema)
if (schema[key].type === FieldType.LINK) {
schema[key].columns = await populateRelTableSchema(
schema[key].tableId,
viewSchema[key].columns || {}
)
}
}
return {
...view,

View File

@ -297,12 +297,6 @@ describe("table sdk", () => {
relationshipType: RelationshipType.ONE_TO_MANY,
fieldName: "table",
tableId: "otherTableId",
schema: {
title: {
visible: true,
readonly: true,
},
},
},
},
}
@ -334,7 +328,15 @@ describe("table sdk", () => {
tableId,
schema: {
name: { visible: true },
other: { visible: true },
other: {
visible: true,
columns: {
title: {
visible: true,
readonly: true,
},
},
},
},
}
@ -351,7 +353,7 @@ describe("table sdk", () => {
other: {
...table.schema.other,
visible: true,
schema: {
columns: {
title: {
visible: true,
readonly: true,

View File

@ -247,6 +247,7 @@ export async function outputProcessing<T extends Row[] | Row>(
preserveLinks?: boolean
fromRow?: Row
skipBBReferences?: boolean
fromViewId?: string
} = {
squash: true,
preserveLinks: false,
@ -343,10 +344,9 @@ export async function outputProcessing<T extends Row[] | Row>(
enriched = await processFormulas(table, enriched, { dynamic: true })
if (opts.squash) {
enriched = (await linkRows.squashLinksToPrimaryDisplay(
table,
enriched
)) as Row[]
enriched = await linkRows.squashLinks(table, enriched, {
fromViewId: opts?.fromViewId,
})
}
// remove null properties to match internal API
const isExternal = isExternalTableID(table._id!)

View File

@ -25,12 +25,6 @@ interface BaseRelationshipFieldMetadata
tableId: string
tableRev?: string
subtype?: AutoFieldSubType.CREATED_BY | AutoFieldSubType.UPDATED_BY
schema?: Record<string, RelationSchemaField>
}
export type RelationSchemaField = {
visible?: boolean
readonly?: boolean
}
// External tables use junction tables, internal tables don't require them

View File

@ -1,5 +1,5 @@
import { SearchFilter, SortOrder, SortType } from "../../api"
import { RelationSchemaField, UIFieldMetadata } from "./table"
import { UIFieldMetadata } from "./table"
import { Document } from "../document"
import { DBView } from "../../sdk"
@ -35,7 +35,12 @@ export interface View {
export type ViewFieldMetadata = UIFieldMetadata & {
readonly?: boolean
schema?: Record<string, RelationSchemaField>
columns?: Record<string, RelationSchemaField>
}
export type RelationSchemaField = {
visible?: boolean
readonly?: boolean
}
export interface ViewV2 {

View File

@ -5,6 +5,7 @@ import { WithRequired } from "../shared"
export interface SearchParams {
tableId?: string
viewId?: string
query?: SearchFilters
paginate?: boolean
bookmark?: string | number

View File

@ -1,5 +1,9 @@
import { TableSchema, ViewV2 } from "../documents"
import { FieldSchema, RelationSchemaField, ViewV2 } from "../documents"
export interface ViewV2Enriched extends ViewV2 {
schema?: TableSchema
schema?: {
[key: string]: FieldSchema & {
columns?: Record<string, RelationSchemaField>
}
}
}