@@ -484,6 +484,91 @@ class ParseQuery {
484484 } ) . _thenRunCallbacks ( options ) ;
485485 }
486486
487+ /**
488+ * Executes a distinct query and returns unique values
489+ *
490+ * @param {String } key A field to find distinct values
491+ * @param {Object } options A Backbone-style options object. Valid options
492+ * are:<ul>
493+ * <li>success: Function to call when the count completes successfully.
494+ * <li>error: Function to call when the find fails.
495+ * <li>sessionToken: A valid session token, used for making a request on
496+ * behalf of a specific user.
497+ * </ul>
498+ *
499+ * @return {Parse.Promise } A promise that is resolved with the query completes.
500+ */
501+ distinct ( key : string , options ? : FullOptions ) : ParsePromise {
502+ options = options || { } ;
503+
504+ const distinctOptions = {
505+ useMasterKey : true
506+ } ;
507+ if ( options . hasOwnProperty ( 'sessionToken' ) ) {
508+ distinctOptions . sessionToken = options . sessionToken ;
509+ }
510+ const controller = CoreManager . getQueryController ( ) ;
511+ const params = {
512+ distinct : key ,
513+ where : this . _where
514+ } ;
515+
516+ return controller . aggregate (
517+ this . className ,
518+ params ,
519+ distinctOptions
520+ ) . then ( ( results ) => {
521+ return results . results ;
522+ } ) . _thenRunCallbacks ( options ) ;
523+ }
524+
525+ /**
526+ * Executes an aggregate query and returns aggregate results
527+ *
528+ * @param {Mixed } pipeline Array or Object of stages to process query
529+ * @param {Object } options A Backbone-style options object. Valid options
530+ * are:<ul>
531+ * <li>success: Function to call when the count completes successfully.
532+ * <li>error: Function to call when the find fails.
533+ * <li>sessionToken: A valid session token, used for making a request on
534+ * behalf of a specific user.
535+ * </ul>
536+ *
537+ * @return {Parse.Promise } A promise that is resolved with the query completes.
538+ */
539+ aggregate ( pipeline : mixed , options ? : FullOptions ) : ParsePromise {
540+ options = options || { } ;
541+
542+ const aggregateOptions = {
543+ useMasterKey : true
544+ } ;
545+ if ( options . hasOwnProperty ( 'sessionToken' ) ) {
546+ aggregateOptions . sessionToken = options . sessionToken ;
547+ }
548+ const controller = CoreManager . getQueryController ( ) ;
549+ let stages = { } ;
550+
551+ if ( Array . isArray ( pipeline ) ) {
552+ pipeline . forEach ( ( stage ) => {
553+ for ( let op in stage ) {
554+ stages [ op ] = stage [ op ] ;
555+ }
556+ } ) ;
557+ } else if ( pipeline && typeof pipeline === 'object' ) {
558+ stages = pipeline ;
559+ } else {
560+ throw new Error ( 'Invalid pipeline must be Array or Object' ) ;
561+ }
562+
563+ return controller . aggregate (
564+ this . className ,
565+ stages ,
566+ aggregateOptions
567+ ) . then ( ( results ) => {
568+ return results . results ;
569+ } ) . _thenRunCallbacks ( options ) ;
570+ }
571+
487572 /**
488573 * Retrieves at most one Parse.Object that satisfies this query.
489574 *
@@ -1196,6 +1281,17 @@ var DefaultController = {
11961281 params ,
11971282 options
11981283 ) ;
1284+ } ,
1285+
1286+ aggregate ( className : string , params : any , options : RequestOptions ) : ParsePromise {
1287+ const RESTController = CoreManager . getRESTController ( ) ;
1288+
1289+ return RESTController . request (
1290+ 'GET' ,
1291+ 'aggregate/' + className ,
1292+ params ,
1293+ options
1294+ ) ;
11991295 }
12001296} ;
12011297
0 commit comments