@@ -257,68 +257,70 @@ exports.setSchema = function (schema) {
257257
258258 foundObjects . get ( type ) . set ( JSON . stringify ( obj . id ) , obj ) ;
259259
260- // fetch all relations
261260 var subTasks = [ ] ;
262- Object . keys ( typeInfo . relations || { } ) . forEach ( function ( field ) {
263- var relationDef = typeInfo . relations [ field ] ;
264- var relationType = Object . keys ( relationDef ) [ 0 ] ;
265- var relatedType = relationDef [ relationType ] ;
266- if ( typeof relatedType !== 'string' ) {
267- var relationOptions = relatedType . options ;
268- var async = idOrIds && idOrIds . async ;
269- if ( async || ( relationOptions && relationOptions . async && ( async === undefined ) ) ) {
270- return ;
271- }
272- relatedType = relatedType . type ;
273- }
274- if ( relationType === 'belongsTo' ) {
275- var relatedId = obj [ field ] ;
276- if ( typeof relatedId !== 'undefined' ) {
277- subTasks . push ( Promise . resolve ( ) . then ( function ( ) {
278-
279- // short-circuit if it's already in the foundObjects
280- // else we could get caught in an infinite loop
281- if ( foundObjects . has ( relatedType ) &&
282- foundObjects . get ( relatedType ) . has ( JSON . stringify ( relatedId ) ) ) {
283- return ;
284- }
285-
286- // signal that we need to fetch it
287- return {
288- relatedType : relatedType ,
289- relatedIds : [ relatedId ]
290- } ;
291- } ) ) ;
261+
262+ if ( ! idOrIds || ! idOrIds . async ) {
263+ // fetch all relations
264+ Object . keys ( typeInfo . relations || { } ) . forEach ( function ( field ) {
265+ var relationDef = typeInfo . relations [ field ] ;
266+ var relationType = Object . keys ( relationDef ) [ 0 ] ;
267+ var relatedType = relationDef [ relationType ] ;
268+ if ( typeof relatedType !== 'string' ) {
269+ var relationOptions = relatedType . options ;
270+ if ( relationOptions && relationOptions . async && typeof idOrIds === 'undefined' ) {
271+ return ;
272+ }
273+ relatedType = relatedType . type ;
292274 }
293- } else { // hasMany
294- var relatedIds = extend ( true , [ ] , obj [ field ] ) ;
295- if ( typeof relatedIds !== 'undefined' && relatedIds . length ) {
296- subTasks . push ( Promise . resolve ( ) . then ( function ( ) {
297-
298- // filter out all ids that are already in the foundObjects
299- for ( var i = relatedIds . length - 1 ; i >= 0 ; i -- ) {
300- var relatedId = relatedIds [ i ] ;
275+ if ( relationType === 'belongsTo' ) {
276+ var relatedId = obj [ field ] ;
277+ if ( typeof relatedId !== 'undefined' ) {
278+ subTasks . push ( Promise . resolve ( ) . then ( function ( ) {
279+
280+ // short-circuit if it's already in the foundObjects
281+ // else we could get caught in an infinite loop
301282 if ( foundObjects . has ( relatedType ) &&
302283 foundObjects . get ( relatedType ) . has ( JSON . stringify ( relatedId ) ) ) {
303- delete relatedIds [ i ] ;
284+ return ;
304285 }
305- }
306- relatedIds = relatedIds . filter ( function ( relatedId ) {
307- return typeof relatedId !== 'undefined' ;
308- } ) ;
309-
310- // just return the ids and the types. We'll find them all
311- // in a single bulk operation in order to minimize HTTP requests
312- if ( relatedIds . length ) {
286+
287+ // signal that we need to fetch it
313288 return {
314289 relatedType : relatedType ,
315- relatedIds : relatedIds
290+ relatedIds : [ relatedId ]
316291 } ;
317- }
318- } ) ) ;
292+ } ) ) ;
293+ }
294+ } else { // hasMany
295+ var relatedIds = extend ( true , [ ] , obj [ field ] ) ;
296+ if ( typeof relatedIds !== 'undefined' && relatedIds . length ) {
297+ subTasks . push ( Promise . resolve ( ) . then ( function ( ) {
298+
299+ // filter out all ids that are already in the foundObjects
300+ for ( var i = relatedIds . length - 1 ; i >= 0 ; i -- ) {
301+ var relatedId = relatedIds [ i ] ;
302+ if ( foundObjects . has ( relatedType ) &&
303+ foundObjects . get ( relatedType ) . has ( JSON . stringify ( relatedId ) ) ) {
304+ delete relatedIds [ i ] ;
305+ }
306+ }
307+ relatedIds = relatedIds . filter ( function ( relatedId ) {
308+ return typeof relatedId !== 'undefined' ;
309+ } ) ;
310+
311+ // just return the ids and the types. We'll find them all
312+ // in a single bulk operation in order to minimize HTTP requests
313+ if ( relatedIds . length ) {
314+ return {
315+ relatedType : relatedType ,
316+ relatedIds : relatedIds
317+ } ;
318+ }
319+ } ) ) ;
320+ }
319321 }
320- }
321- } ) ;
322+ } ) ;
323+ }
322324 return Promise . all ( subTasks ) ;
323325 } ) ;
324326 return Promise . all ( tasks ) ;
0 commit comments