From 5a8bb2972b3c0f9fc3fb905902707b631d962004 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 28 Aug 2024 11:36:57 +0200 Subject: [PATCH] Change types --- .../server/src/api/controllers/row/internal.ts | 3 +-- packages/server/src/db/linkedRows/index.ts | 16 +++++++++------- packages/server/src/db/linkedRows/linkUtils.ts | 14 +++++++++----- packages/server/src/db/tests/linkTests.spec.ts | 8 +++++--- .../server/src/utilities/rowProcessor/index.ts | 2 +- 5 files changed, 25 insertions(+), 18 deletions(-) diff --git a/packages/server/src/api/controllers/row/internal.ts b/packages/server/src/api/controllers/row/internal.ts index b2982a3542..6b9d03f4ef 100644 --- a/packages/server/src/api/controllers/row/internal.ts +++ b/packages/server/src/api/controllers/row/internal.ts @@ -16,7 +16,6 @@ import { PatchRowRequest, PatchRowResponse, Row, - Table, UserCtx, } from "@budibase/types" import sdk from "../../../sdk" @@ -197,7 +196,7 @@ export async function fetchEnrichedRow(ctx: UserCtx) { ) // get the linked tables - const linkTableIds = getLinkedTableIDs(table as Table) + const linkTableIds = getLinkedTableIDs(table.schema) const linkTables = await sdk.tables.getTables(linkTableIds) // perform output processing diff --git a/packages/server/src/db/linkedRows/index.ts b/packages/server/src/db/linkedRows/index.ts index 2da7e212b9..9a86df7041 100644 --- a/packages/server/src/db/linkedRows/index.ts +++ b/packages/server/src/db/linkedRows/index.ts @@ -18,6 +18,7 @@ import { LinkDocumentValue, Row, Table, + TableSchema, } from "@budibase/types" import sdk from "../../sdk" @@ -46,8 +47,8 @@ export const EventType = { TABLE_DELETE: "table:delete", } -function clearRelationshipFields(table: Table, rows: Row[]) { - for (let [key, field] of Object.entries(table.schema)) { +function clearRelationshipFields(schema: TableSchema, rows: Row[]) { + for (let [key, field] of Object.entries(schema)) { if (field.type === FieldType.LINK) { rows = rows.map(row => { delete row[key] @@ -158,11 +159,11 @@ export async function updateLinks(args: { * @return returns the rows with all of the enriched relationships on it. */ export async function attachFullLinkedDocs( - table: Table, + schema: TableSchema, rows: Row[], opts?: { fromRow?: Row } ) { - const linkedTableIds = getLinkedTableIDs(table) + const linkedTableIds = getLinkedTableIDs(schema) if (linkedTableIds.length === 0) { return rows } @@ -182,7 +183,7 @@ export async function attachFullLinkedDocs( } const linkedTables = response[1] as Table[] // clear any existing links that could be dupe'd - rows = clearRelationshipFields(table, rows) + rows = clearRelationshipFields(schema, rows) // now get the docs and combine into the rows let linked: Row[] = [] if (linksWithoutFromRow.length > 0) { @@ -201,7 +202,7 @@ export async function attachFullLinkedDocs( } if (linkedRow) { const linkedTableId = - linkedRow.tableId || getRelatedTableForField(table, link.fieldName) + linkedRow.tableId || getRelatedTableForField(schema, link.fieldName) const linkedTable = linkedTables.find( table => table._id === linkedTableId ) @@ -263,7 +264,8 @@ export async function squashLinksToPrimaryDisplay( } const newLinks = [] for (let link of row[column]) { - const linkTblId = link.tableId || getRelatedTableForField(table, column) + const linkTblId = + link.tableId || getRelatedTableForField(table.schema, column) const linkedTable = await getLinkedTable(linkTblId!, linkedTables) const obj: any = { _id: link._id } obj.primaryDisplay = getPrimaryDisplayValue(link, linkedTable) diff --git a/packages/server/src/db/linkedRows/linkUtils.ts b/packages/server/src/db/linkedRows/linkUtils.ts index c30d62ef35..4cedc4f86a 100644 --- a/packages/server/src/db/linkedRows/linkUtils.ts +++ b/packages/server/src/db/linkedRows/linkUtils.ts @@ -7,6 +7,7 @@ import { LinkDocument, LinkDocumentValue, Table, + TableSchema, } from "@budibase/types" import sdk from "../../sdk" @@ -121,8 +122,8 @@ export function getUniqueByProp(array: any[], prop: string) { return filteredArray } -export function getLinkedTableIDs(table: Table): string[] { - return Object.values(table.schema) +export function getLinkedTableIDs(schema: TableSchema): string[] { + return Object.values(schema) .filter(isRelationshipColumn) .map(column => column.tableId) } @@ -139,13 +140,16 @@ export async function getLinkedTable(id: string, tables: Table[]) { return linkedTable } -export function getRelatedTableForField(table: Table, fieldName: string) { +export function getRelatedTableForField( + schema: TableSchema, + fieldName: string +) { // look to see if its on the table, straight in the schema - const field = table.schema[fieldName] + const field = schema[fieldName] if (field?.type === FieldType.LINK) { return field.tableId } - for (let column of Object.values(table.schema)) { + for (let column of Object.values(schema)) { if (column.type === FieldType.LINK && column.fieldName === fieldName) { return column.tableId } diff --git a/packages/server/src/db/tests/linkTests.spec.ts b/packages/server/src/db/tests/linkTests.spec.ts index 3dbcb32011..2d6636388e 100644 --- a/packages/server/src/db/tests/linkTests.spec.ts +++ b/packages/server/src/db/tests/linkTests.spec.ts @@ -34,7 +34,7 @@ describe("test link functionality", () => { }) describe("getRelatedTableForField", () => { - let link = basicTable() + const link = basicTable() link.schema.link = { name: "link", relationshipType: RelationshipType.ONE_TO_MANY, @@ -44,11 +44,13 @@ describe("test link functionality", () => { } it("should get the field from the table directly", () => { - expect(linkUtils.getRelatedTableForField(link, "link")).toBe("tableID") + expect(linkUtils.getRelatedTableForField(link.schema, "link")).toBe( + "tableID" + ) }) it("should get the field from the link", () => { - expect(linkUtils.getRelatedTableForField(link, "otherLink")).toBe( + expect(linkUtils.getRelatedTableForField(link.schema, "otherLink")).toBe( "tableID" ) }) diff --git a/packages/server/src/utilities/rowProcessor/index.ts b/packages/server/src/utilities/rowProcessor/index.ts index 4b2fd83882..a20a3b25e1 100644 --- a/packages/server/src/utilities/rowProcessor/index.ts +++ b/packages/server/src/utilities/rowProcessor/index.ts @@ -262,7 +262,7 @@ export async function outputProcessing( } // attach any linked row information let enriched = !opts.preserveLinks - ? await linkRows.attachFullLinkedDocs(table, safeRows, { + ? await linkRows.attachFullLinkedDocs(table.schema, safeRows, { fromRow: opts?.fromRow, }) : safeRows