@@ -20,6 +20,7 @@ function RestQuery(config, auth, className, restWhere = {}, restOptions = {}, cl
20
20
this . auth = auth ;
21
21
this . className = className ;
22
22
this . restWhere = restWhere ;
23
+ this . restOptions = restOptions ;
23
24
this . clientSDK = clientSDK ;
24
25
this . response = null ;
25
26
this . findOptions = { } ;
@@ -56,6 +57,7 @@ function RestQuery(config, auth, className, restWhere = {}, restOptions = {}, cl
56
57
switch ( option ) {
57
58
case 'keys' :
58
59
this . keys = new Set ( restOptions . keys . split ( ',' ) ) ;
60
+ // Add the default
59
61
this . keys . add ( 'objectId' ) ;
60
62
this . keys . add ( 'createdAt' ) ;
61
63
this . keys . add ( 'updatedAt' ) ;
@@ -390,6 +392,11 @@ RestQuery.prototype.runFind = function() {
390
392
this . response = { results : [ ] } ;
391
393
return Promise . resolve ( ) ;
392
394
}
395
+ if ( this . keys ) {
396
+ this . findOptions . keys = Array . from ( this . keys ) . map ( ( key ) => {
397
+ return key . split ( '.' ) [ 0 ] ;
398
+ } ) ;
399
+ }
393
400
return this . config . database . find (
394
401
this . className , this . restWhere , this . findOptions ) . then ( ( results ) => {
395
402
if ( this . className === '_User' ) {
@@ -411,19 +418,6 @@ RestQuery.prototype.runFind = function() {
411
418
412
419
this . config . filesController . expandFilesInObject ( this . config , results ) ;
413
420
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
-
427
421
if ( this . redirectClassName ) {
428
422
for ( var r of results ) {
429
423
r . className = this . redirectClassName ;
@@ -455,7 +449,7 @@ RestQuery.prototype.handleInclude = function() {
455
449
}
456
450
457
451
var pathResponse = includePath ( this . config , this . auth ,
458
- this . response , this . include [ 0 ] ) ;
452
+ this . response , this . include [ 0 ] , this . restOptions ) ;
459
453
if ( pathResponse . then ) {
460
454
return pathResponse . then ( ( newResponse ) => {
461
455
this . response = newResponse ;
@@ -473,7 +467,7 @@ RestQuery.prototype.handleInclude = function() {
473
467
// Adds included values to the response.
474
468
// Path is a list of field names.
475
469
// Returns a promise for an augmented response.
476
- function includePath ( config , auth , response , path ) {
470
+ function includePath ( config , auth , response , path , restOptions = { } ) {
477
471
var pointers = findPointers ( response . results , path ) ;
478
472
if ( pointers . length == 0 ) {
479
473
return response ;
@@ -492,9 +486,26 @@ function includePath(config, auth, response, path) {
492
486
}
493
487
}
494
488
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
+
495
506
let queryPromises = Object . keys ( pointersHash ) . map ( ( className ) => {
496
507
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 ) ;
498
509
return query . execute ( ) . then ( ( results ) => {
499
510
results . className = className ;
500
511
return Promise . resolve ( results ) ;
0 commit comments