@directus/api
Version:
Directus is a real-time API and App dashboard for managing SQL database content
29 lines (28 loc) • 1.2 kB
JavaScript
import { isSystemCollection } from '@directus/system-data';
/**
* Returns a list of all related collections for a given collection.
* Or in math terms, returns the [strongly connected component](https://en.wikipedia.org/wiki/Strongly_connected_component) that a given node belongs to.
*/
export function getCollectionRelationList(collection, collectionRelationTree) {
const collectionRelationList = new Set();
traverseCollectionRelationTree(collection);
return collectionRelationList;
function traverseCollectionRelationTree(root) {
const relationTree = collectionRelationTree.get(root);
if (!relationTree)
return;
for (const relationNode of relationTree) {
addRelationNode(relationNode);
}
}
function addRelationNode(node) {
// system collections cannot have duplication fields and therfore can be skipped
if (isSystemCollection(node))
return;
// skip circular reference and existing linked nodes
if (node === collection || collectionRelationList.has(node))
return;
collectionRelationList.add(node);
traverseCollectionRelationTree(node);
}
}