Change types

This commit is contained in:
Adria Navarro 2024-08-28 11:36:57 +02:00
parent f8598ff5fa
commit 5a8bb2972b
5 changed files with 25 additions and 18 deletions

View File

@ -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

View File

@ -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)

View File

@ -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
}

View File

@ -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"
)
})

View File

@ -262,7 +262,7 @@ export async function outputProcessing<T extends Row[] | Row>(
}
// attach any linked row information
let enriched = !opts.preserveLinks
? await linkRows.attachFullLinkedDocs(table, safeRows, {
? await linkRows.attachFullLinkedDocs(table.schema, safeRows, {
fromRow: opts?.fromRow,
})
: safeRows