import * as _ from "lodash" ; import CollectionNode from "../nodes/CollectionNode" ; param childCollectionNode export default async function processRecursiveNode ( childCollectionNode ) { const parentResults = childCollectionNode . parent . results ; const allResults = [ ] ; const linkStorageField = childCollectionNode . linkStorageField ; const linkName = childCollectionNode . name ; const isMany = childCollectionNode . linker . isMany ( ) ; if ( childCollectionNode . isVirtual ) { for ( const parentResult of parentResults ) { const results = await childCollectionNode . toArray ( { [ linkStorageField ] : { $in : [ parentResult . _id ] } } , parentResult ) ; parentResult [ linkName ] = results ; allResults . push ( ... results ) ; } } else { for ( const parentResult of parentResults ) { if ( ! parentResult [ linkStorageField ] ) { parentResult [ linkStorageField ] = [ ] ; continue ; } const $in = isMany ? parentResult [ linkStorageField ] : [ parentResult [ linkStorageField ] ] ; const results = await childCollectionNode . toArray ( { _id : { $in } } , parentResult ) ; allResults . push ( ... results ) ; parentResult [ linkName ] = results ; } } childCollectionNode . results = _ . uniqBy ( allResults , id => id . toString ( ) ) ; }