diff --git a/packages/client/src/api/rows.js b/packages/client/src/api/rows.js index 21f8ec1f98..e3557927aa 100644 --- a/packages/client/src/api/rows.js +++ b/packages/client/src/api/rows.js @@ -112,16 +112,24 @@ export const enrichRows = async (rows, tableId) => { if (!Array.isArray(rows)) { return [] } - if (rows.length && tableId) { - // Fetch table schema so we can check column types - const tableDefinition = await fetchTableDefinition(tableId) - const schema = tableDefinition && tableDefinition.schema - if (schema) { - const keys = Object.keys(schema) - rows.forEach(row => { + if (rows.length) { + // map of tables, incase a row being loaded is not from the same table + const tables = {} + for (let row of rows) { + // fallback to passed in tableId if row doesn't have it specified + let rowTableId = row.tableId || tableId + let table = tables[rowTableId] + if (!table) { + // Fetch table schema so we can check column types + table = await fetchTableDefinition(rowTableId) + tables[rowTableId] = table + } + const schema = table?.schema + if (schema) { + const keys = Object.keys(schema) for (let key of keys) { const type = schema[key].type - if (type === "link") { + if (type === "link" && Array.isArray(row[key])) { // Enrich row a string join of relationship fields row[`${key}_text`] = row[key] @@ -137,7 +145,7 @@ export const enrichRows = async (rows, tableId) => { row[`${key}_first`] = url } } - }) + } } } return rows diff --git a/packages/server/src/db/linkedRows/index.js b/packages/server/src/db/linkedRows/index.js index 840f6454b5..34be44336c 100644 --- a/packages/server/src/db/linkedRows/index.js +++ b/packages/server/src/db/linkedRows/index.js @@ -203,19 +203,17 @@ exports.attachFullLinkedDocs = async (ctx, table, rows) => { exports.squashLinksToPrimaryDisplay = async (appId, table, enriched) => { const db = new CouchDB(appId) // will populate this as we find them - const linkedTables = [] - for (let [column, schema] of Object.entries(table.schema)) { - if (schema.type !== FieldTypes.LINK) { - continue - } - for (let row of enriched) { - if (!row[column] || !row[column].length) { + const linkedTables = [table] + for (let row of enriched) { + // this only fetches the table if its not already in array + const rowTable = await getLinkedTable(db, row.tableId, linkedTables) + for (let [column, schema] of Object.entries(rowTable.schema)) { + if (schema.type !== FieldTypes.LINK || !Array.isArray(row[column])) { continue } const newLinks = [] for (let link of row[column]) { const linkTblId = link.tableId || getRelatedTableForField(table, column) - // this only fetches the table if its not already in array const linkedTable = await getLinkedTable(db, linkTblId, linkedTables) const obj = { _id: link._id } if (link[linkedTable.primaryDisplay]) {