Increased power of tree searching

This commit is contained in:
Stefan Zermatten
2023-06-26 14:45:19 +02:00
parent d4cac831e6
commit 7562e29fac
8 changed files with 205 additions and 63 deletions

View File

@@ -1,4 +1,4 @@
import { union, difference, sortBy, findLast } from 'lodash';
import { union, difference, sortBy, findLast, intersection } from 'lodash';
export function nodeArrayToTree(nodes) {
// Store a dict and list of all the nodes
@@ -83,9 +83,15 @@ export default function nodesToTree({
docs.forEach(doc => {
ancestorIds = union(ancestorIds, doc.ancestors.map(ref => ref.id));
});
// Remove the IDs of docs we have already found
// Get all the docs that are also ancestors and mark them
docs.forEach(doc => {
if (ancestorIds.includes(doc._id)) {
doc._ancestorOfMatchedDocument = true;
}
});
// Remove the ancestor IDs of docs we have already found
ancestorIds = difference(ancestorIds, docIds);
// Get the docs from the collection, don't worry about `removed` docs,
// Get the ancestor docs from the collection, don't worry about `removed` docs,
// if their descendant was not removed, neither are they
ancestors = collection.find({ _id: { $in: ancestorIds } }).map(doc => {
// Mark that the nodes are ancestors of the found nodes