diff --git a/packages/server/src/api/controllers/table/bulkFormula.ts b/packages/server/src/api/controllers/table/bulkFormula.ts index 4cc3849c5b..8f03036c0d 100644 --- a/packages/server/src/api/controllers/table/bulkFormula.ts +++ b/packages/server/src/api/controllers/table/bulkFormula.ts @@ -6,13 +6,9 @@ import isEqual from "lodash/isEqual" import uniq from "lodash/uniq" import { updateAllFormulasInTable } from "../row/staticFormula" import { context } from "@budibase/backend-core" -import { - FieldSchema, - FormulaFieldMetadata, - RelationshipFieldMetadata, - Table, -} from "@budibase/types" +import { FieldSchema, FormulaFieldMetadata, Table } from "@budibase/types" import sdk from "../../../sdk" +import { isRelationshipColumn } from "../../../db/utils" function isStaticFormula( column: FieldSchema @@ -104,10 +100,6 @@ async function checkIfFormulaNeedsCleared( } } -function isLink(column: FieldSchema): column is RelationshipFieldMetadata { - return column.type === FieldTypes.LINK -} - /** * This function adds a note to related tables that they are * used in a static formula - so that the link controller @@ -127,7 +119,9 @@ async function updateRelatedFormulaLinksOnTables( // clone the tables, so we can compare at end const initialTables = cloneDeep(tables) // first find the related column names - const relatedColumns = Object.values(table.schema).filter(isLink) + const relatedColumns = Object.values(table.schema).filter( + isRelationshipColumn + ) // we start by removing the formula field from all tables for (let otherTable of tables) { if (!otherTable.relatedFormula) { diff --git a/packages/server/src/db/linkedRows/linkUtils.ts b/packages/server/src/db/linkedRows/linkUtils.ts index c7db7d522a..ba0d2b3b0e 100644 --- a/packages/server/src/db/linkedRows/linkUtils.ts +++ b/packages/server/src/db/linkedRows/linkUtils.ts @@ -1,13 +1,9 @@ -import { ViewName, getQueryIndex } from "../utils" +import { ViewName, getQueryIndex, isRelationshipColumn } from "../utils" import { FieldTypes } from "../../constants" import { createLinkView } from "../views/staticViews" import { context, logging } from "@budibase/backend-core" -import { - FieldSchema, - LinkDocument, - LinkDocumentValue, - Table, -} from "@budibase/types" +import { LinkDocument, LinkDocumentValue, Table } from "@budibase/types" + export { createLinkView } from "../views/staticViews" /** @@ -93,7 +89,7 @@ export function getUniqueByProp(array: any[], prop: string) { export function getLinkedTableIDs(table: Table) { return Object.values(table.schema) - .filter((column: FieldSchema) => column.type === FieldTypes.LINK) + .filter(isRelationshipColumn) .map(column => column.tableId) } @@ -114,7 +110,7 @@ export function getRelatedTableForField(table: Table, fieldName: string) { // look to see if its on the table, straight in the schema const field = table.schema[fieldName] if (field != null) { - return field.tableId + return (field as any).tableId } for (let column of Object.values(table.schema)) { if (column.type === FieldTypes.LINK && column.fieldName === fieldName) { diff --git a/packages/server/src/db/utils.ts b/packages/server/src/db/utils.ts index abea725707..0ffc2ac43c 100644 --- a/packages/server/src/db/utils.ts +++ b/packages/server/src/db/utils.ts @@ -1,6 +1,12 @@ import newid from "./newid" import { db as dbCore } from "@budibase/backend-core" -import { DocumentType, VirtualDocumentType } from "@budibase/types" +import { + DocumentType, + FieldSchema, + RelationshipFieldMetadata, + VirtualDocumentType, +} from "@budibase/types" +import { FieldTypes } from "src/constants" export { DocumentType, VirtualDocumentType } from "@budibase/types" type Optional = string | null @@ -307,3 +313,9 @@ export function extractViewInfoFromID(viewId: string) { tableId: res!.groups!["tableId"], } } + +export function isRelationshipColumn( + column: FieldSchema +): column is RelationshipFieldMetadata { + return column.type === FieldTypes.LINK +} diff --git a/packages/server/src/sdk/app/tables/validation.ts b/packages/server/src/sdk/app/tables/validation.ts index f97cce39e3..816ddd14ff 100644 --- a/packages/server/src/sdk/app/tables/validation.ts +++ b/packages/server/src/sdk/app/tables/validation.ts @@ -1,11 +1,9 @@ import { AutoReason, Datasource, - FieldSchema, FieldType, RelationshipType, } from "@budibase/types" -import { FieldTypes } from "../../../constants" function checkForeignKeysAreAutoColumns(datasource: Datasource) { if (!datasource.entities) {