@@ -397,31 +397,33 @@ DatabaseController.prototype.owningIds = function(className, key, relatedIds) {
397
397
// Modifies query so that it no longer has $in on relation fields, or
398
398
// equal-to-pointer constraints on relation fields.
399
399
// Returns a promise that resolves when query is mutated
400
- // TODO: this only handles one of these at a time - make it handle more
401
400
DatabaseController . prototype . reduceInRelation = function ( className , query , schema ) {
402
401
// Search for an in-relation or equal-to-relation
403
- for ( var key in query ) {
404
- if ( query [ key ] &&
405
- ( query [ key ] [ '$in' ] || query [ key ] . __type == 'Pointer' ) ) {
406
- var t = schema . getExpectedType ( className , key ) ;
407
- var match = t ? t . match ( / ^ r e l a t i o n < ( .* ) > $ / ) : false ;
408
- if ( ! match ) {
409
- continue ;
410
- }
411
- var relatedClassName = match [ 1 ] ;
412
- var relatedIds ;
413
- if ( query [ key ] [ '$in' ] ) {
414
- relatedIds = query [ key ] [ '$in' ] . map ( r => r . objectId ) ;
415
- } else {
416
- relatedIds = [ query [ key ] . objectId ] ;
402
+ // Make it sequential for now, not sure of paralleization side effects
403
+ return Object . keys ( query ) . reduce ( ( promise , key ) => {
404
+ return promise . then ( ( ) => {
405
+ if ( query [ key ] &&
406
+ ( query [ key ] [ '$in' ] || query [ key ] . __type == 'Pointer' ) ) {
407
+ let t = schema . getExpectedType ( className , key ) ;
408
+ let match = t ? t . match ( / ^ r e l a t i o n < ( .* ) > $ / ) : false ;
409
+ if ( ! match ) {
410
+ return ;
411
+ }
412
+ let relatedClassName = match [ 1 ] ;
413
+ let relatedIds ;
414
+ if ( query [ key ] [ '$in' ] ) {
415
+ relatedIds = query [ key ] [ '$in' ] . map ( r => r . objectId ) ;
416
+ } else {
417
+ relatedIds = [ query [ key ] . objectId ] ;
418
+ }
419
+ return this . owningIds ( className , key , relatedIds ) . then ( ( ids ) => {
420
+ delete query [ key ] ;
421
+ query . objectId = Object . assign ( { '$in' : [ ] } , query . objectId ) ;
422
+ query . objectId [ '$in' ] = query . objectId [ '$in' ] . concat ( ids ) ;
423
+ } ) ;
417
424
}
418
- return this . owningIds ( className , key , relatedIds ) . then ( ( ids ) => {
419
- delete query [ key ] ;
420
- query . objectId = { '$in' : ids } ;
421
- } ) ;
422
- }
423
- }
424
- return Promise . resolve ( ) ;
425
+ } ) ;
426
+ } , Promise . resolve ( ) ) ;
425
427
} ;
426
428
427
429
// Modifies query so that it no longer has $relatedTo
0 commit comments