@@ -20,6 +20,7 @@ function RestQuery(config, auth, className, restWhere = {}, restOptions = {}, cl
2020 this . auth = auth ;
2121 this . className = className ;
2222 this . restWhere = restWhere ;
23+ this . restOptions = restOptions ;
2324 this . clientSDK = clientSDK ;
2425 this . response = null ;
2526 this . findOptions = { } ;
@@ -56,6 +57,7 @@ function RestQuery(config, auth, className, restWhere = {}, restOptions = {}, cl
5657 switch ( option ) {
5758 case 'keys' :
5859 this . keys = new Set ( restOptions . keys . split ( ',' ) ) ;
60+ // Add the default
5961 this . keys . add ( 'objectId' ) ;
6062 this . keys . add ( 'createdAt' ) ;
6163 this . keys . add ( 'updatedAt' ) ;
@@ -390,6 +392,11 @@ RestQuery.prototype.runFind = function() {
390392 this . response = { results : [ ] } ;
391393 return Promise . resolve ( ) ;
392394 }
395+ if ( this . keys ) {
396+ this . findOptions . keys = Array . from ( this . keys ) . map ( ( key ) => {
397+ return key . split ( '.' ) [ 0 ] ;
398+ } ) ;
399+ }
393400 return this . config . database . find (
394401 this . className , this . restWhere , this . findOptions ) . then ( ( results ) => {
395402 if ( this . className === '_User' ) {
@@ -411,19 +418,6 @@ RestQuery.prototype.runFind = function() {
411418
412419 this . config . filesController . expandFilesInObject ( this . config , results ) ;
413420
414- if ( this . keys ) {
415- var keySet = this . keys ;
416- results = results . map ( ( object ) => {
417- var newObject = { } ;
418- for ( var key in object ) {
419- if ( keySet . has ( key ) ) {
420- newObject [ key ] = object [ key ] ;
421- }
422- }
423- return newObject ;
424- } ) ;
425- }
426-
427421 if ( this . redirectClassName ) {
428422 for ( var r of results ) {
429423 r . className = this . redirectClassName ;
@@ -455,7 +449,7 @@ RestQuery.prototype.handleInclude = function() {
455449 }
456450
457451 var pathResponse = includePath ( this . config , this . auth ,
458- this . response , this . include [ 0 ] ) ;
452+ this . response , this . include [ 0 ] , this . restOptions ) ;
459453 if ( pathResponse . then ) {
460454 return pathResponse . then ( ( newResponse ) => {
461455 this . response = newResponse ;
@@ -473,7 +467,7 @@ RestQuery.prototype.handleInclude = function() {
473467// Adds included values to the response.
474468// Path is a list of field names.
475469// Returns a promise for an augmented response.
476- function includePath ( config , auth , response , path ) {
470+ function includePath ( config , auth , response , path , restOptions = { } ) {
477471 var pointers = findPointers ( response . results , path ) ;
478472 if ( pointers . length == 0 ) {
479473 return response ;
@@ -492,9 +486,26 @@ function includePath(config, auth, response, path) {
492486 }
493487 }
494488
489+ let includeRestOptions = { } ;
490+ if ( restOptions . keys ) {
491+ let keys = new Set ( restOptions . keys . split ( ',' ) ) ;
492+ let keySet = Array . from ( keys ) . reduce ( ( set , key ) => {
493+ let keyPath = key . split ( '.' ) ;
494+ let i = 0 ;
495+ for ( i ; i < path . length ; i ++ ) {
496+ if ( path [ i ] != keyPath [ i ] ) {
497+ return set ;
498+ }
499+ }
500+ set . add ( keyPath [ i ] ) ;
501+ return set ;
502+ } , new Set ( ) ) ;
503+ includeRestOptions . keys = Array . from ( keySet ) . join ( ',' ) ;
504+ }
505+
495506 let queryPromises = Object . keys ( pointersHash ) . map ( ( className ) => {
496507 var where = { 'objectId' : { '$in' : pointersHash [ className ] } } ;
497- var query = new RestQuery ( config , auth , className , where ) ;
508+ var query = new RestQuery ( config , auth , className , where , includeRestOptions ) ;
498509 return query . execute ( ) . then ( ( results ) => {
499510 results . className = className ;
500511 return Promise . resolve ( results ) ;
0 commit comments