@@ -116,15 +116,25 @@ module.exports = class JSONAPIMongoParser {
116116 if ( lastNode === currentNode ) {
117117 // Parse select for include type
118118 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
119123 const populate = {
120124 path : wantedNode ,
121125 populate : [ ] ,
122126 } ;
123127
128+ // Select
124129 if ( select ) {
125130 populate . select = select ;
126131 }
127132
133+ // Extra options
134+ if ( options ) {
135+ populate . options = options ;
136+ }
137+
128138 const newNode = currentNode [ k ] = populate ;
129139 currentNode = newNode . populate ;
130140 }
@@ -138,6 +148,15 @@ module.exports = class JSONAPIMongoParser {
138148 return ! _ . isEmpty ( output ) ? output : undefined ;
139149 }
140150
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+
141160 _removeDeepEmptyArray ( item ) {
142161 const that = this ;
143162 _ . forOwn ( item , ( value , key ) => {
@@ -151,26 +170,24 @@ module.exports = class JSONAPIMongoParser {
151170 } ) ;
152171 }
153172
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-
163173 _flattenOptions ( ) {
164174 const flatOptions = { } ;
165175 const that = this ;
166176
167177 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+ }
171188
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 ) => {
174191 flatRelationships [ key ] = flattenRelationships ( value ) ;
175192 } ) ;
176193 }
0 commit comments