@@ -4,39 +4,46 @@ const SearchBuilder = require('./search-builder');
4
4
const utils = require ( '../utils/schema' ) ;
5
5
const FiltersParser = require ( './filters-parser' ) ;
6
6
7
- function HasManyGetter ( parentModel , childModel , opts , params ) {
8
- const OBJECTID_REGEXP = / ^ [ 0 - 9 a - f A - F ] { 24 } $ / ;
9
- const schema = Interface . Schemas . schemas [ utils . getModelName ( childModel ) ] ;
10
- const searchBuilder = new SearchBuilder ( childModel , opts , params ) ;
11
- const filtersParser = new FiltersParser ( childModel , params . timezone , opts ) ;
12
-
13
- function hasPagination ( ) {
14
- return params . page && params . page . number ;
7
+ const OBJECTID_REGEXP = / ^ [ 0 - 9 a - f A - F ] { 24 } $ / ;
8
+
9
+ class HasManyGetter {
10
+ constructor ( parentModel , childModel , opts , params ) {
11
+ this . _parentModel = parentModel ;
12
+ this . _childModel = childModel ;
13
+ this . _params = params ;
14
+ this . _opts = opts ;
15
+ this . _searchBuilder = new SearchBuilder ( childModel , opts , params ) ;
15
16
}
16
17
17
- function getLimit ( ) {
18
- if ( hasPagination ( ) ) {
19
- return parseInt ( params . page . number , 10 ) * params . page . size ;
18
+ _hasPagination ( ) {
19
+ return this . _params . page && this . _params . page . number ;
20
+ }
21
+
22
+ _getLimit ( ) {
23
+ if ( this . _hasPagination ( ) ) {
24
+ return parseInt ( this . _params . page . number , 10 ) * this . _params . page . size ;
20
25
}
21
26
return 5 ;
22
27
}
23
28
24
- function getSkip ( ) {
25
- if ( hasPagination ( ) ) {
26
- return ( parseInt ( params . page . number , 10 ) - 1 ) * params . page . size ;
29
+ _getSkip ( ) {
30
+ if ( this . _hasPagination ( ) ) {
31
+ return ( parseInt ( this . _params . page . number , 10 ) - 1 ) * this . _params . page . size ;
27
32
}
28
33
return 0 ;
29
34
}
30
35
31
- function getProjection ( ) {
36
+ _getProjection ( ) {
32
37
const projection = { } ;
33
- projection [ params . associationName ] = 1 ;
38
+ projection [ this . _params . associationName ] = 1 ;
34
39
projection . _id = 0 ; // eslint-disable-line
35
40
36
41
return projection ;
37
42
}
38
43
39
- function handlePopulate ( query ) {
44
+ _handlePopulate ( query ) {
45
+ const schema = Interface . Schemas . schemas [ utils . getModelName ( this . _childModel ) ] ;
46
+
40
47
_ . each ( schema . fields , ( field ) => {
41
48
if ( field . reference ) {
42
49
query . populate ( {
@@ -46,55 +53,55 @@ function HasManyGetter(parentModel, childModel, opts, params) {
46
53
} ) ;
47
54
}
48
55
49
- async function buildConditions ( recordIds ) {
56
+ async _buildConditions ( recordIds ) {
50
57
const conditions = {
51
58
$and : [ { _id : { $in : recordIds } } ] ,
52
59
} ;
53
60
54
- if ( params . search ) {
55
- const conditionsSearch = await searchBuilder . getConditions ( ) ;
61
+ if ( this . _params . search ) {
62
+ const conditionsSearch = await this . _searchBuilder . getConditions ( ) ;
56
63
conditions . $and . push ( conditionsSearch ) ;
57
64
}
58
65
59
- if ( params . filters ) {
60
- const newFilters = await filtersParser . replaceAllReferences ( params . filters ) ;
66
+ if ( this . _params . filters ) {
67
+ const filtersParser = new FiltersParser ( this . _childModel , this . _params . timezone , this . _opts ) ;
68
+ const newFilters = await filtersParser . replaceAllReferences ( this . _params . filters ) ;
61
69
const newFiltersString = JSON . stringify ( newFilters ) ;
62
70
conditions . $and . push ( await filtersParser . perform ( newFiltersString ) ) ;
63
71
}
64
72
65
73
return conditions ;
66
74
}
67
75
68
- async function getRecordsAndRecordIds ( ) {
69
- let id = params . recordId ;
70
- if ( OBJECTID_REGEXP . test ( params . recordId ) ) {
71
- id = opts . Mongoose . Types . ObjectId ( id ) ;
76
+ async _getRecordsAndRecordIds ( ) {
77
+ let id = this . _params . recordId ;
78
+ if ( OBJECTID_REGEXP . test ( this . _params . recordId ) ) {
79
+ id = this . _opts . Mongoose . Types . ObjectId ( id ) ;
72
80
}
73
81
74
- const parentRecords = await parentModel
82
+ const parentRecords = await this . _parentModel
75
83
. aggregate ( )
76
84
. match ( { _id : id } )
77
- . unwind ( params . associationName )
78
- . project ( getProjection ( ) )
85
+ . unwind ( this . _params . associationName )
86
+ . project ( this . _getProjection ( ) )
79
87
. exec ( ) ;
80
88
81
- const childRecordIds = _ . map ( parentRecords , ( record ) => record [ params . associationName ] ) ;
82
- const conditions = await buildConditions ( childRecordIds ) ;
83
- const query = childModel . find ( conditions ) ;
84
- handlePopulate ( query ) ;
89
+ const childRecordIds = _ . map ( parentRecords , ( record ) => record [ this . _params . associationName ] ) ;
90
+ const conditions = await this . _buildConditions ( childRecordIds ) ;
91
+ const query = this . _childModel . find ( conditions ) ;
92
+ this . _handlePopulate ( query ) ;
85
93
86
94
const childRecords = await query ;
87
95
return [ childRecords , childRecordIds ] ;
88
96
}
89
97
90
- this . perform = async ( ) => {
91
- const [ childRecords , childRecordIds ] = await getRecordsAndRecordIds ( ) ;
92
-
93
- let fieldSort = params . sort ;
98
+ async perform ( ) {
99
+ const [ childRecords , childRecordIds ] = await this . _getRecordsAndRecordIds ( ) ;
100
+ let fieldSort = this . _params . sort ;
94
101
let descending = false ;
95
102
96
- if ( params . sort && ( params . sort [ 0 ] === '-' ) ) {
97
- fieldSort = params . sort . substring ( 1 ) ;
103
+ if ( this . _params . sort && ( this . _params . sort [ 0 ] === '-' ) ) {
104
+ fieldSort = this . _params . sort . substring ( 1 ) ;
98
105
descending = true ;
99
106
}
100
107
@@ -111,19 +118,21 @@ function HasManyGetter(parentModel, childModel, opts, params) {
111
118
let sortedChildRecords = descending ? recordsSorted . reverse ( ) : recordsSorted ;
112
119
let fieldsSearched = null ;
113
120
114
- if ( params . search ) {
115
- fieldsSearched = searchBuilder . getFieldsSearched ( ) ;
121
+ if ( this . _params . search ) {
122
+ fieldsSearched = this . _searchBuilder . getFieldsSearched ( ) ;
116
123
}
117
124
118
- sortedChildRecords = _ . slice ( sortedChildRecords , getSkip ( ) , getSkip ( ) + getLimit ( ) ) ;
125
+ sortedChildRecords = _ . slice (
126
+ sortedChildRecords , this . _getSkip ( ) , this . _getSkip ( ) + this . _getLimit ( ) ,
127
+ ) ;
119
128
120
129
return [ sortedChildRecords , fieldsSearched ] ;
121
- } ;
130
+ }
122
131
123
- this . count = async ( ) => {
124
- const recordsAndRecordIds = await getRecordsAndRecordIds ( ) ;
132
+ async count ( ) {
133
+ const recordsAndRecordIds = await this . _getRecordsAndRecordIds ( ) ;
125
134
return recordsAndRecordIds [ 0 ] . length ;
126
- } ;
135
+ }
127
136
}
128
137
129
138
module . exports = HasManyGetter ;
0 commit comments