@@ -70,35 +70,58 @@ export abstract class NestedSetSubjectRepository<T extends NestedSetSubjectAbstr
70
70
}
71
71
}
72
72
73
+ const results = await preparedQuery . execute ( )
73
74
74
- const subjects = ( await preparedQuery . execute ( ) ) . map ( subject => this . map ( subject ) )
75
- subjects . forEach ( subject => subject . children = [ ] )
76
- const alreadyProcessedChild = [ ]
75
+ let subjects = [ ]
76
+ const alreadyAddedSubjectIds = [ ]
77
+
78
+ for ( const result of results ) {
79
+
80
+ if ( alreadyAddedSubjectIds [ result [ root . getIdentifierName ( ) ] ] ) {
81
+ continue ;
82
+ }
83
+
84
+ const subject = this . map ( result )
85
+
86
+ //Override children and turn it into simple array
87
+ //@ts -ignore
88
+ subject . children = [ ]
89
+
90
+ subjects . push ( subject )
91
+ alreadyAddedSubjectIds . push ( result [ root . getIdentifierName ( ) ] )
92
+ }
93
+
94
+
95
+ const alreadyProcessedChildren = [ ]
77
96
78
97
for ( const subject of subjects ) {
79
98
80
-
81
- let children = subjects . filter ( innerSubject => innerSubject ?. parent ?. getIdentifier ( ) === subject . getIdentifier ( ) )
82
-
83
- if ( children && children . length >= 1 ) {
84
- const alreadyProcessedIds = [ ]
99
+ let children : any [ ] = [ ]
100
+ const alreadyAddedChildrenOnCurrentSubject = [ ]
85
101
86
- // Filter every child in children for duplicates
87
- children = children . filter ( child => {
88
- if ( alreadyProcessedIds . includes ( child . getIdentifier ( ) ) ) {
89
- return false ;
102
+ // Find all children for given subject
103
+ for ( const potentialChild of subjects ) {
104
+ if ( subject ?. getIdentifier ( ) === potentialChild ?. parent ?. getIdentifier ( ) ) {
105
+
106
+ // Check if any of child is duplicate
107
+ if ( alreadyAddedChildrenOnCurrentSubject . includes ( potentialChild . getIdentifier ( ) ) ) {
108
+ continue ;
90
109
}
91
- alreadyProcessedIds . push ( child . getIdentifier ( ) )
92
- return child
93
- } )
94
110
95
- // Filter duplicates on children level because of relations and joins
96
- children = children . filter ( child => {
97
- return ! alreadyProcessedChild . includes ( child . getIdentifier ( ) )
98
- } )
111
+ // Check if any of child is duplicate in global scope
112
+ if ( alreadyProcessedChildren . includes ( potentialChild . getIdentifier ( ) ) ) {
113
+ continue ;
114
+ }
115
+
116
+ children . push ( potentialChild )
117
+ alreadyAddedChildrenOnCurrentSubject . push ( potentialChild . getIdentifier ( ) )
118
+ }
119
+ }
120
+
121
+ subject . children . push ( ...children )
99
122
100
- subject . children . push ( ... children )
101
- children . forEach ( child => alreadyProcessedChild . push ( child . getIdentifier ( ) ) )
123
+ for ( const child of children ) {
124
+ alreadyProcessedChildren . push ( child . getIdentifier ( ) )
102
125
}
103
126
}
104
127
0 commit comments