Skip to content

Commit

Permalink
feat: accept ids array & fetch all together if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
7sete7 committed Jul 8, 2024
1 parent 9fa4a8d commit 8bf9aa2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
4 changes: 1 addition & 3 deletions src/imports/konsistent/updateReferences/lookupReference.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ export default async function updateLookupReference(metaName, fieldName, field,
const projection = convertStringOfFieldsSeparatedByCommaIntoObjectToFind(Object.keys(updateData).join());

const modified = await collection.find(query, { projection }).toArray();
await Promise.all(modified.map(async (modifiedRecord) =>
updateLookupReferences(metaName, modifiedRecord._id, modifiedRecord, dbSession)
));
await updateLookupReferences(metaName, modified.map(m => m._id), updateData, dbSession);
}

return updateResult.modifiedCount;
Expand Down
17 changes: 13 additions & 4 deletions src/imports/konsistent/updateReferences/lookupReferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default async function updateLookupReferences(metaName, id, data, dbSessi
const references = MetaObject.References[metaName];

if (!isObject(references) || size(keys(references.from)) === 0) {
logger.debug(`No references from ${metaName}`);
return;
}

Expand Down Expand Up @@ -55,21 +56,29 @@ export default async function updateLookupReferences(metaName, id, data, dbSessi
}

if (Object.keys(referencesToUpdate).length === 0) {
logger.debug(`No references to update for ${metaName}`);
return;
}

const record = await collection.findOne({ _id: id }, { session: dbSession });
if (Array.isArray(id) && id.length > 1) {
const records = await collection.find({ _id: { $in: id } }, { session: dbSession }).toArray();
return await Promise.all(records.map(record => processReferences({ referencesToUpdate, metaName, record, dbSession })));
}

const record = await collection.findOne({ _id: [].concat(id)[0] }, { session: dbSession });
if (!record) {
return logger.error(`Can't find record ${id} from ${metaName}`);
}

logger.debug(`Updating references for ${metaName} - ${Object.keys(referencesToUpdate).join(", ")}`);

await BluebirdPromise.map(Object.keys(referencesToUpdate), async referenceDocumentName => {
return await processReferences({ referencesToUpdate, metaName, record, dbSession });
}
const processReferences = async ({ referencesToUpdate, metaName, record, dbSession }) => {
return await BluebirdPromise.map(Object.keys(referencesToUpdate), async referenceDocumentName => {
const fields = referencesToUpdate[referenceDocumentName];
await BluebirdPromise.map(Object.keys(fields), async fieldName => {
const field = fields[fieldName];
return updateLookupReference(referenceDocumentName, fieldName, field, record, metaName, dbSession);
}, { concurrency: 5 });
}, { concurrency: 5 });
}
}

0 comments on commit 8bf9aa2

Please sign in to comment.