@@ -4,27 +4,58 @@ const Interface = require('forest-express');
4
4
const orm = require ( '../utils/orm' ) ;
5
5
const QueryBuilder = require ( './query-builder' ) ;
6
6
const SearchBuilder = require ( './search-builder' ) ;
7
+ const FiltersParser = require ( './filters-parser' ) ;
7
8
const CompositeKeysManager = require ( './composite-keys-manager' ) ;
8
9
const extractRequestedFields = require ( './requested-fields-extractor' ) ;
10
+ const Operators = require ( '../utils/operators' ) ;
9
11
10
- function HasManyGetter ( model , association , opts , params ) {
11
- const queryBuilder = new QueryBuilder ( model , opts , params ) ;
12
- const schema = Interface . Schemas . schemas [ association . name ] ;
13
- const primaryKeyModel = _ . keys ( model . primaryKeys ) [ 0 ] ;
14
-
15
- function getFieldNamesRequested ( ) {
16
- return extractRequestedFields ( params . fields , association , Interface . Schemas . schemas ) ;
12
+ class HasManyGetter {
13
+ constructor ( model , association , options , params ) {
14
+ this . model = model ;
15
+ this . association = association ;
16
+ this . params = params ;
17
+ this . queryBuilder = new QueryBuilder ( model , options , params ) ;
18
+ this . schema = Interface . Schemas . schemas [ association . name ] ;
19
+ [ this . primaryKeyModel ] = _ . keys ( model . primaryKeys ) ;
20
+ this . operators = Operators . getInstance ( options ) ;
21
+ this . filtersParser = new FiltersParser ( this . schema , params . timezone , options ) ;
22
+ this . fieldNamesRequested = extractRequestedFields (
23
+ params . fields , association , Interface . Schemas . schemas ,
24
+ ) ;
25
+ this . searchBuilder = new SearchBuilder (
26
+ association ,
27
+ options ,
28
+ params ,
29
+ this . fieldNamesRequested ,
30
+ ) ;
17
31
}
18
32
19
- const fieldNamesRequested = getFieldNamesRequested ( ) ;
20
- const searchBuilder = new SearchBuilder ( association , opts , params , fieldNamesRequested ) ;
21
- const where = searchBuilder . perform ( params . associationName ) ;
22
- const include = queryBuilder . getIncludes ( association , fieldNamesRequested ) ;
33
+ async buildWhereConditions ( { associationName, search, filters } ) {
34
+ const { AND } = this . operators ;
35
+ const where = { [ AND ] : [ ] } ;
36
+
37
+ if ( search ) {
38
+ const searchCondition = this . searchBuilder . perform ( associationName ) ;
39
+ where [ AND ] . push ( searchCondition ) ;
40
+ }
41
+
42
+
43
+ if ( filters ) {
44
+ const formattedFilters = await this . filtersParser . perform ( filters ) ;
45
+ where [ AND ] . push ( formattedFilters ) ;
46
+ }
23
47
24
- function findQuery ( queryOptions ) {
48
+ return where ;
49
+ }
50
+
51
+ async findQuery ( queryOptions ) {
25
52
if ( ! queryOptions ) { queryOptions = { } ; }
53
+ const { associationName, recordId } = this . params ;
54
+
55
+ const where = await this . buildWhereConditions ( this . params ) ;
56
+ const include = this . queryBuilder . getIncludes ( this . association , this . fieldNamesRequested ) ;
26
57
27
- return orm . findRecord ( model , params . recordId , {
58
+ const record = await orm . findRecord ( this . model , recordId , {
28
59
order : queryOptions . order ,
29
60
subQuery : false ,
30
61
offset : queryOptions . offset ,
@@ -35,61 +66,69 @@ function HasManyGetter(model, association, opts, params) {
35
66
// and we don't need the parent's attributes
36
67
attributes : [ ] ,
37
68
include : [ {
38
- model : association ,
39
- as : params . associationName ,
69
+ model : this . association ,
70
+ as : associationName ,
40
71
scope : false ,
41
72
required : false ,
42
73
where,
43
74
include,
44
75
} ] ,
45
- } )
46
- . then ( ( record ) => ( ( record && record [ params . associationName ] ) || [ ] ) ) ;
76
+ } ) ;
77
+
78
+ return ( record && record [ associationName ] ) || [ ] ;
47
79
}
48
80
49
- function getCount ( ) {
50
- return model . count ( {
51
- where : { [ primaryKeyModel ] : params . recordId } ,
81
+ async count ( ) {
82
+ const { associationName, recordId } = this . params ;
83
+ const where = await this . buildWhereConditions ( this . params ) ;
84
+ const include = this . queryBuilder . getIncludes ( this . association , this . fieldNamesRequested ) ;
85
+
86
+ return this . model . count ( {
87
+ where : { [ this . primaryKeyModel ] : recordId } ,
52
88
include : [ {
53
- model : association ,
54
- as : params . associationName ,
89
+ model : this . association ,
90
+ as : associationName ,
55
91
where,
56
92
required : true ,
57
93
scope : false ,
94
+ include,
58
95
} ] ,
59
96
} ) ;
60
97
}
61
98
62
- function getRecords ( ) {
99
+ async getRecords ( ) {
100
+ const { associationName } = this . params ;
101
+
63
102
const queryOptions = {
64
- order : queryBuilder . getOrder ( params . associationName , schema ) ,
65
- offset : queryBuilder . getSkip ( ) ,
66
- limit : queryBuilder . getLimit ( ) ,
103
+ order : this . queryBuilder . getOrder ( associationName , this . schema ) ,
104
+ offset : this . queryBuilder . getSkip ( ) ,
105
+ limit : this . queryBuilder . getLimit ( ) ,
67
106
} ;
68
107
69
- return findQuery ( queryOptions )
70
- . then ( ( records ) => P . map ( records , ( record ) => {
71
- if ( schema . isCompositePrimary ) {
72
- record . forestCompositePrimary = new CompositeKeysManager ( association , schema , record )
73
- . createCompositePrimary ( ) ;
74
- }
108
+ const records = await this . findQuery ( queryOptions ) ;
109
+ return P . map ( records , ( record ) => {
110
+ if ( this . schema . isCompositePrimary ) {
111
+ record . forestCompositePrimary = new CompositeKeysManager (
112
+ this . association , this . schema , record ,
113
+ )
114
+ . createCompositePrimary ( ) ;
115
+ }
75
116
76
- return record ;
77
- } ) ) ;
117
+ return record ;
118
+ } ) ;
78
119
}
79
120
80
- this . perform = ( ) =>
81
- getRecords ( )
82
- . then ( ( records ) => {
83
- let fieldsSearched = null ;
121
+ async perform ( ) {
122
+ const records = await this . getRecords ( ) ;
84
123
85
- if ( params . search ) {
86
- fieldsSearched = searchBuilder . getFieldsSearched ( ) ;
87
- }
124
+ let fieldsSearched = null ;
88
125
89
- return [ records , fieldsSearched ] ;
90
- } ) ;
126
+ if ( this . params . search ) {
127
+ fieldsSearched = this . searchBuilder . getFieldsSearched ( ) ;
128
+ }
91
129
92
- this . count = getCount ;
130
+ return [ records , fieldsSearched ] ;
131
+ }
93
132
}
94
133
95
134
module . exports = HasManyGetter ;
0 commit comments