Trim unwanted columns in outputProcessing

This commit is contained in:
Adria Navarro 2024-08-14 12:24:49 +02:00
parent 1a88d9f89b
commit 385c5f6e99
2 changed files with 37 additions and 19 deletions

View File

@ -192,10 +192,10 @@ export function buildSqlFieldList(
function extractRealFields(table: Table, existing: string[] = []) { function extractRealFields(table: Table, existing: string[] = []) {
return Object.entries(table.schema) return Object.entries(table.schema)
.filter( .filter(
column => ([columnName, column]) =>
column[1].type !== FieldType.LINK && column.type !== FieldType.LINK &&
column[1].type !== FieldType.FORMULA && column.type !== FieldType.FORMULA &&
!existing.find((field: string) => field === column[0]) !existing.find((field: string) => field === columnName)
) )
.map(column => `${table.name}.${column[0]}`) .map(column => `${table.name}.${column[0]}`)
} }

View File

@ -19,6 +19,7 @@ import {
User, User,
} from "@budibase/types" } from "@budibase/types"
import { cloneDeep } from "lodash/fp" import { cloneDeep } from "lodash/fp"
import { pick } from "lodash"
import { import {
processInputBBReference, processInputBBReference,
processInputBBReferences, processInputBBReferences,
@ -26,7 +27,11 @@ import {
processOutputBBReferences, processOutputBBReferences,
} from "./bbReferenceProcessor" } from "./bbReferenceProcessor"
import { isExternalTableID } from "../../integrations/utils" import { isExternalTableID } from "../../integrations/utils"
import { helpers } from "@budibase/shared-core" import {
helpers,
PROTECTED_EXTERNAL_COLUMNS,
PROTECTED_INTERNAL_COLUMNS,
} from "@budibase/shared-core"
import { processString } from "@budibase/string-templates" import { processString } from "@budibase/string-templates"
export * from "./utils" export * from "./utils"
@ -53,9 +58,9 @@ export async function processAutoColumn(
row: Row, row: Row,
opts?: AutoColumnProcessingOpts opts?: AutoColumnProcessingOpts
) { ) {
let noUser = !userId const noUser = !userId
let isUserTable = table._id === InternalTables.USER_METADATA const isUserTable = table._id === InternalTables.USER_METADATA
let now = new Date().toISOString() const now = new Date().toISOString()
// if a row doesn't have a revision then it doesn't exist yet // if a row doesn't have a revision then it doesn't exist yet
const creating = !row._rev const creating = !row._rev
// check its not user table, or whether any of the processing options have been disabled // check its not user table, or whether any of the processing options have been disabled
@ -111,7 +116,7 @@ async function processDefaultValues(table: Table, row: Row) {
ctx.user = user ctx.user = user
} }
for (let [key, schema] of Object.entries(table.schema)) { for (const [key, schema] of Object.entries(table.schema)) {
if ("default" in schema && schema.default != null && row[key] == null) { if ("default" in schema && schema.default != null && row[key] == null) {
const processed = await processString(schema.default, ctx) const processed = await processString(schema.default, ctx)
@ -165,10 +170,10 @@ export async function inputProcessing(
row: Row, row: Row,
opts?: AutoColumnProcessingOpts opts?: AutoColumnProcessingOpts
) { ) {
let clonedRow = cloneDeep(row) const clonedRow = cloneDeep(row)
const dontCleanseKeys = ["type", "_id", "_rev", "tableId"] const dontCleanseKeys = ["type", "_id", "_rev", "tableId"]
for (let [key, value] of Object.entries(clonedRow)) { for (const [key, value] of Object.entries(clonedRow)) {
const field = table.schema[key] const field = table.schema[key]
// cleanse fields that aren't in the schema // cleanse fields that aren't in the schema
if (!field) { if (!field) {
@ -268,13 +273,13 @@ export async function outputProcessing<T extends Row[] | Row>(
} }
// process complex types: attachments, bb references... // process complex types: attachments, bb references...
for (let [property, column] of Object.entries(table.schema)) { for (const [property, column] of Object.entries(table.schema)) {
if ( if (
column.type === FieldType.ATTACHMENTS || column.type === FieldType.ATTACHMENTS ||
column.type === FieldType.ATTACHMENT_SINGLE || column.type === FieldType.ATTACHMENT_SINGLE ||
column.type === FieldType.SIGNATURE_SINGLE column.type === FieldType.SIGNATURE_SINGLE
) { ) {
for (let row of enriched) { for (const row of enriched) {
if (row[property] == null) { if (row[property] == null) {
continue continue
} }
@ -299,7 +304,7 @@ export async function outputProcessing<T extends Row[] | Row>(
!opts.skipBBReferences && !opts.skipBBReferences &&
column.type == FieldType.BB_REFERENCE column.type == FieldType.BB_REFERENCE
) { ) {
for (let row of enriched) { for (const row of enriched) {
row[property] = await processOutputBBReferences( row[property] = await processOutputBBReferences(
row[property], row[property],
column.subtype column.subtype
@ -309,14 +314,14 @@ export async function outputProcessing<T extends Row[] | Row>(
!opts.skipBBReferences && !opts.skipBBReferences &&
column.type == FieldType.BB_REFERENCE_SINGLE column.type == FieldType.BB_REFERENCE_SINGLE
) { ) {
for (let row of enriched) { for (const row of enriched) {
row[property] = await processOutputBBReference( row[property] = await processOutputBBReference(
row[property], row[property],
column.subtype column.subtype
) )
} }
} else if (column.type === FieldType.DATETIME && column.timeOnly) { } else if (column.type === FieldType.DATETIME && column.timeOnly) {
for (let row of enriched) { for (const row of enriched) {
if (row[property] instanceof Date) { if (row[property] instanceof Date) {
const hours = row[property].getUTCHours().toString().padStart(2, "0") const hours = row[property].getUTCHours().toString().padStart(2, "0")
const minutes = row[property] const minutes = row[property]
@ -343,14 +348,27 @@ export async function outputProcessing<T extends Row[] | Row>(
)) as Row[] )) as Row[]
} }
// remove null properties to match internal API // remove null properties to match internal API
if (isExternalTableID(table._id!)) { const isExternal = isExternalTableID(table._id!)
for (let row of enriched) { if (isExternal) {
for (let key of Object.keys(row)) { for (const row of enriched) {
for (const key of Object.keys(row)) {
if (row[key] === null) { if (row[key] === null) {
delete row[key] delete row[key]
} }
} }
} }
} }
const protectedColumns = isExternal
? PROTECTED_EXTERNAL_COLUMNS
: PROTECTED_INTERNAL_COLUMNS
const tableFields = Object.keys(table.schema).filter(
f => table.schema[f].visible !== false
)
enriched = enriched.map((r: Row) =>
pick(r, [...tableFields, ...protectedColumns])
)
return (wasArray ? enriched : enriched[0]) as T return (wasArray ? enriched : enriched[0]) as T
} }