@@ -116,15 +116,25 @@ module.exports = class JSONAPIMongoParser {
116
116
if ( lastNode === currentNode ) {
117
117
// Parse select for include type
118
118
const select = this . parseFields ( _ . get ( flatOptions [ type ] , chain . slice ( 0 , j + 1 ) . concat ( [ 'type' ] ) ) , fieldsQuery ) ;
119
+ // Get populate options defined on this relationships
120
+ const options = _ . get ( flatOptions [ type ] , chain . slice ( 0 , j + 1 ) . concat ( [ 'options' ] ) ) ;
121
+
122
+ // Populate query
119
123
const populate = {
120
124
path : wantedNode ,
121
125
populate : [ ] ,
122
126
} ;
123
127
128
+ // Select
124
129
if ( select ) {
125
130
populate . select = select ;
126
131
}
127
132
133
+ // Extra options
134
+ if ( options ) {
135
+ populate . options = options ;
136
+ }
137
+
128
138
const newNode = currentNode [ k ] = populate ;
129
139
currentNode = newNode . populate ;
130
140
}
@@ -138,6 +148,15 @@ module.exports = class JSONAPIMongoParser {
138
148
return ! _ . isEmpty ( output ) ? output : undefined ;
139
149
}
140
150
151
+ parse ( type , query ) {
152
+ return {
153
+ select : this . parseFields ( type , query . fields ) ,
154
+ sort : this . parseSort ( query . sort ) ,
155
+ page : this . parsePage ( query . page ) ,
156
+ populate : this . parseInclude ( type , query . include , query . fields ) ,
157
+ } ;
158
+ }
159
+
141
160
_removeDeepEmptyArray ( item ) {
142
161
const that = this ;
143
162
_ . forOwn ( item , ( value , key ) => {
@@ -151,26 +170,24 @@ module.exports = class JSONAPIMongoParser {
151
170
} ) ;
152
171
}
153
172
154
- parse ( type , query ) {
155
- return {
156
- select : this . parseFields ( type , query . fields ) ,
157
- sort : this . parseSort ( query . sort ) ,
158
- page : this . parsePage ( query . page ) ,
159
- populate : this . parseInclude ( type , query . include , query . fields ) ,
160
- } ;
161
- }
162
-
163
173
_flattenOptions ( ) {
164
174
const flatOptions = { } ;
165
175
const that = this ;
166
176
167
177
const flattenRelationships = function ( type ) {
168
- const flatRelationships = {
169
- type : type ,
170
- } ;
178
+ let flatRelationships ;
179
+
180
+ // Type definition can be a string or an object with extra options for population query
181
+ if ( _ . isString ( type ) ) {
182
+ flatRelationships = {
183
+ type : type ,
184
+ } ;
185
+ } else {
186
+ flatRelationships = type ;
187
+ }
171
188
172
- if ( that . options [ type ] && that . options [ type ] . relationships ) {
173
- _ . forOwn ( that . options [ type ] . relationships , ( value , key ) => {
189
+ if ( that . options [ flatRelationships . type ] && that . options [ flatRelationships . type ] . relationships ) {
190
+ _ . forOwn ( that . options [ flatRelationships . type ] . relationships , ( value , key ) => {
174
191
flatRelationships [ key ] = flattenRelationships ( value ) ;
175
192
} ) ;
176
193
}
0 commit comments