@@ -457,7 +457,6 @@ Memory.prototype._findAllSkippingIncludes = function(model, filter) {
457457 let nodes = Object . keys ( this . collection ( model ) ) . map ( function ( key ) {
458458 return this . fromDb ( model , this . collection ( model ) [ key ] ) ;
459459 } . bind ( this ) ) ;
460-
461460 if ( filter ) {
462461 if ( ! filter . order ) {
463462 const idNames = this . idNames ( model ) ;
@@ -502,7 +501,41 @@ Memory.prototype._findAllSkippingIncludes = function(model, filter) {
502501 // limit/skip
503502 const skip = filter . skip || filter . offset || 0 ;
504503 const limit = filter . limit || nodes . length ;
504+ // groupBy
505505 nodes = nodes . slice ( skip , skip + limit ) ;
506+ if ( filter . groupBy ) {
507+ nodes = utils . groupBy ( nodes , filter . groupBy ) ;
508+ const tempNodes = [ ] ;
509+ Object . keys ( nodes ) . forEach ( nodeKey => {
510+ let count = undefined ;
511+ const tempNode = { ...nodes [ nodeKey ] [ 0 ] } ;
512+ if ( filter . count ) {
513+ count = nodes [ nodeKey ] . filter ( ( obj ) => {
514+ const id = obj [ filter . count ] ;
515+ return obj [ filter . count ] === id ;
516+ } ) . length ;
517+ tempNode . count = count ;
518+ }
519+ if ( filter . max ) {
520+ tempNode . max = Math . max ( ...nodes [ nodeKey ] . map ( o => o [ filter . max ] ) ) ;
521+ }
522+ if ( filter . min ) {
523+ tempNode . min = Math . min ( ...nodes [ nodeKey ] . map ( o => o [ filter . min ] ) ) ;
524+ }
525+ if ( filter . sum ) {
526+ tempNode . sum = nodes [ nodeKey ] . reduce ( ( accumulator , object ) => {
527+ return accumulator + object [ filter . sum ] ;
528+ } , 0 ) ;
529+ }
530+ if ( filter . avg ) {
531+ tempNode . avg = nodes [ nodeKey ] . reduce ( ( accumulator , object ) => {
532+ return accumulator + object [ filter . avg ] ;
533+ } , 0 ) ;
534+ tempNode . avg = tempNode . avg / nodes [ nodeKey ] . length ;
535+ }
536+ } ) ;
537+ nodes = tempNodes ;
538+ }
506539 }
507540 return nodes ;
508541
@@ -529,7 +562,6 @@ Memory.prototype._findAllSkippingIncludes = function(model, filter) {
529562Memory . prototype . all = function all ( model , filter , options , callback ) {
530563 const self = this ;
531564 const nodes = self . _findAllSkippingIncludes ( model , filter ) ;
532-
533565 process . nextTick ( function ( ) {
534566 if ( filter && filter . include ) {
535567 self . _models [ model ] . model . include ( nodes , filter . include , options , callback ) ;
0 commit comments