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, PatchRowRequest,
PatchRowResponse, PatchRowResponse,
Row, Row,
Table,
UserCtx, UserCtx,
} from "@budibase/types" } from "@budibase/types"
import sdk from "../../../sdk" import sdk from "../../../sdk"
@ -197,7 +196,7 @@ export async function fetchEnrichedRow(ctx: UserCtx) {
) )
// get the linked tables // get the linked tables
const linkTableIds = getLinkedTableIDs(table as Table) const linkTableIds = getLinkedTableIDs(table.schema)
const linkTables = await sdk.tables.getTables(linkTableIds) const linkTables = await sdk.tables.getTables(linkTableIds)
// perform output processing // perform output processing

View File

@ -18,6 +18,7 @@ import {
LinkDocumentValue, LinkDocumentValue,
Row, Row,
Table, Table,
TableSchema,
} from "@budibase/types" } from "@budibase/types"
import sdk from "../../sdk" import sdk from "../../sdk"
@ -46,8 +47,8 @@ export const EventType = {
TABLE_DELETE: "table:delete", TABLE_DELETE: "table:delete",
} }
function clearRelationshipFields(table: Table, rows: Row[]) { function clearRelationshipFields(schema: TableSchema, rows: Row[]) {
for (let [key, field] of Object.entries(table.schema)) { for (let [key, field] of Object.entries(schema)) {
if (field.type === FieldType.LINK) { if (field.type === FieldType.LINK) {
rows = rows.map(row => { rows = rows.map(row => {
delete row[key] delete row[key]
@ -158,11 +159,11 @@ export async function updateLinks(args: {
* @return returns the rows with all of the enriched relationships on it. * @return returns the rows with all of the enriched relationships on it.
*/ */
export async function attachFullLinkedDocs( export async function attachFullLinkedDocs(
table: Table, schema: TableSchema,
rows: Row[], rows: Row[],
opts?: { fromRow?: Row } opts?: { fromRow?: Row }
) { ) {
const linkedTableIds = getLinkedTableIDs(table) const linkedTableIds = getLinkedTableIDs(schema)
if (linkedTableIds.length === 0) { if (linkedTableIds.length === 0) {
return rows return rows
} }
@ -182,7 +183,7 @@ export async function attachFullLinkedDocs(
} }
const linkedTables = response[1] as Table[] const linkedTables = response[1] as Table[]
// clear any existing links that could be dupe'd // 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 // now get the docs and combine into the rows
let linked: Row[] = [] let linked: Row[] = []
if (linksWithoutFromRow.length > 0) { if (linksWithoutFromRow.length > 0) {
@ -201,7 +202,7 @@ export async function attachFullLinkedDocs(
} }
if (linkedRow) { if (linkedRow) {
const linkedTableId = const linkedTableId =
linkedRow.tableId || getRelatedTableForField(table, link.fieldName) linkedRow.tableId || getRelatedTableForField(schema, link.fieldName)
const linkedTable = linkedTables.find( const linkedTable = linkedTables.find(
table => table._id === linkedTableId table => table._id === linkedTableId
) )
@ -263,7 +264,8 @@ export async function squashLinksToPrimaryDisplay(
} }
const newLinks = [] const newLinks = []
for (let link of row[column]) { 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 linkedTable = await getLinkedTable(linkTblId!, linkedTables)
const obj: any = { _id: link._id } const obj: any = { _id: link._id }
obj.primaryDisplay = getPrimaryDisplayValue(link, linkedTable) obj.primaryDisplay = getPrimaryDisplayValue(link, linkedTable)

View File

@ -7,6 +7,7 @@ import {
LinkDocument, LinkDocument,
LinkDocumentValue, LinkDocumentValue,
Table, Table,
TableSchema,
} from "@budibase/types" } from "@budibase/types"
import sdk from "../../sdk" import sdk from "../../sdk"
@ -121,8 +122,8 @@ export function getUniqueByProp(array: any[], prop: string) {
return filteredArray return filteredArray
} }
export function getLinkedTableIDs(table: Table): string[] { export function getLinkedTableIDs(schema: TableSchema): string[] {
return Object.values(table.schema) return Object.values(schema)
.filter(isRelationshipColumn) .filter(isRelationshipColumn)
.map(column => column.tableId) .map(column => column.tableId)
} }
@ -139,13 +140,16 @@ export async function getLinkedTable(id: string, tables: Table[]) {
return linkedTable 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 // 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) { if (field?.type === FieldType.LINK) {
return field.tableId 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) { if (column.type === FieldType.LINK && column.fieldName === fieldName) {
return column.tableId return column.tableId
} }

View File

@ -34,7 +34,7 @@ describe("test link functionality", () => {
}) })
describe("getRelatedTableForField", () => { describe("getRelatedTableForField", () => {
let link = basicTable() const link = basicTable()
link.schema.link = { link.schema.link = {
name: "link", name: "link",
relationshipType: RelationshipType.ONE_TO_MANY, relationshipType: RelationshipType.ONE_TO_MANY,
@ -44,11 +44,13 @@ describe("test link functionality", () => {
} }
it("should get the field from the table directly", () => { 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", () => { it("should get the field from the link", () => {
expect(linkUtils.getRelatedTableForField(link, "otherLink")).toBe( expect(linkUtils.getRelatedTableForField(link.schema, "otherLink")).toBe(
"tableID" "tableID"
) )
}) })

View File

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