File tree Expand file tree Collapse file tree 1 file changed +19
-12
lines changed Expand file tree Collapse file tree 1 file changed +19
-12
lines changed Original file line number Diff line number Diff line change @@ -30,20 +30,14 @@ export abstract class NestedSetSubjectRepository<T extends NestedSetSubjectAbstr
30
30
relationName : string , // 'b.tags'
31
31
alias : string // 't'
32
32
} [ ]
33
- } = { } ) : Promise < NestedSetSubjectAbstract < T > > {
34
- const rootsQuery = this . createQueryBuilder ( 'root' )
35
- . where ( {
36
- parent : null
37
- } )
38
-
39
- const rootResult = ( await rootsQuery . execute ( ) ) . map ( subject => this . map ( subject ) )
33
+ root ?: NestedSetSubjectAbstract < T > | T
34
+ } = { } ) : Promise < NestedSetSubjectAbstract < T > > {
40
35
41
- if ( rootResult . length === 0 ) {
42
- return null
36
+ let root = options . root ;
37
+ if ( ! root ) {
38
+ root = this . findRoot ( )
43
39
}
44
-
45
- const root = rootResult [ 0 ]
46
-
40
+
47
41
let query = {
48
42
left : {
49
43
$gte : root . left
@@ -109,6 +103,19 @@ export abstract class NestedSetSubjectRepository<T extends NestedSetSubjectAbstr
109
103
return subjects [ 0 ] as NestedSetSubjectAbstract < T >
110
104
}
111
105
106
+ private async findRoot ( ) : Promise < NestedSetSubjectAbstract < T > > {
107
+ const rootsQuery = this . createQueryBuilder ( 'root' )
108
+ . where ( {
109
+ parent : null
110
+ } )
111
+
112
+ const rootResult = ( await rootsQuery . execute ( ) ) . map ( subject => this . map ( subject ) )
112
113
114
+ if ( rootResult . length === 0 ) {
115
+ return null
116
+ }
117
+
118
+ return rootResult [ 0 ] as NestedSetSubjectAbstract < T > ;
119
+ }
113
120
114
121
}
You can’t perform that action at this time.
0 commit comments