Merge pull request #684 from Budibase/link-record-performance

Small change that drastically improves the performance of linked records
This commit is contained in:
Michael Drury 2020-10-12 10:47:57 +01:00 committed by GitHub
commit 058149981e
2 changed files with 14 additions and 11 deletions

View File

@ -1,5 +1,6 @@
const LinkController = require("./LinkController") const LinkController = require("./LinkController")
const { IncludeDocs, getLinkDocuments, createLinkView } = require("./linkUtils") const { IncludeDocs, getLinkDocuments, createLinkView } = require("./linkUtils")
const _ = require("lodash")
/** /**
* This functionality makes sure that when records with links are created, updated or deleted they are processed * This functionality makes sure that when records with links are created, updated or deleted they are processed
@ -88,23 +89,23 @@ exports.attachLinkInfo = async (instanceId, records) => {
records = [records] records = [records]
wasArray = false wasArray = false
} }
let modelIds = [...new Set(records.map(el => el.modelId))]
// 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 = _.flatten(
records.map(record => await Promise.all(
getLinkDocuments({ modelIds.map(modelId =>
instanceId, getLinkDocuments({
modelId: record.modelId, instanceId,
recordId: record._id, modelId: modelId,
includeDocs: IncludeDocs.EXCLUDE, includeDocs: IncludeDocs.EXCLUDE,
}) })
)
) )
) )
// can just use an index to access responses, order maintained
let index = 0
// now iterate through the records and all field information // now iterate through the records and all field information
for (let record of records) { for (let record of records) {
// get all links for record, ignore fieldName for now // get all links for record, ignore fieldName for now
const linkVals = responses[index++] const linkVals = responses.filter(el => el.thisId === record._id)
for (let linkVal of linkVals) { for (let linkVal of linkVals) {
// work out which link pertains to this record // work out which link pertains to this record
if (!(record[linkVal.fieldName] instanceof Array)) { if (!(record[linkVal.fieldName] instanceof Array)) {

View File

@ -27,10 +27,12 @@ exports.createLinkView = async instanceId => {
let doc2 = doc.doc2 let doc2 = doc.doc2
emit([doc1.modelId, doc1.recordId], { emit([doc1.modelId, doc1.recordId], {
id: doc2.recordId, id: doc2.recordId,
thisId: doc1.recordId,
fieldName: doc1.fieldName, fieldName: doc1.fieldName,
}) })
emit([doc2.modelId, doc2.recordId], { emit([doc2.modelId, doc2.recordId], {
id: doc1.recordId, id: doc1.recordId,
thisId: doc2.recordId,
fieldName: doc2.fieldName, fieldName: doc2.fieldName,
}) })
} }