diff --git a/packages/server/src/db/linkedRows/index.js b/packages/server/src/db/linkedRows/index.js index 45b4b6236f..7c0b2f5570 100644 --- a/packages/server/src/db/linkedRows/index.js +++ b/packages/server/src/db/linkedRows/index.js @@ -82,29 +82,30 @@ exports.updateLinks = async function({ * then an array will be output, object input -> object output. */ exports.attachLinkInfo = async (instanceId, rows) => { - // handle a single row as well as multiple + // handle a single record as well as multiple let wasArray = true if (!(rows instanceof Array)) { rows = [rows] wasArray = false } + let tableIds = [...new Set(rows.map(el => el.tableId))] // start by getting all the link values for performance reasons - let responses = await Promise.all( - rows.map(row => - getLinkDocuments({ - instanceId, - tableId: row.tableId, - rowId: row._id, - includeDocs: IncludeDocs.EXCLUDE, - }) + let responses = [].concat.apply( + [], + await Promise.all( + tableIds.map(tableId => + getLinkDocuments({ + instanceId, + tableId: tableId, + includeDocs: IncludeDocs.EXCLUDE, + }) + ) ) ) - // can just use an index to access responses, order maintained - let index = 0 // now iterate through the rows and all field information for (let row of rows) { // get all links for row, ignore fieldName for now - const linkVals = responses[index++] + const linkVals = responses.filter(el => el.thisId === row._id) for (let linkVal of linkVals) { // work out which link pertains to this row if (!(row[linkVal.fieldName] instanceof Array)) { diff --git a/packages/server/src/db/linkedRows/linkUtils.js b/packages/server/src/db/linkedRows/linkUtils.js index ea90eb3beb..6af1c79b7d 100644 --- a/packages/server/src/db/linkedRows/linkUtils.js +++ b/packages/server/src/db/linkedRows/linkUtils.js @@ -27,10 +27,12 @@ exports.createLinkView = async instanceId => { let doc2 = doc.doc2 emit([doc1.tableId, doc1.rowId], { id: doc2.rowId, + thisId: doc1.rowId, fieldName: doc1.fieldName, }) emit([doc2.tableId, doc2.rowId], { id: doc1.rowId, + thisId: doc2.rowId, fieldName: doc2.fieldName, }) }