Merge branch 'link-record-performance' of github.com:Budibase/budibase into endpoint-renaming
This commit is contained in:
commit
abbf7fcc02
|
@ -82,29 +82,30 @@ exports.updateLinks = async function({
|
||||||
* then an array will be output, object input -> object output.
|
* then an array will be output, object input -> object output.
|
||||||
*/
|
*/
|
||||||
exports.attachLinkInfo = async (instanceId, rows) => {
|
exports.attachLinkInfo = async (instanceId, rows) => {
|
||||||
// handle a single row as well as multiple
|
// handle a single record as well as multiple
|
||||||
let wasArray = true
|
let wasArray = true
|
||||||
if (!(rows instanceof Array)) {
|
if (!(rows instanceof Array)) {
|
||||||
rows = [rows]
|
rows = [rows]
|
||||||
wasArray = false
|
wasArray = false
|
||||||
}
|
}
|
||||||
|
let tableIds = [...new Set(rows.map(el => el.tableId))]
|
||||||
// start by getting all the link values for performance reasons
|
// start by getting all the link values for performance reasons
|
||||||
let responses = await Promise.all(
|
let responses = [].concat.apply(
|
||||||
rows.map(row =>
|
[],
|
||||||
getLinkDocuments({
|
await Promise.all(
|
||||||
instanceId,
|
tableIds.map(tableId =>
|
||||||
tableId: row.tableId,
|
getLinkDocuments({
|
||||||
rowId: row._id,
|
instanceId,
|
||||||
includeDocs: IncludeDocs.EXCLUDE,
|
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
|
// now iterate through the rows and all field information
|
||||||
for (let row of rows) {
|
for (let row of rows) {
|
||||||
// get all links for row, ignore fieldName for now
|
// 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) {
|
for (let linkVal of linkVals) {
|
||||||
// work out which link pertains to this row
|
// work out which link pertains to this row
|
||||||
if (!(row[linkVal.fieldName] instanceof Array)) {
|
if (!(row[linkVal.fieldName] instanceof Array)) {
|
||||||
|
|
|
@ -27,10 +27,12 @@ exports.createLinkView = async instanceId => {
|
||||||
let doc2 = doc.doc2
|
let doc2 = doc.doc2
|
||||||
emit([doc1.tableId, doc1.rowId], {
|
emit([doc1.tableId, doc1.rowId], {
|
||||||
id: doc2.rowId,
|
id: doc2.rowId,
|
||||||
|
thisId: doc1.rowId,
|
||||||
fieldName: doc1.fieldName,
|
fieldName: doc1.fieldName,
|
||||||
})
|
})
|
||||||
emit([doc2.tableId, doc2.rowId], {
|
emit([doc2.tableId, doc2.rowId], {
|
||||||
id: doc1.rowId,
|
id: doc1.rowId,
|
||||||
|
thisId: doc2.rowId,
|
||||||
fieldName: doc2.fieldName,
|
fieldName: doc2.fieldName,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue