From 50b99ef54fea808ab5e757784f7e02146d4ef420 Mon Sep 17 00:00:00 2001 From: Stefan Zermatten Date: Sun, 21 Jun 2020 23:24:07 +0200 Subject: [PATCH] Improved performance of adding library properties with many decendants --- app/imports/api/creature/CreatureProperties.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/app/imports/api/creature/CreatureProperties.js b/app/imports/api/creature/CreatureProperties.js index e09c93e4..de3027b3 100644 --- a/app/imports/api/creature/CreatureProperties.js +++ b/app/imports/api/creature/CreatureProperties.js @@ -158,7 +158,7 @@ const insertPropertyFromLibraryNode = new ValidatedMethod({ 'ancestors.id': nodeId, removed: {$ne: true}, }).fetch(); - // The root node is last in the array of nodes + // The root node is last in the array of nodes nodes.push(node); // re-map all the ancestors @@ -181,17 +181,16 @@ const insertPropertyFromLibraryNode = new ValidatedMethod({ }); // Insert the creature properties - let docId; - nodes.forEach(doc => { - docId = CreatureProperties.insert(doc); - }); + let insertedDocIds = CreatureProperties.batchInsert(nodes); + + // get the root inserted doc + let rootId = insertedDocIds[insertedDocIds.length - 1]; // Recompute the creatures doc was attached to - let doc = CreatureProperties.findOne(docId); - recomputeCreatures(doc); + recomputeCreatures(node); // Return the docId of the last property, the inserted root property - return docId; + return rootId; }, })