Skip to content

Commit aafe6cf

Browse files
author
kamilfronczak
committed
optimized version for readable tree
1 parent 4bf7cad commit aafe6cf

File tree

3 files changed

+47
-22
lines changed

3 files changed

+47
-22
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mikroorm-nested-set",
3-
"version": "1.1.8",
3+
"version": "1.2.0",
44
"types": "src/index.d.ts",
55
"description": "Package for MikroORM nested set feature",
66
"main": "src/index.js",

src/model/nested-set-subject.abstract.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export abstract class NestedSetSubjectAbstract<T> {
3131

3232
abstract getIdentifier() : number | string
3333

34+
abstract getIdentifierName() : string
35+
3436
getNextChild(computeSessionToken: string) : NestedSetSubjectAbstract<T> {
3537

3638
if(this.computeSessionToken !== computeSessionToken) {

src/repository/nested-set-subject.repository.ts

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,35 +70,58 @@ export abstract class NestedSetSubjectRepository<T extends NestedSetSubjectAbstr
7070
}
7171
}
7272

73+
const results = await preparedQuery.execute()
7374

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 = []
7796

7897
for(const subject of subjects) {
7998

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 = []
85101

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;
90109
}
91-
alreadyProcessedIds.push(child.getIdentifier())
92-
return child
93-
})
94110

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)
99122

100-
subject.children.push(...children)
101-
children.forEach(child => alreadyProcessedChild.push(child.getIdentifier()))
123+
for(const child of children) {
124+
alreadyProcessedChildren.push(child.getIdentifier())
102125
}
103126
}
104127

0 commit comments

Comments
 (0)